From e4db9c4b6440c4f34c67f670b0517b7eed269063 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 22 Oct 2001 22:59:26 +0000 Subject: [PATCH] Add a routine to get the directory in which personal configuration files reside. Use it, rather than concatenating the user's home directory and ".ethereal" in a number of files. Fix up some additional places to use G_DIR_SEPARATOR_S as the pathname separator. svn path=/trunk/; revision=4061 --- epan/filesystem.c | 31 +++++++++++++++++++++- epan/filesystem.h | 16 +++++------- epan/plugins.c | 11 ++++---- epan/resolv.c | 18 ++++++------- filters.c | 41 ++++++++++++++--------------- gtk/colors.c | 38 ++++++++++++++++----------- gtk/main.c | 11 +++++--- gtk/main.h | 12 ++++++--- gtk/prefs_dlg.c | 4 +-- prefs.c | 66 +++++++++++++++++++++++++++-------------------- prefs.h | 9 +++---- tethereal.c | 5 ++-- 12 files changed, 154 insertions(+), 108 deletions(-) diff --git a/epan/filesystem.c b/epan/filesystem.c index ead507c535..94343c1459 100644 --- a/epan/filesystem.c +++ b/epan/filesystem.c @@ -1,7 +1,7 @@ /* filesystem.c * Filesystem utility routines * - * $Id: filesystem.c,v 1.7 2001/10/21 21:47:58 guy Exp $ + * $Id: filesystem.c,v 1.8 2001/10/22 22:59:25 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -363,3 +363,32 @@ get_home_dir(void) return home; } + +/* + * Name of directory, under the user's home directory, in which + * personal configuration files are stored. + * + * XXX - should this be ".libepan"? For backwards-compatibility, I'll keep + * it ".ethereal" for now. + */ +#define PF_DIR ".ethereal" + +/* + * Get the directory in which personal configuration files reside; + * it's PF_DIR, under the user's home directory. + */ +const char * +get_persconffile_dir(void) +{ + static char *pf_dir = NULL; + const char *homedir; + + /* Return the cached value, if available */ + if (pf_dir != NULL) + return pf_dir; + + homedir = get_home_dir(); + pf_dir = g_malloc(strlen(homedir) + strlen(PF_DIR) + 2); + sprintf(pf_dir, "%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR); + return pf_dir; +} diff --git a/epan/filesystem.h b/epan/filesystem.h index 37f4d432a8..20c459e6e7 100644 --- a/epan/filesystem.h +++ b/epan/filesystem.h @@ -1,7 +1,7 @@ /* filesystem.h * Filesystem utility definitions * - * $Id: filesystem.h,v 1.5 2001/10/21 21:47:58 guy Exp $ + * $Id: filesystem.h,v 1.6 2001/10/22 22:59:25 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -25,15 +25,6 @@ #ifndef FILESYSTEM_H #define FILESYSTEM_H -/* - * Name of directory, under the user's home directory, in which - * personal configuration files are stored. - * - * XXX - should this be ".libepan"? For backwards-compatibility, I'll keep - * it ".ethereal" for now. - */ -#define PF_DIR ".ethereal" - /* * Given a pathname, return a pointer to the last pathname separator * character in the pathname, or NULL if the pathname contains no @@ -84,4 +75,9 @@ const char *get_systemfile_dir(void); * variable, or a default directory if HOME is not set */ const char* get_home_dir(void); +/* + * Get the directory in which personal configuration files reside. + */ +const char *get_persconffile_dir(void); + #endif /* FILESYSTEM_H */ diff --git a/epan/plugins.c b/epan/plugins.c index 8ad868514c..f1c8ef2229 100644 --- a/epan/plugins.c +++ b/epan/plugins.c @@ -1,7 +1,7 @@ /* plugins.c * plugin routines * - * $Id: plugins.c,v 1.35 2001/10/21 21:47:58 guy Exp $ + * $Id: plugins.c,v 1.36 2001/10/22 22:59:25 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -504,11 +504,10 @@ init_plugins(const char *plugin_dir) #endif if (!user_plug_dir) { - user_plug_dir = (gchar *)g_malloc(strlen(get_home_dir()) + - strlen(PF_DIR) + - strlen(PLUGINS_DIR_NAME) + 3); - sprintf(user_plug_dir, "%s/%s/%s", get_home_dir(), - PF_DIR, PLUGINS_DIR_NAME); + user_plug_dir = (gchar *)g_malloc(strlen(get_persconffile_dir()) + + strlen(PLUGINS_DIR_NAME) + 2); + sprintf(user_plug_dir, "%s" G_DIR_SEPARATOR_S "%s", + get_persconffile_dir(), PLUGINS_DIR_NAME); } plugins_scan_dir(user_plug_dir); } diff --git a/epan/resolv.c b/epan/resolv.c index aa7457e077..35861e27f6 100644 --- a/epan/resolv.c +++ b/epan/resolv.c @@ -1,7 +1,7 @@ /* resolv.c * Routines for network object lookup * - * $Id: resolv.c,v 1.14 2001/10/21 21:47:58 guy Exp $ + * $Id: resolv.c,v 1.15 2001/10/22 22:59:25 guy Exp $ * * Laurent Deniel * @@ -638,10 +638,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(get_home_dir()) + - strlen(PF_DIR) + strlen(ENAME_ETHERS) + 3); - sprintf(g_pethers_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), PF_DIR, ENAME_ETHERS); + g_pethers_path = g_malloc(strlen(get_persconffile_dir()) + + strlen(ENAME_ETHERS) + 2); + sprintf(g_pethers_path, "%s" G_DIR_SEPARATOR_S "%s", + get_persconffile_dir(), ENAME_ETHERS); } /* manuf hash table initialization */ @@ -950,10 +950,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(get_home_dir()) + - strlen(PF_DIR) + strlen(ENAME_IPXNETS) + 3); - sprintf(g_pipxnets_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), PF_DIR, ENAME_IPXNETS); + g_pipxnets_path = g_malloc(strlen(get_persconffile_dir()) + + strlen(ENAME_IPXNETS) + 2); + sprintf(g_pipxnets_path, "%s" G_DIR_SEPARATOR_S "%s", + get_persconffile_dir(), ENAME_IPXNETS); } } /* initialize_ipxnets */ diff --git a/filters.c b/filters.c index b6a10739da..5c115e0b43 100644 --- a/filters.c +++ b/filters.c @@ -1,7 +1,7 @@ /* filters.c * Code for reading and writing the filters file. * - * $Id: filters.c,v 1.10 2001/10/21 21:47:57 guy Exp $ + * $Id: filters.c,v 1.11 2001/10/22 22:59:23 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -89,7 +89,8 @@ void read_filter_list(filter_list_type_t list, char **pref_path_return, int *errno_return) { - char *ff_path, *ff_dir = PF_DIR, *ff_name; + const char *pf_dir_path; + char *ff_path, *ff_name; FILE *ff; GList **flp; GList *fl_ent; @@ -120,10 +121,9 @@ read_filter_list(filter_list_type_t list, char **pref_path_return, } /* To do: generalize this */ - ff_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(ff_dir) + - strlen(ff_name) + 4); - sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), ff_dir, ff_name); + pf_dir_path = get_persconffile_dir(); + ff_path = (gchar *) g_malloc(strlen(pf_dir_path) + strlen(ff_name) + 2); + sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s", pf_dir_path, ff_name); if ((ff = fopen(ff_path, "r")) == NULL) { /* @@ -146,8 +146,8 @@ read_filter_list(filter_list_type_t list, char **pref_path_return, * the filter lists, and delete the ones that don't belong in * a particular list. */ - sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), ff_dir, FILTER_FILE_NAME); + sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s", pf_dir_path, + FILTER_FILE_NAME); if ((ff = fopen(ff_path, "r")) == NULL) { /* * Well, that didn't work, either. Just give up. @@ -418,7 +418,8 @@ void save_filter_list(filter_list_type_t list, char **pref_path_return, int *errno_return) { - gchar *ff_path, *ff_path_new, *ff_dir = PF_DIR, *ff_name; + const char *pf_dir_path; + gchar *ff_path, *ff_path_new, *ff_name; int path_length; GList *fl; GList *flp; @@ -446,27 +447,23 @@ save_filter_list(filter_list_type_t list, char **pref_path_return, return; } - path_length = strlen(get_home_dir()) + strlen(ff_dir) + strlen(ff_name) - + 4 + 4; - ff_path = (gchar *) g_malloc(path_length); - sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s", get_home_dir(), ff_dir); - - if (stat(ff_path, &s_buf) != 0) + pf_dir_path = get_persconffile_dir(); + if (stat(pf_dir_path, &s_buf) != 0) #ifdef WIN32 - mkdir(ff_path); + mkdir(pf_dir_path); #else - mkdir(ff_path, 0755); + mkdir(pf_dir_path, 0755); #endif - sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), ff_dir, ff_name); + path_length = strlen(pf_dir_path) + strlen(ff_name) + 2; + ff_path = (gchar *) g_malloc(path_length); + sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s", pf_dir_path, ff_name); /* Write to "XXX.new", and rename if that succeeds. That means we don't trash the file if we fail to write it out completely. */ - ff_path_new = (gchar *) g_malloc(path_length); - sprintf(ff_path_new, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s.new", - get_home_dir(), ff_dir, ff_name); + ff_path_new = (gchar *) g_malloc(path_length + 4); + sprintf(ff_path_new, "%s.new", ff_path); if ((ff = fopen(ff_path_new, "w")) == NULL) { *pref_path_return = ff_path; diff --git a/gtk/colors.c b/gtk/colors.c index 409d117a1f..4df4a807e9 100644 --- a/gtk/colors.c +++ b/gtk/colors.c @@ -1,7 +1,7 @@ /* colors.c * Definitions for color structures and routines * - * $Id: colors.c,v 1.11 2001/10/21 21:56:05 guy Exp $ + * $Id: colors.c,v 1.12 2001/10/22 22:59:26 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -161,6 +161,24 @@ delete_color_filter(color_filter_t *colorf) g_free(colorf); } +/* + * Get the pathname of the preferences file. + */ +static const char * +get_colorfilter_file_path(void) +{ + static gchar *cf_path = NULL; + static const char fname[] = "colorfilters"; + + if (cf_path == NULL) { + cf_path = (gchar *) g_malloc(strlen(get_persconffile_dir()) + + sizeof fname + 1); + sprintf(cf_path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(), + fname); + } + return cf_path; +} + static gboolean read_filters(colfilter *filter) { @@ -172,9 +190,8 @@ read_filters(colfilter *filter) GdkColor fg_color, bg_color; color_filter_t *colorf; int i; + const gchar *path; FILE *f; - gchar *path; - gchar *fname = PF_DIR G_DIR_SEPARATOR_S "colorfilters"; dfilter_t *temp_dfilter; /* decide what file to open (from dfilter code) */ @@ -185,19 +202,15 @@ read_filters(colfilter *filter) return FALSE; /* we have a clist */ - path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(fname) + 4); - sprintf(path, "%s/%s", get_home_dir(), fname); - + path = get_colorfilter_file_path(); if ((f = fopen(path, "r")) == NULL) { if (errno != ENOENT) { simple_dialog(ESD_TYPE_CRIT, NULL, "Could not open filter file\n\"%s\": %s.", path, strerror(errno)); } - g_free(path); return FALSE; } - g_free(path); i = 0; @@ -274,24 +287,19 @@ write_filter(gpointer filter_arg, gpointer file_arg) gboolean write_filters(colfilter *filter) { + const gchar *path; FILE *f; - gchar *path; - gchar *name = PF_DIR G_DIR_SEPARATOR_S "colorfilters"; - /* decide what file to open (from dfilter code) */ - path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(name) + 4); - sprintf(path, "%s/%s", get_home_dir(), name); + path = get_colorfilter_file_path(); if ((f = fopen(path, "w+")) == NULL) { simple_dialog(ESD_TYPE_CRIT, NULL, "Could not open\n%s\nfor writing: %s.", path, strerror(errno)); - g_free(path); return FALSE; } fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n"); g_slist_foreach(filter_list, write_filter, f); fclose(f); - g_free(path); return TRUE; } diff --git a/gtk/main.c b/gtk/main.c index 8a9ed56f17..e0158cc5a2 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -1,6 +1,6 @@ /* main.c * - * $Id: main.c,v 1.206 2001/10/21 16:15:19 gerald Exp $ + * $Id: main.c,v 1.207 2001/10/22 22:59:26 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -866,7 +866,8 @@ main(int argc, char *argv[]) WSADATA wsaData; #endif - char *gpf_path, *pf_path, *cf_path, *df_path; + char *gpf_path, *cf_path, *df_path; + const char *pf_path; int gpf_open_errno, pf_open_errno, cf_open_errno, df_open_errno; int err; #ifdef HAVE_LIBPCAP @@ -1324,8 +1325,10 @@ main(int argc, char *argv[]) else if (cfile.snap < MIN_PACKET_SIZE) cfile.snap = MIN_PACKET_SIZE; - rc_file = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(RC_FILE) + 4); - sprintf(rc_file, "%s/%s", get_home_dir(), RC_FILE); + rc_file = (gchar *) g_malloc(strlen(get_persconffile_dir()) + + strlen(RC_FILE) + 2); + sprintf(rc_file, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(), + RC_FILE); gtk_rc_parse(rc_file); /* Try to load the regular and boldface fixed-width fonts */ diff --git a/gtk/main.h b/gtk/main.h index d9c845b177..f3e5aa8dbd 100644 --- a/gtk/main.h +++ b/gtk/main.h @@ -1,12 +1,11 @@ /* main.h * Global defines, etc. * - * $Id: main.h,v 1.22 2001/04/09 22:35:23 gram Exp $ + * $Id: main.h,v 1.23 2001/10/22 22:59:26 guy Exp $ * * Ethereal - Network traffic analyzer - * By Gerald Combs + * By Gerald Combs * Copyright 1998 Gerald Combs - * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -32,7 +31,12 @@ #include "globals.h" -#define RC_FILE PF_DIR "/gtkrc" +/* + * File under personal preferences directory in which GTK settings for + * Ethereal are stored. + */ +#define RC_FILE "gtkrc" + #define DEF_WIDTH 750 #define DEF_HEIGHT 550 #ifdef HAVE_LIBPCAP diff --git a/gtk/prefs_dlg.c b/gtk/prefs_dlg.c index eb9cad9070..38c91f1f82 100644 --- a/gtk/prefs_dlg.c +++ b/gtk/prefs_dlg.c @@ -1,7 +1,7 @@ /* prefs_dlg.c * Routines for handling preferences * - * $Id: prefs_dlg.c,v 1.29 2001/10/13 12:05:32 sharpe Exp $ + * $Id: prefs_dlg.c,v 1.30 2001/10/22 22:59:26 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -643,7 +643,7 @@ prefs_main_save_cb(GtkWidget *save_bt, gpointer parent_w) { gboolean must_redissect = FALSE; int err; - char *pf_path; + const char *pf_path; /* Fetch the preferences (i.e., make sure all the values set in all of the preferences panes have been copied to "prefs" and the registered diff --git a/prefs.c b/prefs.c index 6b927ce1bc..7e0badd020 100644 --- a/prefs.c +++ b/prefs.c @@ -1,7 +1,7 @@ /* prefs.c * Routines for handling preferences * - * $Id: prefs.c,v 1.65 2001/10/21 21:47:57 guy Exp $ + * $Id: prefs.c,v 1.66 2001/10/22 22:59:23 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -70,7 +70,6 @@ static void free_col_info(e_prefs *); static gboolean init_prefs = TRUE; static gchar *gpf_path = NULL; -static gchar *pf_path = NULL; /* * XXX - variables to allow us to attempt to interpret the first @@ -604,6 +603,23 @@ print.file: /a/very/long/path/ static void read_prefs_file(const char *pf_path, FILE *pf); +/* + * Get the pathname of the preferences file. + */ +static const char * +get_preffile_path(void) +{ + static gchar *pf_path = NULL; + + if (pf_path == NULL) { + pf_path = (gchar *) g_malloc(strlen(get_persconffile_dir()) + + strlen(PF_NAME) + 2); + sprintf(pf_path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(), + PF_NAME); + } + return pf_path; +} + /* Read the preferences file, fill in "prefs", and return a pointer to it. If we got an error (other than "it doesn't exist") trying to read @@ -617,16 +633,16 @@ static void read_prefs_file(const char *pf_path, FILE *pf); return NULL. */ e_prefs * read_prefs(int *gpf_errno_return, char **gpf_path_return, - int *pf_errno_return, char **pf_path_return) + int *pf_errno_return, const char **pf_path_return) { - int i; - FILE *pf; - fmt_data *cfmt; - gchar *col_fmt[] = {"No.", "%m", "Time", "%t", - "Source", "%s", "Destination", "%d", - "Protocol", "%p", "Info", "%i"}; + int i; + const char *pf_path; + FILE *pf; + fmt_data *cfmt; + gchar *col_fmt[] = {"No.", "%m", "Time", "%t", + "Source", "%s", "Destination", "%d", + "Protocol", "%p", "Info", "%i"}; - if (init_prefs) { /* Initialize preferences to wired-in default values. They may be overridded by the global preferences file or the @@ -757,12 +773,7 @@ read_prefs(int *gpf_errno_return, char **gpf_path_return, } /* Construct the pathname of the user's preferences file. */ - if (! pf_path) { - pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) + - strlen(PF_NAME) + 4); - sprintf(pf_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), PF_DIR, PF_NAME); - } + pf_path = get_preffile_path(); /* Read the user's preferences file, if it exists. */ *pf_path_return = NULL; @@ -1450,8 +1461,10 @@ write_module_prefs(gpointer data, gpointer user_data) If we got an error, stuff a pointer to the path of the preferences file into "*pf_path_return", and return the errno. */ int -write_prefs(char **pf_path_return) +write_prefs(const char **pf_path_return) { + const char *pf_dir_path; + const char *pf_path; FILE *pf; struct stat s_buf; GList *clp, *col_l; @@ -1463,21 +1476,18 @@ write_prefs(char **pf_path_return) * so that duplication can be avoided with filter.c */ - if (! pf_path) { - pf_path = (gchar *) g_malloc(strlen(get_home_dir()) + strlen(PF_DIR) + - strlen(PF_NAME) + 4); - } - - sprintf(pf_path, "%s" G_DIR_SEPARATOR_S "%s", get_home_dir(), PF_DIR); - if (stat(pf_path, &s_buf) != 0) + /* + * Create the directory that holds personal configuration files. + */ + pf_dir_path = get_persconffile_dir(); + if (stat(pf_dir_path, &s_buf) != 0) #ifdef WIN32 - mkdir(pf_path); + mkdir(pf_dir_path); #else - mkdir(pf_path, 0755); + mkdir(pf_dir_path, 0755); #endif - sprintf(pf_path, "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s", - get_home_dir(), PF_DIR, PF_NAME); + pf_path = get_preffile_path(); if ((pf = fopen(pf_path, "w")) == NULL) { *pf_path_return = pf_path; return errno; diff --git a/prefs.h b/prefs.h index da6289f019..bca7c7664e 100644 --- a/prefs.h +++ b/prefs.h @@ -1,12 +1,11 @@ /* prefs.h * Definitions for preference handling routines * - * $Id: prefs.h,v 1.31 2001/07/22 21:25:11 guy Exp $ + * $Id: prefs.h,v 1.32 2001/10/22 22:59:23 guy Exp $ * * Ethereal - Network traffic analyzer - * By Gerald Combs + * By Gerald Combs * Copyright 1998 Gerald Combs - * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -186,13 +185,13 @@ void prefs_register_modules(void); the user's preferences file, stuff the errno into "*pf_errno_return" and a pointer to the path of the file into "*pf_path_return", and return NULL. */ -e_prefs *read_prefs(int *, char **, int *, char **); +e_prefs *read_prefs(int *, char **, int *, const char **); /* Write out "prefs" to the user's preferences file, and return 0. If we got an error, stuff a pointer to the path of the preferences file into "*pf_path_return", and return the errno. */ -int write_prefs(char **); +int write_prefs(const char **); /* Copy a set of preferences. */ void copy_prefs(e_prefs *dest, e_prefs *src); diff --git a/tethereal.c b/tethereal.c index 5d94bc22a5..d5287f5aa1 100644 --- a/tethereal.c +++ b/tethereal.c @@ -1,6 +1,6 @@ /* tethereal.c * - * $Id: tethereal.c,v 1.92 2001/10/04 08:30:33 guy Exp $ + * $Id: tethereal.c,v 1.93 2001/10/22 22:59:23 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -214,7 +214,8 @@ main(int argc, char *argv[]) WSADATA wsaData; #endif - char *gpf_path, *pf_path; + char *gpf_path; + const char *pf_path; int gpf_open_errno, pf_open_errno; int err; #ifdef HAVE_LIBPCAP