From ea8136cd8e70da00e46ae281f14e089bd920919c Mon Sep 17 00:00:00 2001 From: Gilbert Ramirez Date: Sat, 29 Jan 2000 16:41:28 +0000 Subject: Remove instances of getenv("HOME") and provide a get_home_dir() function which provides a default value if "HOME" is not set. svn path=/trunk/; revision=1579 --- colors.c | 11 ++++++----- gtk/filter_prefs.c | 13 +++++++------ gtk/main.c | 6 +++--- plugins.c | 19 ++++++++++--------- prefs.c | 13 +++++++------ resolv.c | 11 ++++++----- util.c | 31 ++++++++++++++++++++++++++++++- util.h | 6 +++++- 8 files changed, 74 insertions(+), 36 deletions(-) diff --git a/colors.c b/colors.c index ddf2c9d784..ae9225466b 100644 --- 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 @@ -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, diff --git a/gtk/filter_prefs.c b/gtk/filter_prefs.c index 6ee30aa650..0d550489b7 100644 --- a/gtk/filter_prefs.c +++ b/gtk/filter_prefs.c @@ -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 @@ -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); diff --git a/gtk/main.c b/gtk/main.c index 8883ecdb1d..408fc5b0aa 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -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 @@ -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) { diff --git a/plugins.c b/plugins.c index 67fcbda6e2..6d23fada2f 100644 --- 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 @@ -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 46d7ba40a1..3150e6882b 100644 --- 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 @@ -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; diff --git a/resolv.c b/resolv.c index 56afa5babf..add7a14c31 100644 --- 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 * @@ -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 0082f523d2..429741db2f 100644 --- 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 @@ -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 6bea55f116..d292e131ee 100644 --- 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 @@ -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); -- cgit v1.2.3