]> granicus.if.org Git - curl/commitdiff
curl_setup: Add macros for FOPEN_READTEXT, FOPEN_WRITETEXT
authorJay Satiro <raysatiro@yahoo.com>
Mon, 1 Jun 2015 07:20:18 +0000 (03:20 -0400)
committerJay Satiro <raysatiro@yahoo.com>
Mon, 1 Jun 2015 07:21:23 +0000 (03:21 -0400)
- Change fopen calls to use FOPEN_READTEXT instead of "r" or "rt"
- Change fopen calls to use FOPEN_WRITETEXT instead of "w" or "wt"

This change is to explicitly specify when we need to read/write text.
Unfortunately 't' is not part of POSIX fopen so we can't specify it
directly. Instead we now have FOPEN_READTEXT, FOPEN_WRITETEXT.

Prior to this change we had an issue on Windows if an application that
uses libcurl overrides the default file mode to binary. The default file
mode in Windows is normally text mode (translation mode) and that's what
libcurl expects.

Bug: https://github.com/bagder/curl/pull/258#issuecomment-107093055
Reported-by: Orgad Shaneh
lib/cookie.c
lib/curl_setup.h
lib/memdebug.c
lib/netrc.c
lib/vtls/gtls.c
lib/vtls/openssl.c
src/tool_cb_dbg.c
src/tool_easysrc.c
src/tool_getparam.c
src/tool_parsecfg.c

index 012792605a20c094e8e0c0a4b617b6b2b87e2130..fd7ed4168f9c0b28f84f3e97394e40a09a92a1f8 100644 (file)
@@ -914,7 +914,7 @@ struct CookieInfo *Curl_cookie_init(struct SessionHandle *data,
     fp = NULL;
   }
   else
-    fp = file?fopen(file, "r"):NULL;
+    fp = file?fopen(file, FOPEN_READTEXT):NULL;
 
   c->newsession = newsession; /* new session? */
 
@@ -1262,7 +1262,7 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere)
     use_stdout=TRUE;
   }
   else {
-    out = fopen(dumphere, "w");
+    out = fopen(dumphere, FOPEN_WRITETEXT);
     if(!out)
       return 1; /* failure */
   }
index 9c7cc07ebef42a3b332f30f0a884b80bda3587b7..cbec34f26f9089c1c655b2675a676d6ca5f2fccd 100644 (file)
@@ -707,4 +707,24 @@ int netware_init(void);
 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
 #endif
 
+/* In Windows the default file mode is text but an application can override it.
+Therefore we specify it explicitly. https://github.com/bagder/curl/pull/258
+*/
+#if defined(WIN32)
+#define FOPEN_READTEXT "rt"
+#define FOPEN_WRITETEXT "wt"
+#elif defined(__CYGWIN__)
+/* Cygwin has specific behavior we need to address when WIN32 is not defined.
+https://cygwin.com/cygwin-ug-net/using-textbinary.html
+For write we want our output to have line endings of LF and be compatible with
+other Cygwin utilities. For read we want to handle input that may have line
+endings either CRLF or LF so 't' is appropriate.
+*/
+#define FOPEN_READTEXT "rt"
+#define FOPEN_WRITETEXT "w"
+#else
+#define FOPEN_READTEXT "r"
+#define FOPEN_WRITETEXT "w"
+#endif
+
 #endif /* HEADER_CURL_SETUP_H */
index 3b38a749259ed6a8a2730359d03b4a9ad5b3ddeb..dd8889b2db13ffa239185e3c3c18643506ec88e1 100644 (file)
@@ -112,7 +112,7 @@ void curl_memdebug(const char *logname)
 {
   if(!logfile) {
     if(logname && *logname)
-      logfile = fopen(logname, "w");
+      logfile = fopen(logname, FOPEN_WRITETEXT);
     else
       logfile = stderr;
 #ifdef MEMDEBUG_LOG_SYNC
index 97a07b88e2da8d1c1d868d5618b0871c4b68cf85..06f8ea15a41ac6a3f6de29be9fd0f1406c4e3b52 100644 (file)
@@ -109,11 +109,7 @@ int Curl_parsenetrc(const char *host,
     netrc_alloc = TRUE;
   }
 
-#ifdef __CYGWIN__
-  file = fopen(netrcfile, "rt");
-#else
-  file = fopen(netrcfile, "r");
-#endif
+  file = fopen(netrcfile, FOPEN_READTEXT);
   if(netrc_alloc)
     free(netrcfile);
   if(file) {
index d6eb6c445e0f9bd4b46cb675e54ae50f3419e154..1db31e40c253c8d6cb62a016e01af859f4ef9dc2 100644 (file)
@@ -231,7 +231,7 @@ static gnutls_datum_t load_file (const char *file)
   long filelen;
   void *ptr;
 
-  if(!(f = fopen(file, "r")))
+  if(!(f = fopen(file, "rb")))
     return loaded_file;
   if(fseek(f, 0, SEEK_END) != 0
      || (filelen = ftell(f)) < 0
index 96a7d6e89c5c1cca29927749b7ea9a6b52953064..eb2cf5bf57b3c777e77d6d6335b464bc92704a2e 100644 (file)
@@ -2725,7 +2725,7 @@ static CURLcode servercert(struct connectdata *conn,
 
     /* e.g. match issuer name with provided issuer certificate */
     if(data->set.str[STRING_SSL_ISSUERCERT]) {
-      fp = fopen(data->set.str[STRING_SSL_ISSUERCERT], "r");
+      fp = fopen(data->set.str[STRING_SSL_ISSUERCERT], FOPEN_READTEXT);
       if(!fp) {
         if(strict)
           failf(data, "SSL: Unable to open issuer cert (%s)",
index 0fe0f13475e2f031ff834314ab3ade0cb76384d6..f52714683c5cb3110a82f4300cd825ef1ca08f3b 100644 (file)
@@ -79,7 +79,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
       /* Ok, this is somewhat hackish but we do it undocumented for now */
       config->trace_stream = config->errors;  /* aka stderr */
     else {
-      config->trace_stream = fopen(config->trace_dump, "w");
+      config->trace_stream = fopen(config->trace_dump, FOPEN_WRITETEXT);
       config->trace_fopened = TRUE;
     }
   }
index e1336c3d9dad5317423040b4b83bfe3b400c6ea2..0482ef6ae95bf2f00e8a9c81674c18d0b7f8eb4d 100644 (file)
@@ -176,7 +176,7 @@ void dumpeasysrc(struct GlobalConfig *config)
     FILE *out;
     bool fopened = FALSE;
     if(strcmp(o, "-")) {
-      out = fopen(o, "w");
+      out = fopen(o, FOPEN_WRITETEXT);
       fopened = TRUE;
     }
     else
index 62283a5dd3d4a10f62b1faa31ef7f7407271e1c8..c86e6b4b11df4e59d7e5c5c0ae8f10fdf64aba12 100644 (file)
@@ -681,7 +681,7 @@ ParameterError getparameter(char *flag,    /* f or -long-flag */
 
       case 'v': /* --stderr */
         if(strcmp(nextarg, "-")) {
-          FILE *newfile = fopen(nextarg, "wt");
+          FILE *newfile = fopen(nextarg, FOPEN_WRITETEXT);
           if(!newfile)
             warnf(global, "Failed to open %s!\n", nextarg);
           else {
@@ -1748,7 +1748,7 @@ ParameterError getparameter(char *flag,    /* f or -long-flag */
         }
         else {
           fname = nextarg;
-          file = fopen(nextarg, "r");
+          file = fopen(nextarg, FOPEN_READTEXT);
         }
         err = file2string(&config->writeout, file);
         if(file && (file != stdin))
index 4c25ddbd511a1e7342fe594464e26786f9bab87b..39b43eef43e944942bc7b1b00463ca7bc4aed6eb 100644 (file)
@@ -69,7 +69,7 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
         /* Check if the file exists - if not, try CURLRC in the same
          * directory as our executable
          */
-        file = fopen(filebuffer, "r");
+        file = fopen(filebuffer, FOPEN_READTEXT);
         if(file != NULL) {
           fclose(file);
           filename = filebuffer;
@@ -115,7 +115,7 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
   }
 
   if(strcmp(filename, "-"))
-    file = fopen(filename, "r");
+    file = fopen(filename, FOPEN_READTEXT);
   else
     file = stdin;