summaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-11-09 19:13:08 +0000
committerNick Mathewson <nickm@torproject.org>2004-11-09 19:13:08 +0000
commit7daab4034dff228245cd8cdc16191457087fcd01 (patch)
tree40db4f6d36b0d8d89857d3066793ff63067270e4 /src/common/util.c
parentcd753df7bf741f5063d672afaf23390ebbd55c48 (diff)
downloadtor-7daab4034dff228245cd8cdc16191457087fcd01.tar.gz
tor-7daab4034dff228245cd8cdc16191457087fcd01.zip
Fix windows build for VC6; centralize newline-fiasco-damage-control logic
svn:r2756
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/common/util.c b/src/common/util.c
index d3c044db88..8f0922c399 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -384,6 +384,7 @@ tor_parse_ulong(const char *s, int base, unsigned long min,
CHECK_STRTOX_RESULT();
}
+/** Only base 10 is guaranteed to work for now. */
uint64_t
tor_parse_uint64(const char *s, int base, uint64_t min,
uint64_t max, int *ok, char **next)
@@ -394,7 +395,15 @@ tor_parse_uint64(const char *s, int base, uint64_t min,
#ifdef HAVE_STRTOULL
r = (uint64_t)strtoull(s, &endptr, base);
#elif defined(MS_WINDOWS)
+#if _MSC_VER < 1300
+ tor_assert(base <= 10);
+ r = (uint64_t)_atoi64(s);
+ endptr = (char*)s;
+ while(isspace(*endptr)) endptr++;
+ while(isdigit(*endptr)) endptr++;
+#else
r = (uint64_t)_strtoui64(s, &endptr, base);
+#endif
#elif SIZEOF_LONG == 8
r = (uint64_t)strtoul(s, &endptr, base);
#else
@@ -779,6 +788,11 @@ int check_private_dir(const char *dirname, cpd_check_t check)
int
write_str_to_file(const char *fname, const char *str, int bin)
{
+#ifdef MS_WINDOWS
+ if (strchr(str, '\r')) {
+ log_fn(LOG_WARN, "How odd. Writing a string that does contain CR already.");
+ }
+#endif
return write_bytes_to_file(fname, str, strlen(str), bin);
}
@@ -849,7 +863,10 @@ char *read_file_to_str(const char *filename, int bin) {
tor_free(string);
close(fd);
return NULL;
- } else if (bin && r != statbuf.st_size) {
+ }
+ string[r] = '\0'; /* NUL-terminate the result. */
+
+ if (bin && r != statbuf.st_size) {
/* If we're in binary mode, then we'd better have an exact match for
* size. Otherwise, win32 encoding may throw us off, and that's okay. */
log_fn(LOG_WARN,"Could read only %d of %ld bytes of file '%s'.",
@@ -858,9 +875,17 @@ char *read_file_to_str(const char *filename, int bin) {
close(fd);
return NULL;
}
+#ifdef MS_WINDOWS
+ if (!bin && strchr(string, '\r')) {
+ if (strchr(string, '\r')) {
+ log_fn(LOG_DEBUG, "We didn't convert CRLF to LF as well as we hoped when reading %s. Coping.",
+ filename);
+ tor_strstrip(string, "\r");
+ }
+ }
+#endif
close(fd);
- string[statbuf.st_size] = 0; /* null terminate it */
return string;
}