aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2015-01-18 20:30:23 +0000
committerdrh <drh@noemail.net>2015-01-18 20:30:23 +0000
commit047d4538e3f6f35c9369f27cde2982baf3ca59c6 (patch)
treee49f49d0455ee69a4c5e4347c91df7cf4e5c0601 /src
parent5d907be5c9c2eb195f747a3112edf5aab4e42c98 (diff)
downloadsqlite-047d4538e3f6f35c9369f27cde2982baf3ca59c6.tar.gz
sqlite-047d4538e3f6f35c9369f27cde2982baf3ca59c6.zip
Set binary mode for output on Windows when writing a quoted string that
might contain newline characters. FossilOrigin-Name: 7096e6c06d9a3e48d3f0d134f5f3275dde796be2
Diffstat (limited to 'src')
-rw-r--r--src/shell.c36
1 files changed, 25 insertions, 11 deletions
diff --git a/src/shell.c b/src/shell.c
index 9e23734ae..5643e4f24 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -106,6 +106,26 @@ extern int pclose(FILE*);
#define IsDigit(X) isdigit((unsigned char)X)
#define ToLower(X) (char)tolower((unsigned char)X)
+/* On Windows, we normally run with output mode of TEXT so that \n characters
+** are automatically translated into \r\n. However, this behavior needs
+** to be disabled in some cases (ex: when generating CSV output and when
+** rendering quoted strings that contain \n characters). The following
+** routines take care of that.
+*/
+#if defined(_WIN32) || defined(WIN32)
+static setBinaryMode(FILE *out){
+ fflush(out);
+ _setmode(_fileno(out), _O_BINARY);
+}
+static setTextMode(FILE *out){
+ fflush(out);
+ _setmode(_fileno(out), _O_TEXT);
+}
+#else
+# define setBinaryMode(X)
+# define setTextMode(X)
+#endif
+
/* True if the timer is enabled */
static int enableTimer = 0;
@@ -584,6 +604,7 @@ static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
static void output_quoted_string(FILE *out, const char *z){
int i;
int nSingle = 0;
+ setBinaryMode(out);
for(i=0; z[i]; i++){
if( z[i]=='\'' ) nSingle++;
}
@@ -606,6 +627,7 @@ static void output_quoted_string(FILE *out, const char *z){
}
fprintf(out,"'");
}
+ setTextMode(out);
}
/*
@@ -908,10 +930,7 @@ static int shell_callback(
break;
}
case MODE_Csv: {
-#if defined(WIN32) || defined(_WIN32)
- fflush(p->out);
- _setmode(_fileno(p->out), _O_BINARY);
-#endif
+ setBinaryMode(p->out);
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
@@ -924,10 +943,7 @@ static int shell_callback(
}
fprintf(p->out, "%s", p->rowSeparator);
}
-#if defined(WIN32) || defined(_WIN32)
- fflush(p->out);
- _setmode(_fileno(p->out), _O_TEXT);
-#endif
+ setTextMode(p->out);
break;
}
case MODE_Insert: {
@@ -4176,9 +4192,7 @@ int main(int argc, char **argv){
exit(1);
}
#endif
-#if defined(WIN32) || defined(_WIN32)
- _setmode(0, _O_BINARY);
-#endif
+ setBinaryMode(stdin);
Argv0 = argv[0];
main_init(&data);
stdin_is_interactive = isatty(0);