[or-cvs] Add generic expand_filename function to handle expanding ~/

Nick Mathewson nickm at seul.org
Thu Jul 15 02:00:45 UTC 2004


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv31503/src/common

Modified Files:
	util.c util.h 
Log Message:
Add generic expand_filename function to handle expanding ~/

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.111
retrieving revision 1.112
diff -u -d -r1.111 -r1.112
--- util.c	13 Jul 2004 16:57:37 -0000	1.111
+++ util.c	15 Jul 2004 02:00:43 -0000	1.112
@@ -1453,6 +1453,27 @@
   return 1;
 }
 
+/** Expand any homedir prefix on 'filename'; return a newly allocated
+ * string. */
+char *expand_filename(const char *filename)
+{
+  tor_assert(filename);
+  if (!strncmp(filename,"~/",2)) {
+    const char *home = getenv("HOME");
+    char *result;
+    if (!home) {
+      log_fn(LOG_ERR, "Couldn't find $HOME environment variable while expanding %s", filename);
+      return NULL;
+    }
+    /* minus two characters for ~/, plus one for /, plus one for NUL. */
+    result = tor_malloc(strlen(home)+strlen(filename)+16);
+    sprintf(result,"%s/%s",home,filename+2);
+    return result;
+  } else {
+    return tor_strdup(filename);
+  }
+}
+
 /** Return true iff <b>ip</b> (in host order) is an IP reserved to localhost,
  * or reserved for local networks by RFC 1918.
  */

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -d -r1.73 -r1.74
--- util.h	13 Jul 2004 16:57:37 -0000	1.73
+++ util.h	15 Jul 2004 02:00:43 -0000	1.74
@@ -217,6 +217,7 @@
 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);
+char *expand_filename(const char *filename);
 
 int spawn_func(int (*func)(void *), void *data);
 void spawn_exit();



More information about the tor-commits mailing list