- 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
fp = NULL;
}
else
- fp = file?fopen(file, "r"):NULL;
+ fp = file?fopen(file, FOPEN_READTEXT):NULL;
c->newsession = newsession; /* new session? */
use_stdout=TRUE;
}
else {
- out = fopen(dumphere, "w");
+ out = fopen(dumphere, FOPEN_WRITETEXT);
if(!out)
return 1; /* failure */
}
#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 */
{
if(!logfile) {
if(logname && *logname)
- logfile = fopen(logname, "w");
+ logfile = fopen(logname, FOPEN_WRITETEXT);
else
logfile = stderr;
#ifdef MEMDEBUG_LOG_SYNC
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) {
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
/* 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)",
/* 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;
}
}
FILE *out;
bool fopened = FALSE;
if(strcmp(o, "-")) {
- out = fopen(o, "w");
+ out = fopen(o, FOPEN_WRITETEXT);
fopened = TRUE;
}
else
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 {
}
else {
fname = nextarg;
- file = fopen(nextarg, "r");
+ file = fopen(nextarg, FOPEN_READTEXT);
}
err = file2string(&config->writeout, file);
if(file && (file != stdin))
/* 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;
}
if(strcmp(filename, "-"))
- file = fopen(filename, "r");
+ file = fopen(filename, FOPEN_READTEXT);
else
file = stdin;