Have a routine that takes a file name for a personal configuration file

and generates the path name; have it, if the file is to be opened for
reading on Win32, check whether it exists and, if not, check for it in
the old home directory-based configuration directory and, if so, return
that path instead, so that files saved with earlier versions of Ethereal
will be seen.

svn path=/trunk/; revision=4072
This commit is contained in:
Guy Harris 2001-10-24 07:18:39 +00:00
parent d453f6d992
commit 509f30374e
9 changed files with 148 additions and 90 deletions

View File

@ -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;
}

View File

@ -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>
@ -71,11 +71,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
@ -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 */

View File

@ -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);
}
}

View File

@ -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 */

View File

@ -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) {

View File

@ -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.",

View File

@ -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"

View File

@ -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 */

25
prefs.c
View File

@ -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;