[or-cvs] wrap strdup; prefer time() to gettimeofday()

Roger Dingledine arma at seul.org
Sat Oct 4 03:29:10 UTC 2003


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/common

Modified Files:
	log.c util.c util.h 
Log Message:
wrap strdup; prefer time() to gettimeofday()


Index: log.c
===================================================================
RCS file: /home/or/cvsroot/src/common/log.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- log.c	4 Oct 2003 01:36:11 -0000	1.19
+++ log.c	4 Oct 2003 03:29:07 -0000	1.20
@@ -40,7 +40,7 @@
 
   buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
   
-  my_gettimeofday(&now);
+  tor_gettimeofday(&now);
   t = (time_t)now.tv_sec;
   
   n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- util.c	4 Oct 2003 01:36:11 -0000	1.25
+++ util.c	4 Oct 2003 03:29:07 -0000	1.26
@@ -9,7 +9,7 @@
 #endif
 
 /*
- *    Memory
+ *    Memory wrappers
  */
 
 void *tor_malloc(size_t size) {
@@ -22,17 +22,26 @@
     exit(1);
   }
   memset(result,'X',size); /* XXX deadbeef to encourage bugs */
-
   return result;
 }
 
+char *tor_strdup(const char *s) {
+  char *dup;
+  assert(s);
+
+  dup = strdup(s);
+  if(!dup) {
+    log_fn(LOG_ERR,"Out of memory. Dying.");
+    exit(1);
+  }
+  return dup;
+}
+
 /*
  *    Time
  */
 
-void 
-my_gettimeofday(struct timeval *timeval) 
-{
+void tor_gettimeofday(struct timeval *timeval) {
 #ifdef HAVE_GETTIMEOFDAY
   if (gettimeofday(timeval, NULL)) {
     log_fn(LOG_ERR, "gettimeofday failed.");
@@ -141,6 +150,10 @@
  *   Process control
  */
 
+/* Minimalist interface to run a void function in the background.  On
+ * unix calls fork, on win32 calls beginthread.  Returns -1 on failure.
+ * func should not return, but rather should call spawn_exit.
+ */
 int spawn_func(int (*func)(void *), void *data)
 {
 #ifdef MS_WINDOWS
@@ -294,6 +307,10 @@
 /*
  *    Filesystem operations.
  */
+
+/* Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
+ * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
+ * directory. */
 file_status_t file_status(const char *fname)
 {
   struct stat st;
@@ -311,6 +328,8 @@
     return FN_ERROR;
 }
 
+/* Check whether dirname exists and is private.  If yes returns
+   0.  Else returns -1. */
 int check_private_dir(const char *dirname, int create)
 {
   struct stat st;

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- util.h	1 Oct 2003 22:31:12 -0000	1.15
+++ util.h	4 Oct 2003 03:29:07 -0000	1.16
@@ -33,11 +33,9 @@
 #endif
 
 void *tor_malloc(size_t size);
+char *tor_strdup(const char *s);
+void tor_gettimeofday(struct timeval *timeval);
 
-/* Same as gettimeofday, but no need to check exit value. */
-void my_gettimeofday(struct timeval *timeval);
-/* Returns the number of microseconds between start and end.  Requires that
- * end >= start, and that the number of microseconds < LONG_MAX. */
 long tv_udiff(struct timeval *start, struct timeval *end);
 
 void tv_addms(struct timeval *a, long ms);
@@ -51,22 +49,12 @@
 
 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
 
-/* Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
- * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
- * directory. */
 file_status_t file_status(const char *filename);
-/* Check whether dirname exists and is private.  If yes returns
- * 0.  Else returns -1.
- */
 int check_private_dir(const char *dirname, int create);
 int write_str_to_file(const char *fname, const char *str);
 char *read_file_to_str(const char *filename);
 int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
 
-/* Minimalist interface to run a void function in the background.  On
-   unix calls fork, on win32 calls beginthread.  Returns -1 on failure.
-   func should not return, but rather should call spawn_exit.
-*/
 int spawn_func(int (*func)(void *), void *data);
 void spawn_exit();
 



More information about the tor-commits mailing list