aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--epan/filesystem.c111
-rw-r--r--epan/filesystem.h19
-rw-r--r--epan/plugins.c9
-rw-r--r--epan/resolv.c18
-rw-r--r--filters.c21
-rw-r--r--gtk/colors.c24
-rw-r--r--gtk/file_dlg.c4
-rw-r--r--gtk/main.c7
-rw-r--r--prefs.c25
9 files changed, 148 insertions, 90 deletions
diff --git a/epan/filesystem.c b/epan/filesystem.c
index 997b1fd816..be2085f891 100644
--- a/epan/filesystem.c
+++ b/epan/filesystem.c
@@ -1,7 +1,7 @@
/* filesystem.c
* Filesystem utility routines
*
- * $Id: filesystem.c,v 1.13 2001/10/24 06:13:05 guy Exp $
+ * $Id: filesystem.c,v 1.14 2001/10/24 07:18:37 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -311,7 +311,7 @@ get_systemfile_dir(void)
* or, if %APPDATA% isn't set, it's "%USERPROFILE%\Application Data"
* (which is what %APPDATA% normally is on Windows 2000).
*/
-const char *
+static const char *
get_persconffile_dir(void)
{
#ifdef WIN32
@@ -444,3 +444,110 @@ create_persconffile_dir(char **pf_dir_path_return)
*pf_dir_path_return = g_strdup(pf_dir_path);
return ret;
}
+
+#ifdef WIN32
+/*
+ * Returns the user's home directory on Win32.
+ */
+static const char *
+get_home_dir(void)
+{
+ static const char *home = NULL;
+ char *homedrive, *homepath;
+ char *homestring;
+ char *lastsep;
+
+ /* Return the cached value, if available */
+ if (home)
+ return home;
+
+ /*
+ * XXX - should we use USERPROFILE anywhere in this process?
+ * Is there a chance that it might be set but one or more of
+ * HOMEDRIVE or HOMEPATH isn't set?
+ */
+ homedrive = getenv("HOMEDRIVE");
+ if (homedrive != NULL) {
+ homepath = getenv("HOMEPATH");
+ if (homepath != NULL) {
+ /*
+ * This is cached, so we don't need to worry about
+ * allocating multiple ones of them.
+ */
+ homestring =
+ g_malloc(strlen(homedrive) + strlen(homepath) + 1);
+ strcpy(homestring, homedrive);
+ strcat(homestring, homepath);
+
+ /*
+ * Trim off any trailing slash or backslash.
+ */
+ lastsep = find_last_pathname_separator(homestring);
+ if (lastsep != NULL && *(lastsep + 1) == '\0') {
+ /*
+ * Last separator is the last character
+ * in the string. Nuke it.
+ */
+ *lastsep = '\0';
+ }
+ home = homestring;
+ } else
+ home = homedrive;
+ } else {
+ /*
+ * Give up and use C:.
+ */
+ home = "C:";
+ }
+}
+#endif
+
+/*
+ * Construct the path name of a personal configuration file, given the
+ * file name.
+ *
+ * On Win32, if "for_writing" is FALSE, we check whether the file exists
+ * and, if not, construct a path name relative to the ".ethereal"
+ * subdirectory of the user's home directory, and check whether that
+ * exists; if it does, we return that, so that configuration files
+ * from earlier versions can be read.
+ */
+char *
+get_persconffile_path(const char *filename, gboolean for_writing)
+{
+ char *path;
+#ifdef WIN32
+ struct stat s_buf;
+ char *old_path;
+#endif
+
+ path = (gchar *) g_malloc(strlen(get_persconffile_dir()) +
+ strlen(filename) + 2);
+ sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
+ filename);
+#ifdef WIN32
+ if (!for_writing) {
+ if (stat(path, &s_buf) != 0 && errno == ENOENT) {
+ /*
+ * OK, it's not in the personal configuration file
+ * directory; is it in the ".ethereal" subdirectory
+ * of their home directory?
+ */
+ old_path = (gchar *) g_malloc(strlen(get_home_dir()) +
+ strlen(".ethereal") + strlen(filename) + 3);
+ sprintf(old_path,
+ "%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR ".ethereal",
+ get_home_dir(), filename);
+ if (stat(old_path, &s_buf) == 0) {
+ /*
+ * OK, it exists; return it instead.
+ */
+ g_free(path);
+ path = old_path;
+ }
+ }
+ }
+#endif
+
+ return path;
+}
diff --git a/epan/filesystem.h b/epan/filesystem.h
index 0e6a2dbbb4..6ad4078473 100644
--- a/epan/filesystem.h
+++ b/epan/filesystem.h
@@ -1,7 +1,7 @@
/* filesystem.h
* Filesystem utility definitions
*
- * $Id: filesystem.h,v 1.9 2001/10/24 06:13:05 guy Exp $
+ * $Id: filesystem.h,v 1.10 2001/10/24 07:18:37 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -72,11 +72,6 @@ const char *get_datafile_dir(void);
const char *get_systemfile_dir(void);
/*
- * Get the directory in which personal configuration files are stored.
- */
-const char *get_persconffile_dir(void);
-
-/*
* Create the directory that holds personal configuration files, if
* necessary. If we attempted to create it, and failed, return -1 and
* set "*pf_dir_path_return" to the pathname of the directory we failed
@@ -85,4 +80,16 @@ const char *get_persconffile_dir(void);
*/
int create_persconffile_dir(char **pf_dir_path_return);
+/*
+ * Construct the path name of a personal configuration file, given the
+ * file name.
+ *
+ * On Win32, if "for_writing" is FALSE, we check whether the file exists
+ * and, if not, construct a path name relative to the ".ethereal"
+ * subdirectory of the user's home directory, and check whether that
+ * exists; if it does, we return that, so that configuration files
+ * from earlier versions can be read.
+ */
+char *get_persconffile_path(const char *filename, gboolean for_writing);
+
#endif /* FILESYSTEM_H */
diff --git a/epan/plugins.c b/epan/plugins.c
index f1c8ef2229..69ca623e5a 100644
--- a/epan/plugins.c
+++ b/epan/plugins.c
@@ -1,7 +1,7 @@
/* plugins.c
* plugin routines
*
- * $Id: plugins.c,v 1.36 2001/10/22 22:59:25 guy Exp $
+ * $Id: plugins.c,v 1.37 2001/10/24 07:18:37 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -503,12 +503,7 @@ init_plugins(const char *plugin_dir)
}
#endif
if (!user_plug_dir)
- {
- 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);
- }
+ user_plug_dir = get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
plugins_scan_dir(user_plug_dir);
}
}
diff --git a/epan/resolv.c b/epan/resolv.c
index 35861e27f6..287e45bfe3 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.15 2001/10/22 22:59:25 guy Exp $
+ * $Id: resolv.c,v 1.16 2001/10/24 07:18:37 guy Exp $
*
* Laurent Deniel <deniel@worldnet.fr>
*
@@ -637,12 +637,8 @@ static void initialize_ethers(void)
/* Set g_pethers_path here, but don't actually do anything
* with it. It's used in get_ethbyname() and get_ethbyaddr()
*/
- if (g_pethers_path == NULL) {
- 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);
- }
+ if (g_pethers_path == NULL)
+ g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
/* manuf hash table initialization */
@@ -949,12 +945,8 @@ static void initialize_ipxnets(void)
/* Set g_pipxnets_path here, but don't actually do anything
* with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
*/
- if (g_pipxnets_path == NULL) {
- 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);
- }
+ if (g_pipxnets_path == NULL)
+ g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
} /* initialize_ipxnets */
diff --git a/filters.c b/filters.c
index 9433b244ab..e904d8c5a9 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.12 2001/10/23 05:00:57 guy Exp $
+ * $Id: filters.c,v 1.13 2001/10/24 07:18:36 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -81,7 +81,6 @@ void
read_filter_list(filter_list_type_t list, char **pref_path_return,
int *errno_return)
{
- const char *pf_dir_path;
char *ff_path, *ff_name;
FILE *ff;
GList **flp;
@@ -113,10 +112,7 @@ read_filter_list(filter_list_type_t list, char **pref_path_return,
}
/* To do: generalize this */
- 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);
-
+ ff_path = get_persconffile_path(ff_name, FALSE);
if ((ff = fopen(ff_path, "r")) == NULL) {
/*
* Did that fail because we the file didn't exist?
@@ -138,8 +134,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", pf_dir_path,
- FILTER_FILE_NAME);
+ g_free(ff_path);
+ ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
if ((ff = fopen(ff_path, "r")) == NULL) {
/*
* Well, that didn't work, either. Just give up.
@@ -410,9 +406,7 @@ void
save_filter_list(filter_list_type_t list, char **pref_path_return,
int *errno_return)
{
- const char *pf_dir_path;
gchar *ff_path, *ff_path_new, *ff_name;
- int path_length;
GList *fl;
GList *flp;
filter_def *filt;
@@ -438,15 +432,12 @@ save_filter_list(filter_list_type_t list, char **pref_path_return,
return;
}
- pf_dir_path = get_persconffile_dir();
- 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);
+ ff_path = get_persconffile_path(ff_name, TRUE);
/* 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 + 4);
+ ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
sprintf(ff_path_new, "%s.new", ff_path);
if ((ff = fopen(ff_path_new, "w")) == NULL) {
diff --git a/gtk/colors.c b/gtk/colors.c
index cebf124d23..cd1c0da016 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.14 2001/10/24 06:13:06 guy Exp $
+ * $Id: colors.c,v 1.15 2001/10/24 07:18:39 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -162,24 +162,6 @@ 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)
{
@@ -203,7 +185,7 @@ read_filters(colfilter *filter)
return FALSE;
/* we have a clist */
- path = get_colorfilter_file_path();
+ path = get_persconffile_path("colorfilters", FALSE);
if ((f = fopen(path, "r")) == NULL) {
if (errno != ENOENT) {
simple_dialog(ESD_TYPE_CRIT, NULL,
@@ -302,7 +284,7 @@ write_filters(colfilter *filter)
return FALSE;
}
- path = get_colorfilter_file_path();
+ path = get_persconffile_path("colorfilters", TRUE);
if ((f = fopen(path, "w+")) == NULL) {
simple_dialog(ESD_TYPE_CRIT, NULL,
"Could not open\n%s\nfor writing: %s.",
diff --git a/gtk/file_dlg.c b/gtk/file_dlg.c
index 0f4ebdbeab..2b621f2ad9 100644
--- a/gtk/file_dlg.c
+++ b/gtk/file_dlg.c
@@ -1,7 +1,7 @@
/* file_dlg.c
* Dialog boxes for handling files
*
- * $Id: file_dlg.c,v 1.42 2001/09/10 08:49:11 guy Exp $
+ * $Id: file_dlg.c,v 1.43 2001/10/24 07:18:39 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -36,6 +36,8 @@
#include <string.h>
+#include <glib.h>
+
#include <epan/filesystem.h>
#include "globals.h"
diff --git a/gtk/main.c b/gtk/main.c
index e0158cc5a2..1471ed321a 100644
--- a/gtk/main.c
+++ b/gtk/main.c
@@ -1,6 +1,6 @@
/* main.c
*
- * $Id: main.c,v 1.207 2001/10/22 22:59:26 guy Exp $
+ * $Id: main.c,v 1.208 2001/10/24 07:18:39 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -1325,10 +1325,7 @@ main(int argc, char *argv[])
else if (cfile.snap < MIN_PACKET_SIZE)
cfile.snap = MIN_PACKET_SIZE;
- 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);
+ rc_file = get_persconffile_path(RC_FILE, FALSE);
gtk_rc_parse(rc_file);
/* Try to load the regular and boldface fixed-width fonts */
diff --git a/prefs.c b/prefs.c
index 1a7833f5b8..ae81638054 100644
--- a/prefs.c
+++ b/prefs.c
@@ -1,7 +1,7 @@
/* prefs.c
* Routines for handling preferences
*
- * $Id: prefs.c,v 1.67 2001/10/23 05:00:57 guy Exp $
+ * $Id: prefs.c,v 1.68 2001/10/24 07:18:36 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -39,6 +39,8 @@
#include <unistd.h>
#endif
+#include <glib.h>
+
#include <filesystem.h>
#include "globals.h"
#include "packet.h"
@@ -595,23 +597,6 @@ 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
@@ -765,7 +750,7 @@ read_prefs(int *gpf_errno_return, char **gpf_path_return,
}
/* Construct the pathname of the user's preferences file. */
- pf_path = get_preffile_path();
+ pf_path = get_persconffile_path(PF_NAME, FALSE);
/* Read the user's preferences file, if it exists. */
*pf_path_return = NULL;
@@ -1466,7 +1451,7 @@ write_prefs(const char **pf_path_return)
* so that duplication can be avoided with filter.c
*/
- pf_path = get_preffile_path();
+ pf_path = get_persconffile_path(PF_NAME, TRUE);
if ((pf = fopen(pf_path, "w")) == NULL) {
*pf_path_return = pf_path;
return errno;