[tor-commits] [torperf/master] Fix compilation on MacOS X

karsten at torproject.org karsten at torproject.org
Sat Jul 30 01:20:43 UTC 2011


commit a8cb23f556b9391562ad0e632d9f05a332eaaf95
Author: Steven Murdoch <Steven.Murdoch at cl.cam.ac.uk>
Date:   Fri Jul 29 16:54:44 2011 +0100

    Fix compilation on MacOS X
    
    - Properly cast parameters to printf
    - Replace GNU-specific strndup with tor_strndup
---
 trivsocks-client.c |    2 +-
 util.c             |   23 ++++++++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/trivsocks-client.c b/trivsocks-client.c
index 70e29a7..d68aaca 100644
--- a/trivsocks-client.c
+++ b/trivsocks-client.c
@@ -249,7 +249,7 @@ do_http_get(int s, const char *path, const char *hostname, size_t *read_bytes, s
 
 static int
 print_time(struct timeval t) {
-  return printf("%ld %ld ", t.tv_sec, t.tv_usec);
+  return printf("%ld %ld ", (long int)t.tv_sec, (long int)t.tv_usec);
 }
 
 // Timestamps of important events
diff --git a/util.c b/util.c
index 96cee0a..fcc8ef9 100644
--- a/util.c
+++ b/util.c
@@ -73,6 +73,27 @@ read_all(int fd, char *buf, size_t count, int isSocket)
   return numread;
 }
 
+/** Allocate and return a new string containing the first <b>n</b>
+ * characters of <b>s</b>.  If <b>s</b> is longer than <b>n</b>
+ * characters, only the first <b>n</b> are copied.  The result is
+ * always NUL-terminated.
+ */
+char *
+tor_strndup(const char *s, size_t n)
+{
+  char *dup;
+  dup = malloc(n+1);
+  /* Performance note: Ordinarily we prefer strlcpy to strncpy.  But
+   * this function gets called a whole lot, and platform strncpy is
+   * much faster than strlcpy when strlen(s) is much longer than n.
+   */
+  if (!dup)
+      return dup;
+  strncpy(dup, s, n);
+  dup[n]='\0';
+  return dup;
+}
+
 /**
  * Read a 16-bit value beginning at <b>cp</b>.  Equivalent to
  * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
@@ -207,7 +228,7 @@ parse_addr_port(int severity, const char *addrport, char **address,
 
   colon = strchr(addrport, ':');
   if (colon) {
-    _address = strndup(addrport, colon-addrport);
+    _address = tor_strndup(addrport, colon-addrport);
     _port = (int) parse_long(colon+1,10,1,65535,NULL,NULL);
     if (!_port) {
       fprintf(stderr, "Port %s out of range\n", colon+1);





More information about the tor-commits mailing list