summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorRoger Dingledine <arma@torproject.org>2003-10-04 03:29:09 +0000
committerRoger Dingledine <arma@torproject.org>2003-10-04 03:29:09 +0000
commitbe874358a4385d8d6bd7f82d3f372bb79055033c (patch)
tree72b615a6357820b49e9a66ab32c3554463a7834c /src/common
parentf563bbd2f988cd1f6ca292f89a47a86f98fe0e8d (diff)
downloadtor-be874358a4385d8d6bd7f82d3f372bb79055033c.tar.gz
tor-be874358a4385d8d6bd7f82d3f372bb79055033c.zip
wrap strdup; prefer time() to gettimeofday()
svn:r538
Diffstat (limited to 'src/common')
-rw-r--r--src/common/log.c2
-rw-r--r--src/common/util.c29
-rw-r--r--src/common/util.h16
3 files changed, 27 insertions, 20 deletions
diff --git a/src/common/log.c b/src/common/log.c
index 81a7091574..70646201a8 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -40,7 +40,7 @@ static INLINE void format_msg(char *buf, size_t buf_len,
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));
diff --git a/src/common/util.c b/src/common/util.c
index c2ca39f0c2..a4b2bd0b38 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -9,7 +9,7 @@
#endif
/*
- * Memory
+ * Memory wrappers
*/
void *tor_malloc(size_t size) {
@@ -22,17 +22,26 @@ void *tor_malloc(size_t size) {
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 @@ void set_socket_nonblocking(int socket)
* 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 @@ int correct_socket_errno(int s)
/*
* 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 @@ file_status_t file_status(const char *fname)
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;
diff --git a/src/common/util.h b/src/common/util.h
index d1127260c6..40e35d9629 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -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 @@ void set_socket_nonblocking(int socket);
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();