Remove instances of getenv("HOME") and provide a get_home_dir() function
authorgram <gram@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 29 Jan 2000 16:41:28 +0000 (16:41 +0000)
committergram <gram@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 29 Jan 2000 16:41:28 +0000 (16:41 +0000)
which provides a default value if "HOME" is not set.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@1579 f5534014-38df-0310-8fa8-9805f1628bb7

colors.c
gtk/filter_prefs.c
gtk/main.c
plugins.c
prefs.c
resolv.c
util.c
util.h

index ddf2c9d7844954db243e50dce391eaedeccdf879..ae9225466bd9d0aea258c04233c894276661306b 100644 (file)
--- a/colors.c
+++ b/colors.c
@@ -1,7 +1,7 @@
 /* colors.c
  * Definitions for color structures and routines
  *
- * $Id: colors.c,v 1.28 2000/01/03 06:59:07 guy Exp $
+ * $Id: colors.c,v 1.29 2000/01/29 16:41:13 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -43,6 +43,7 @@
 #include "dfilter.h"
 #include "simple_dialog.h"
 #include "ui_util.h"
+#include "util.h"
 
 extern capture_file cf;
 
@@ -228,8 +229,8 @@ read_filters(colfilter *filter)
                return FALSE;
        /* we have a clist */
 
-       path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(fname) +  4);
-       sprintf(path, "%s/%s", getenv("HOME"), fname);
+       path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(fname) +  4);
+       sprintf(path, "%s/%s", get_home_dir(), fname);
 
        if ((f = fopen(path, "r")) == NULL) {
          if (errno != ENOENT) {
@@ -321,8 +322,8 @@ write_filters(colfilter *filter)
        gchar *path;
        gchar *name = PF_DIR "/colorfilters";
        /* decide what file to open (from dfilter code) */
-       path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(name) +  4);
-       sprintf(path, "%s/%s", getenv("HOME"), name);
+       path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(name) +  4);
+       sprintf(path, "%s/%s", get_home_dir(), name);
 
        if ((f = fopen(path, "w+")) == NULL) {
          simple_dialog(ESD_TYPE_WARN, NULL,
index 6ee30aa650466e13fa8f7704df4c414f6bfe2707..0d550489b712b7f66298b8b5ce0cc7dab95ccf63 100644 (file)
@@ -3,7 +3,7 @@
  * (This used to be a notebook page under "Preferences", hence the
  * "prefs" in the file name.)
  *
- * $Id: filter_prefs.c,v 1.7 1999/12/10 07:20:57 guy Exp $
+ * $Id: filter_prefs.c,v 1.8 2000/01/29 16:41:27 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -46,6 +46,7 @@
 #include "filter_prefs.h"
 #include "packet.h"
 #include "file.h"
+#include "util.h"
 #include "prefs_dlg.h"
 
 #define E_FILT_NAME_KEY "filter_name"
@@ -84,8 +85,8 @@ get_filter_list() {
   if (fl) return;
   
   /* To do: generalize this */
-  ff_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(ff_name) +  4);
-  sprintf(ff_path, "%s/%s", getenv("HOME"), ff_name);
+  ff_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(ff_name) +  4);
+  sprintf(ff_path, "%s/%s", get_home_dir(), ff_name);
 
   if ((ff = fopen(ff_path, "r")) == NULL) {
     g_free(ff_path);
@@ -548,9 +549,9 @@ filter_prefs_save(GtkWidget *w) {
   FILE        *ff;
   struct stat  s_buf;
   
-  ff_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(ff_dir) +  
+  ff_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(ff_dir) +  
     strlen(ff_name) + 4);
-  sprintf(ff_path, "%s/%s", getenv("HOME"), ff_dir);
+  sprintf(ff_path, "%s/%s", get_home_dir(), ff_dir);
 
   if (stat(ff_path, &s_buf) != 0)
 #ifdef WIN32
@@ -559,7 +560,7 @@ filter_prefs_save(GtkWidget *w) {
     mkdir(ff_path, 0755);
 #endif
     
-  sprintf(ff_path, "%s/%s/%s", getenv("HOME"), ff_dir, ff_name);
+  sprintf(ff_path, "%s/%s/%s", get_home_dir(), ff_dir, ff_name);
 
   if ((ff = fopen(ff_path, "w")) != NULL) {
     flp = g_list_first(fl);
index 8883ecdb1d808a767282ede4af0edee799352a3a..408fc5b0aac02d8807ea8fe53719ab942bf1748b 100644 (file)
@@ -1,6 +1,6 @@
 /* main.c
  *
- * $Id: main.c,v 1.101 2000/01/29 13:30:08 gram Exp $
+ * $Id: main.c,v 1.102 2000/01/29 16:41:28 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -1355,8 +1355,8 @@ main(int argc, char *argv[])
   else if (cf.snap < MIN_PACKET_SIZE)
     cf.snap = MIN_PACKET_SIZE;
   
-  rc_file = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(RC_FILE) + 4);
-  sprintf(rc_file, "%s/%s", getenv("HOME"), RC_FILE);
+  rc_file = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(RC_FILE) + 4);
+  sprintf(rc_file, "%s/%s", get_home_dir(), RC_FILE);
   gtk_rc_parse(rc_file);
 
   if ((m_r_font = gdk_font_load(medium_font)) == NULL) {
index 67fcbda6e25eab2f26cec2348626e25874d08aac..6d23fada2fe6ade2d323c434764e89f2f1a16b40 100644 (file)
--- a/plugins.c
+++ b/plugins.c
@@ -1,7 +1,7 @@
 /* plugins.c
  * plugin routines
  *
- * $Id: plugins.c,v 1.5 2000/01/15 00:22:34 gram Exp $
+ * $Id: plugins.c,v 1.6 2000/01/29 16:41:14 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -55,6 +55,7 @@
 #endif
 
 #include "globals.h"
+#include "util.h"
 
 
 /* linked list of all plugins */
@@ -249,13 +250,13 @@ save_plugin_status()
     plugin *pt_plug;
 
     if (!plugin_status_file) {
-       plugin_status_file = (gchar *)g_malloc(strlen(getenv("HOME")) + 26);
-       sprintf(plugin_status_file, "%s/%s/plugins.status", getenv("HOME"), PF_DIR);
+       plugin_status_file = (gchar *)g_malloc(strlen(get_home_dir()) + 26);
+       sprintf(plugin_status_file, "%s/%s/plugins.status", get_home_dir(), PF_DIR);
     }
     statusfile=fopen(plugin_status_file, "w");
     if (!statusfile) {
-       pf_path = g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) + 2);
-       sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
+       pf_path = g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) + 2);
+       sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
        #ifdef WIN32
        mkdir(pf_path);
        #else
@@ -346,8 +347,8 @@ plugins_scan_dir(const char *dirname)
 
     if (!plugin_status_file)
     {
-       plugin_status_file = (gchar *)g_malloc(strlen(getenv("HOME")) + 26);
-       sprintf(plugin_status_file, "%s/%s/plugins.status", getenv("HOME"), PF_DIR);
+       plugin_status_file = (gchar *)g_malloc(strlen(get_home_dir()) + 26);
+       sprintf(plugin_status_file, "%s/%s/plugins.status", get_home_dir(), PF_DIR);
     }
     statusfile = fopen(plugin_status_file, "r");
 
@@ -436,8 +437,8 @@ init_plugins()
         }
        if (!user_plug_dir)
        {
-           user_plug_dir = (gchar *)g_malloc(strlen(getenv("HOME")) + 19);
-           sprintf(user_plug_dir, "%s/%s/plugins", getenv("HOME"), PF_DIR);
+           user_plug_dir = (gchar *)g_malloc(strlen(get_home_dir()) + 19);
+           sprintf(user_plug_dir, "%s/%s/plugins", get_home_dir(), PF_DIR);
        }
        plugins_scan_dir(user_plug_dir);
     }
diff --git a/prefs.c b/prefs.c
index 46d7ba40a1f41e15a4ff5058aec25302e5aed300..3150e6882b1eba0d644c3bda63ec510af173ec27 100644 (file)
--- a/prefs.c
+++ b/prefs.c
@@ -1,7 +1,7 @@
 /* prefs.c
  * Routines for handling preferences
  *
- * $Id: prefs.c,v 1.29 2000/01/03 06:29:32 guy Exp $
+ * $Id: prefs.c,v 1.30 2000/01/29 16:41:14 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -51,6 +51,7 @@
 #include "prefs.h"
 #include "column.h"
 #include "print.h"
+#include "util.h"
 
 /* Internal functions */
 static int    set_pref(gchar*, gchar*);
@@ -220,9 +221,9 @@ read_prefs(char **pf_path_return) {
   }
 
   if (! pf_path) {
-    pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
+    pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
       strlen(PF_NAME) + 4);
-    sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
+    sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
   }
     
   *pf_path_return = NULL;
@@ -465,11 +466,11 @@ write_prefs(char **pf_path_return) {
    */
 
   if (! pf_path) {
-    pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
+    pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) +
       strlen(PF_NAME) + 4);
   }
 
-  sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
+  sprintf(pf_path, "%s/%s", get_home_dir(), PF_DIR);
   if (stat(pf_path, &s_buf) != 0)
 #ifdef WIN32
     mkdir(pf_path);
@@ -477,7 +478,7 @@ write_prefs(char **pf_path_return) {
     mkdir(pf_path, 0755);
 #endif
 
-  sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
+  sprintf(pf_path, "%s/%s/%s", get_home_dir(), PF_DIR, PF_NAME);
   if ((pf = fopen(pf_path, "w")) == NULL) {
     *pf_path_return = pf_path;
     return errno;
index 56afa5babff5ca43c72b1ec64e17f37da3326695..add7a14c314a23926d6fce679d0a2fab8cf53c7d 100644 (file)
--- a/resolv.c
+++ b/resolv.c
@@ -1,7 +1,7 @@
 /* resolv.c
  * Routines for network object lookup
  *
- * $Id: resolv.c,v 1.22 2000/01/10 17:32:52 gram Exp $
+ * $Id: resolv.c,v 1.23 2000/01/29 16:41:14 gram Exp $
  *
  * Laurent Deniel <deniel@worldnet.fr>
  *
@@ -78,6 +78,7 @@
 #include "packet-ipx.h"
 #include "globals.h"
 #include "resolv.h"
+#include "util.h"
 
 #define MAXMANUFLEN    9       /* max vendor name length with ending '\0' */
 #define HASHETHSIZE    1024
@@ -592,10 +593,10 @@ static void initialize_ethers(void)
    * with it. It's used in get_ethbyname() and get_ethbyaddr()
    */
   if (g_pethers_path == NULL) {
-    g_pethers_path = g_malloc(strlen(getenv("HOME")) + 
+    g_pethers_path = g_malloc(strlen(get_home_dir()) + 
                              strlen(EPATH_PERSONAL_ETHERS) + 2);
     sprintf(g_pethers_path, "%s/%s", 
-           (char *)getenv("HOME"), EPATH_PERSONAL_ETHERS);
+           get_home_dir(), EPATH_PERSONAL_ETHERS);
   }
 
   /* manuf hash table initialization */
@@ -880,10 +881,10 @@ static void initialize_ipxnets(void)
    * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
    */
   if (g_pipxnets_path == NULL) {
-    g_pipxnets_path = g_malloc(strlen(getenv("HOME")) + 
+    g_pipxnets_path = g_malloc(strlen(get_home_dir()) + 
                              strlen(EPATH_PERSONAL_IPXNETS) + 2);
     sprintf(g_pipxnets_path, "%s/%s", 
-           (char *)getenv("HOME"), EPATH_PERSONAL_IPXNETS);
+           get_home_dir(), EPATH_PERSONAL_IPXNETS);
   }
 
 } /* initialize_ipxnets */
diff --git a/util.c b/util.c
index 0082f523d2808592545ada390e17565df5f4fd69..429741db2f803f65e7cb223068ba039d5247d90f 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,7 +1,7 @@
 /* util.c
  * Utility routines
  *
- * $Id: util.c,v 1.30 2000/01/26 04:56:14 guy Exp $
+ * $Id: util.c,v 1.31 2000/01/29 16:41:15 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -574,4 +574,33 @@ free_interface_list(GList *if_list)
        }
 }
 
+const char*
+get_home_dir(void)
+{
+       char *env_value;
+       static const char *home = NULL;
+#ifdef WIN32
+       static const char *default_home = "C:";
+#else
+       static const char *default_home = "/tmp";
+#endif
+
+       /* Return the cached value, if available */
+       if (home)
+               return home;
+
+       env_value = getenv("HOME");
+
+       if (env_value) {
+               home = env_value;
+       }
+       else {
+               home = default_home;
+       }
+
+       return home;
+}
+
+
+
 #endif /* HAVE_LIBPCAP */
diff --git a/util.h b/util.h
index 6bea55f1167aa84594434ab655bca3f0dbfb43f0..d292e131ee9a9878d34d0dc14f203dbe8fe065f3 100644 (file)
--- a/util.h
+++ b/util.h
@@ -1,7 +1,7 @@
 /* util.h
  * Utility definitions
  *
- * $Id: util.h,v 1.17 2000/01/25 05:48:39 guy Exp $
+ * $Id: util.h,v 1.18 2000/01/29 16:41:15 gram Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -44,6 +44,10 @@ char *get_dirname(char *);
 
 int create_tempfile(char *, int, const char *);
 
+/* Returns the user's home directory, via the HOME environment
+ * variable, or a default directory if HOME is not set */
+const char* get_home_dir(void);
+
 void ASCII_to_EBCDIC(guint8 *buf, guint bytes);
 guint8 ASCII_to_EBCDIC1(guint8 c);
 void EBCDIC_to_ASCII(guint8 *buf, guint bytes);