2000-09-28 03:16:29 +00:00
|
|
|
/* filesystem.c
|
|
|
|
* Filesystem utility routines
|
|
|
|
*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
2000-09-28 03:16:29 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2000-09-28 03:16:29 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2000-09-28 03:16:29 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2000-09-28 03:16:29 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2000-09-28 03:16:29 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-10-23 03:40:39 +00:00
|
|
|
#include <stdio.h>
|
2000-09-28 03:16:29 +00:00
|
|
|
#include <stdlib.h>
|
2000-10-11 07:35:02 +00:00
|
|
|
#include <string.h>
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2000-09-28 03:16:29 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-07 02:47:09 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2006-03-08 22:13:21 +00:00
|
|
|
#ifdef _WIN32
|
2001-08-21 08:16:54 +00:00
|
|
|
#include <windows.h>
|
2006-03-08 20:55:32 +00:00
|
|
|
#include <tchar.h>
|
|
|
|
#include "epan/strutil.h"
|
2006-03-08 22:13:21 +00:00
|
|
|
#else
|
|
|
|
#include <pwd.h>
|
2000-09-28 03:16:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "filesystem.h"
|
2005-11-07 02:47:09 +00:00
|
|
|
#include <wiretap/file_util.h>
|
2000-09-28 03:16:29 +00:00
|
|
|
|
2001-04-02 09:53:46 +00:00
|
|
|
/*
|
|
|
|
* Given a pathname, return a pointer to the last pathname separator
|
|
|
|
* character in the pathname, or NULL if the pathname contains no
|
|
|
|
* separators.
|
|
|
|
*/
|
"gtk_entry_get_text()" returns a "const char *" - assign the result to
one.
"get_basename()" doesn't modify its argument, and its callers don't
modify the substring pointed to by the result, so make it take a "const
char *" as an argument and return a "const char *".
"find_last_pathname_separator()" doesn't modify its argument, so make it
a "const char *" - but some of its callers pass a non-"const" "char *"
and modify the result, so don't make its return value a "const char *".
And, as none of its callers are outside "filesystem.c", make it static.
In "about_folders_page_new()", have separate variables for pathnames
returned as "const char *" (which are cached by the routine that returns
them, so you can't modify them - and can't free them, so get rid of the
commented-out "g_free()" calls for them) and pathnames returned as "char
*" (which are allocated anew for each call, and can be modified, but
have to be freed).
Clean up white space.
svn path=/trunk/; revision=12881
2004-12-31 00:26:36 +00:00
|
|
|
static char *
|
|
|
|
find_last_pathname_separator(const char *path)
|
2001-04-02 09:53:46 +00:00
|
|
|
{
|
|
|
|
char *separator;
|
|
|
|
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-04-02 09:53:46 +00:00
|
|
|
char c;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to scan for '\' or '/'.
|
|
|
|
* Get to the end of the string.
|
|
|
|
*/
|
"gtk_entry_get_text()" returns a "const char *" - assign the result to
one.
"get_basename()" doesn't modify its argument, and its callers don't
modify the substring pointed to by the result, so make it take a "const
char *" as an argument and return a "const char *".
"find_last_pathname_separator()" doesn't modify its argument, so make it
a "const char *" - but some of its callers pass a non-"const" "char *"
and modify the result, so don't make its return value a "const char *".
And, as none of its callers are outside "filesystem.c", make it static.
In "about_folders_page_new()", have separate variables for pathnames
returned as "const char *" (which are cached by the routine that returns
them, so you can't modify them - and can't free them, so get rid of the
commented-out "g_free()" calls for them) and pathnames returned as "char
*" (which are allocated anew for each call, and can be modified, but
have to be freed).
Clean up white space.
svn path=/trunk/; revision=12881
2004-12-31 00:26:36 +00:00
|
|
|
separator = strchr(path, '\0'); /* points to ending '\0' */
|
2001-04-02 09:53:46 +00:00
|
|
|
while (separator > path) {
|
|
|
|
c = *--separator;
|
|
|
|
if (c == '\\' || c == '/')
|
|
|
|
return separator; /* found it */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, we didn't find any, so no directories - but there might
|
|
|
|
* be a drive letter....
|
|
|
|
*/
|
|
|
|
return strchr(path, ':');
|
|
|
|
#else
|
|
|
|
separator = strrchr(path, '/');
|
|
|
|
#endif
|
|
|
|
return separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pathname, return the last component.
|
|
|
|
*/
|
"gtk_entry_get_text()" returns a "const char *" - assign the result to
one.
"get_basename()" doesn't modify its argument, and its callers don't
modify the substring pointed to by the result, so make it take a "const
char *" as an argument and return a "const char *".
"find_last_pathname_separator()" doesn't modify its argument, so make it
a "const char *" - but some of its callers pass a non-"const" "char *"
and modify the result, so don't make its return value a "const char *".
And, as none of its callers are outside "filesystem.c", make it static.
In "about_folders_page_new()", have separate variables for pathnames
returned as "const char *" (which are cached by the routine that returns
them, so you can't modify them - and can't free them, so get rid of the
commented-out "g_free()" calls for them) and pathnames returned as "char
*" (which are allocated anew for each call, and can be modified, but
have to be freed).
Clean up white space.
svn path=/trunk/; revision=12881
2004-12-31 00:26:36 +00:00
|
|
|
const char *
|
|
|
|
get_basename(const char *path)
|
2001-04-02 09:53:46 +00:00
|
|
|
{
|
"gtk_entry_get_text()" returns a "const char *" - assign the result to
one.
"get_basename()" doesn't modify its argument, and its callers don't
modify the substring pointed to by the result, so make it take a "const
char *" as an argument and return a "const char *".
"find_last_pathname_separator()" doesn't modify its argument, so make it
a "const char *" - but some of its callers pass a non-"const" "char *"
and modify the result, so don't make its return value a "const char *".
And, as none of its callers are outside "filesystem.c", make it static.
In "about_folders_page_new()", have separate variables for pathnames
returned as "const char *" (which are cached by the routine that returns
them, so you can't modify them - and can't free them, so get rid of the
commented-out "g_free()" calls for them) and pathnames returned as "char
*" (which are allocated anew for each call, and can be modified, but
have to be freed).
Clean up white space.
svn path=/trunk/; revision=12881
2004-12-31 00:26:36 +00:00
|
|
|
const char *filename;
|
2001-04-02 09:53:46 +00:00
|
|
|
|
2003-09-15 19:05:01 +00:00
|
|
|
g_assert(path != NULL);
|
2001-04-02 09:53:46 +00:00
|
|
|
filename = find_last_pathname_separator(path);
|
|
|
|
if (filename == NULL) {
|
|
|
|
/*
|
|
|
|
* There're no directories, drive letters, etc. in the
|
|
|
|
* name; the pathname *is* the file name.
|
|
|
|
*/
|
|
|
|
filename = path;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Skip past the pathname or drive letter separator.
|
|
|
|
*/
|
|
|
|
filename++;
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pathname, return a string containing everything but the
|
|
|
|
* last component. NOTE: this overwrites the pathname handed into
|
|
|
|
* it....
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
get_dirname(char *path)
|
|
|
|
{
|
|
|
|
char *separator;
|
|
|
|
|
2003-09-15 19:05:01 +00:00
|
|
|
g_assert(path != NULL);
|
2001-04-02 09:53:46 +00:00
|
|
|
separator = find_last_pathname_separator(path);
|
|
|
|
if (separator == NULL) {
|
|
|
|
/*
|
|
|
|
* There're no directories, drive letters, etc. in the
|
|
|
|
* name; there is no directory path to return.
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get rid of the last pathname separator and the final file
|
|
|
|
* name following it.
|
|
|
|
*/
|
|
|
|
*separator = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "path" now contains the pathname of the directory containing
|
|
|
|
* the file/directory to which it referred.
|
|
|
|
*/
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
/*
|
|
|
|
* Given a pathname, return:
|
|
|
|
*
|
|
|
|
* the errno, if an attempt to "stat()" the file fails;
|
|
|
|
*
|
|
|
|
* EISDIR, if the attempt succeeded and the file turned out
|
|
|
|
* to be a directory;
|
|
|
|
*
|
|
|
|
* 0, if the attempt succeeded and the file turned out not
|
|
|
|
* to be a directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
|
|
|
|
* define them either.)
|
|
|
|
*
|
|
|
|
* Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
|
|
|
|
*/
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
#ifndef S_IFIFO
|
|
|
|
#define S_IFIFO _S_IFIFO
|
|
|
|
#endif
|
|
|
|
#ifndef S_ISFIFO
|
|
|
|
#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
|
|
|
|
#endif
|
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
test_for_directory(const char *path)
|
|
|
|
{
|
|
|
|
struct stat statb;
|
|
|
|
|
2005-11-06 22:43:25 +00:00
|
|
|
if (eth_stat(path, &statb) < 0)
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
return errno;
|
|
|
|
|
|
|
|
if (S_ISDIR(statb.st_mode))
|
|
|
|
return EISDIR;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-23 21:33:09 +00:00
|
|
|
int
|
|
|
|
test_for_fifo(const char *path)
|
|
|
|
{
|
|
|
|
struct stat statb;
|
|
|
|
|
2005-11-06 22:43:25 +00:00
|
|
|
if (eth_stat(path, &statb) < 0)
|
2002-06-23 21:33:09 +00:00
|
|
|
return errno;
|
|
|
|
|
|
|
|
if (S_ISFIFO(statb.st_mode))
|
|
|
|
return ESPIPE;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
static char *progfile_dir;
|
|
|
|
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
/*
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
* Get the pathname of the directory from which the executable came,
|
2006-05-01 06:51:14 +00:00
|
|
|
* and save it for future use. Returns NULL on success, and a
|
|
|
|
* g_mallocated string containing an error on failure.
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
*/
|
2006-05-01 06:51:14 +00:00
|
|
|
char *
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
init_progfile_dir(const char *arg0
|
|
|
|
#ifdef _WIN32
|
|
|
|
_U_
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char *dir_end;
|
|
|
|
char *path;
|
|
|
|
#ifdef _WIN32
|
2006-03-08 20:55:32 +00:00
|
|
|
TCHAR prog_pathname_w[_MAX_PATH+2];
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
size_t progfile_dir_len;
|
2006-03-08 20:55:32 +00:00
|
|
|
char *prog_pathname;
|
2006-05-01 08:09:39 +00:00
|
|
|
DWORD error;
|
2006-05-01 06:51:14 +00:00
|
|
|
TCHAR *msg_w;
|
|
|
|
guchar *msg;
|
|
|
|
size_t msglen;
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to get the full pathname of the currently running
|
|
|
|
* program.
|
|
|
|
*/
|
2006-03-08 20:55:32 +00:00
|
|
|
if (GetModuleFileName(NULL, prog_pathname_w, sizeof prog_pathname_w) != 0) {
|
|
|
|
/*
|
|
|
|
* XXX - Should we use g_utf16_to_utf8(), as in
|
|
|
|
* getenv_utf8()?
|
|
|
|
*/
|
|
|
|
prog_pathname = utf_16to8(prog_pathname_w);
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
/*
|
|
|
|
* We got it; strip off the last component, which would be
|
|
|
|
* the file name of the executable, giving us the pathname
|
|
|
|
* of the directory where the executable resies
|
|
|
|
*
|
2006-05-01 06:51:14 +00:00
|
|
|
* First, find the last "\" in the directory, as that
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
* marks the end of the directory pathname.
|
|
|
|
*
|
|
|
|
* XXX - Can the pathname be something such as
|
2006-05-28 19:49:07 +00:00
|
|
|
* "C:wireshark.exe"? Or is it always a full pathname
|
2006-05-01 06:51:14 +00:00
|
|
|
* beginning with "\" after the drive letter?
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
*/
|
|
|
|
dir_end = strrchr(prog_pathname, '\\');
|
|
|
|
if (dir_end != NULL) {
|
|
|
|
/*
|
|
|
|
* Found it - now figure out how long the program
|
|
|
|
* directory pathname will be.
|
|
|
|
*/
|
|
|
|
progfile_dir_len = (dir_end - prog_pathname);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a buffer for the program directory
|
|
|
|
* pathname, and construct it.
|
|
|
|
*/
|
|
|
|
path = g_malloc(progfile_dir_len + 1);
|
|
|
|
strncpy(path, prog_pathname, progfile_dir_len);
|
|
|
|
path[progfile_dir_len] = '\0';
|
|
|
|
progfile_dir = path;
|
2006-05-01 06:51:14 +00:00
|
|
|
|
|
|
|
return NULL; /* we succeeded */
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* OK, no \ - what do we do now?
|
|
|
|
*/
|
2006-05-01 08:09:39 +00:00
|
|
|
return g_strdup_printf("No \\ in executable pathname \"%s\"",
|
2006-05-01 06:51:14 +00:00
|
|
|
prog_pathname);
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
2006-05-01 06:51:14 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Oh, well. Return an indication of the error.
|
|
|
|
*/
|
|
|
|
error = GetLastError();
|
|
|
|
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, error, 0, (LPTSTR) &msg_w, 0, NULL) == 0) {
|
|
|
|
/*
|
|
|
|
* Gak. We can't format the message.
|
|
|
|
*/
|
2006-05-01 08:09:39 +00:00
|
|
|
return g_strdup_printf("GetModuleFileName failed: %u (FormatMessage failed: %u)",
|
2006-05-01 06:51:14 +00:00
|
|
|
error, GetLastError());
|
|
|
|
}
|
|
|
|
msg = utf_16to8(msg_w);
|
|
|
|
LocalFree(msg_w);
|
|
|
|
/*
|
|
|
|
* "FormatMessage()" "helpfully" sticks CR/LF at the
|
|
|
|
* end of the message. Get rid of it.
|
|
|
|
*/
|
|
|
|
msglen = strlen(msg);
|
|
|
|
if (msglen >= 2) {
|
|
|
|
msg[msglen - 1] = '\0';
|
|
|
|
msg[msglen - 2] = '\0';
|
|
|
|
}
|
2006-05-01 08:09:39 +00:00
|
|
|
return g_strdup_printf("GetModuleFileName failed: %s (%u)",
|
2006-05-01 06:51:14 +00:00
|
|
|
msg, error);
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
char *prog_pathname;
|
|
|
|
char *curdir;
|
|
|
|
long path_max;
|
2006-05-01 06:51:14 +00:00
|
|
|
char *pathstr;
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
char *path_start, *path_end;
|
|
|
|
size_t path_component_len;
|
2006-05-01 06:51:14 +00:00
|
|
|
char *retstr;
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to figure out the directory in which the currently running
|
|
|
|
* program resides, given the argv[0] it was started with. That
|
|
|
|
* might be the absolute path of the program, or a path relative
|
|
|
|
* to the current directory of the process that started it, or
|
|
|
|
* just a name for the program if it was started from the command
|
|
|
|
* line and was searched for in $PATH. It's not guaranteed to be
|
|
|
|
* any of those, however, so there are no guarantees....
|
|
|
|
*/
|
|
|
|
if (arg0[0] == '/') {
|
|
|
|
/*
|
|
|
|
* It's an absolute path.
|
|
|
|
*/
|
|
|
|
prog_pathname = g_strdup(arg0);
|
|
|
|
} else if (strchr(arg0, '/') != NULL) {
|
|
|
|
/*
|
|
|
|
* It's a relative path, with a directory in it.
|
|
|
|
* Get the current directory, and combine it
|
|
|
|
* with that directory.
|
|
|
|
*/
|
|
|
|
path_max = pathconf(".", _PC_PATH_MAX);
|
|
|
|
if (path_max == -1) {
|
|
|
|
/*
|
|
|
|
* We have no idea how big a buffer to
|
|
|
|
* allocate for the current directory.
|
|
|
|
*/
|
2006-05-01 06:51:14 +00:00
|
|
|
return g_strdup_printf("pathconf failed: %s\n",
|
|
|
|
strerror(errno));
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
|
|
|
curdir = g_malloc(path_max);
|
2006-05-18 12:01:33 +00:00
|
|
|
if (getcwd(curdir, path_max) == NULL) {
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
/*
|
|
|
|
* It failed - give up, and just stick
|
|
|
|
* with DATAFILE_DIR.
|
|
|
|
*/
|
|
|
|
g_free(curdir);
|
2006-05-01 06:51:14 +00:00
|
|
|
return g_strdup_printf("getcwd failed: %s\n",
|
|
|
|
strerror(errno));
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
|
|
|
path = g_malloc(strlen(curdir) + 1 + strlen(arg0) + 1);
|
|
|
|
strcpy(path, curdir);
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, arg0);
|
|
|
|
g_free(curdir);
|
|
|
|
prog_pathname = path;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It's just a file name.
|
|
|
|
* Search the path for a file with that name
|
|
|
|
* that's executable.
|
|
|
|
*/
|
|
|
|
prog_pathname = NULL; /* haven't found it yet */
|
2006-05-01 06:51:14 +00:00
|
|
|
pathstr = getenv("PATH");
|
|
|
|
path_start = pathstr;
|
2006-02-14 09:08:12 +00:00
|
|
|
if (path_start != NULL) {
|
|
|
|
while (*path_start != '\0') {
|
|
|
|
path_end = strchr(path_start, ':');
|
|
|
|
if (path_end == NULL)
|
|
|
|
path_end = path_start + strlen(path_start);
|
|
|
|
path_component_len = path_end - path_start;
|
|
|
|
path = g_malloc(path_component_len + 1
|
|
|
|
+ strlen(arg0) + 1);
|
|
|
|
memcpy(path, path_start, path_component_len);
|
|
|
|
path[path_component_len] = '\0';
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, arg0);
|
|
|
|
if (access(path, X_OK) == 0) {
|
|
|
|
/*
|
|
|
|
* Found it!
|
|
|
|
*/
|
|
|
|
prog_pathname = path;
|
|
|
|
break;
|
|
|
|
}
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
|
|
|
|
/*
|
2006-02-14 09:08:12 +00:00
|
|
|
* That's not it. If there are more
|
|
|
|
* path components to test, try them.
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
*/
|
2006-02-14 09:08:12 +00:00
|
|
|
if (*path_end == '\0') {
|
|
|
|
/*
|
|
|
|
* There's nothing more to try.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*path_end == ':')
|
|
|
|
path_end++;
|
|
|
|
path_start = path_end;
|
|
|
|
g_free(path);
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
2006-05-01 06:51:14 +00:00
|
|
|
if (prog_pathname == NULL) {
|
|
|
|
/*
|
|
|
|
* Program not found in path.
|
|
|
|
*/
|
|
|
|
return g_strdup_printf("\"%s\" not found in \"%s\"",
|
|
|
|
arg0, pathstr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* PATH isn't set.
|
|
|
|
* XXX - should we pick a default?
|
|
|
|
*/
|
|
|
|
return g_strdup("PATH isn't set");
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-01 06:51:14 +00:00
|
|
|
/*
|
|
|
|
* OK, we have what we think is the pathname
|
|
|
|
* of the program.
|
|
|
|
*
|
|
|
|
* First, find the last "/" in the directory,
|
|
|
|
* as that marks the end of the directory pathname.
|
|
|
|
*/
|
|
|
|
dir_end = strrchr(prog_pathname, '/');
|
|
|
|
if (dir_end != NULL) {
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
/*
|
2006-05-01 06:51:14 +00:00
|
|
|
* Found it. Strip off the last component,
|
|
|
|
* as that's the path of the program.
|
|
|
|
*/
|
|
|
|
*dir_end = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is there a "/.libs" at the end?
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
*/
|
|
|
|
dir_end = strrchr(prog_pathname, '/');
|
|
|
|
if (dir_end != NULL) {
|
2006-05-01 06:51:14 +00:00
|
|
|
if (strcmp(dir_end, "/.libs") == 0) {
|
|
|
|
/*
|
|
|
|
* Yup, it's ".libs".
|
|
|
|
* Strip that off; it's an
|
|
|
|
* artifact of libtool.
|
|
|
|
*/
|
|
|
|
*dir_end = '\0';
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-01 06:51:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, we have the path we want.
|
|
|
|
*/
|
|
|
|
progfile_dir = prog_pathname;
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This "shouldn't happen"; we apparently
|
|
|
|
* have no "/" in the pathname.
|
|
|
|
* Just free up prog_pathname.
|
|
|
|
*/
|
|
|
|
retstr = g_strdup_printf("No / found in \"%s\"", prog_pathname);
|
|
|
|
g_free(prog_pathname);
|
|
|
|
return retstr;
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the directory in which the program resides.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
get_progfile_dir(void)
|
|
|
|
{
|
|
|
|
return progfile_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the directory in which the global configuration and data files are
|
|
|
|
* stored.
|
2003-03-26 00:34:27 +00:00
|
|
|
*
|
2006-05-31 00:23:01 +00:00
|
|
|
* XXX - if we ever make libwireshark a real library, used by multiple
|
2006-05-31 17:38:42 +00:00
|
|
|
* applications (more than just TShark and versions of Wireshark with
|
2003-03-26 00:34:27 +00:00
|
|
|
* various UIs), should the configuration files belong to the library
|
|
|
|
* (and be shared by all those applications) or to the applications?
|
|
|
|
*
|
|
|
|
* If they belong to the library, that could be done on UNIX by the
|
|
|
|
* configure script, but it's trickier on Windows, as you can't just
|
|
|
|
* use the pathname of the executable.
|
|
|
|
*
|
|
|
|
* If they belong to the application, that could be done on Windows
|
|
|
|
* by using the pathname of the executable, but we'd have to have it
|
|
|
|
* passed in as an argument, in some call, on UNIX.
|
|
|
|
*
|
|
|
|
* Note that some of those configuration files might be used by code in
|
2006-05-31 00:23:01 +00:00
|
|
|
* libwireshark, some of them might be used by dissectors (would they
|
|
|
|
* belong to libwireshark, the application, or a separate library?),
|
2006-05-22 08:21:22 +00:00
|
|
|
* and some of them might be used by other code (the Wireshark preferences
|
2003-03-26 00:34:27 +00:00
|
|
|
* file includes resolver preferences that control the behavior of code
|
2006-05-31 00:23:01 +00:00
|
|
|
* in libwireshark, dissector preferences, and UI preferences, for
|
2003-03-26 00:34:27 +00:00
|
|
|
* example).
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
get_datafile_dir(void)
|
|
|
|
{
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
/*
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
* Do we have the pathname of the program? If so, assume we're
|
|
|
|
* running an installed version of the program. If we fail,
|
|
|
|
* we don't change "datafile_dir", and thus end up using the
|
|
|
|
* default.
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
*
|
|
|
|
* XXX - does NSIS put the installation directory into
|
2006-05-28 19:49:07 +00:00
|
|
|
* "\HKEY_LOCAL_MACHINE\SOFTWARE\Wireshark\InstallDir"?
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
* If so, perhaps we should read that from the registry,
|
|
|
|
* instead.
|
|
|
|
*/
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
if (progfile_dir != NULL)
|
|
|
|
return progfile_dir;
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
/*
|
|
|
|
* No, we don't.
|
|
|
|
* Fall back on the default installation directory.
|
|
|
|
*/
|
2006-05-28 19:49:07 +00:00
|
|
|
return "C:\\Program Files\\Wireshark\\";
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Just use DATAFILE_DIR, as that's what the configure script
|
|
|
|
* set it to be.
|
|
|
|
*/
|
|
|
|
return DATAFILE_DIR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-10-21 21:48:00 +00:00
|
|
|
/*
|
|
|
|
* Get the directory in which files that, at least on UNIX, are
|
|
|
|
* system files (such as "/etc/ethers") are stored; on Windows,
|
Add a routine to attempt to get the absolute pathname of the executable
file, strip off the last component to get the pathname of the directory
containing the executable file, and save it for future use. On Windows,
you can get that from the OS, but, on UN*X, you have to look at argv[0]
and derive the absolute path from that (argv[0] is not guaranteed to be
an absolute path, or even a path at all). (In addition, if you're
running from the build directory, you might have to strip off a ".libs/"
added to argv[0] as an artifact of the libtoolizing script.)
Use that in the About dialog, and use it to construct the path of
dumpcap.
Don't put quotes into the path of dumpcap; you don't have to quote
strings with spaces in them when handing them to execvp and, in fact,
you *mustn't* quote them, as the quotes will be treated as part of the
pathname.
svn path=/trunk/; revision=17267
2006-02-12 03:15:29 +00:00
|
|
|
* there's no "/etc" directory, so we get them from the global
|
|
|
|
* configuration and data file directory.
|
2001-10-21 21:48:00 +00:00
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
get_systemfile_dir(void)
|
|
|
|
{
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-21 21:48:00 +00:00
|
|
|
return get_datafile_dir();
|
|
|
|
#else
|
|
|
|
return "/etc";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-10-22 23:16:01 +00:00
|
|
|
/*
|
|
|
|
* Name of directory, under the user's home directory, in which
|
|
|
|
* personal configuration files are stored.
|
2001-10-24 06:13:07 +00:00
|
|
|
*/
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-28 19:49:07 +00:00
|
|
|
#define PF_DIR "Wireshark"
|
2001-10-24 06:13:07 +00:00
|
|
|
#else
|
|
|
|
/*
|
2001-10-22 23:16:01 +00:00
|
|
|
* XXX - should this be ".libepan"? For backwards-compatibility, I'll keep
|
2006-05-28 19:49:07 +00:00
|
|
|
* it ".wireshark" for now.
|
2001-10-22 23:16:01 +00:00
|
|
|
*/
|
2006-05-28 19:49:07 +00:00
|
|
|
#define PF_DIR ".wireshark"
|
2001-10-24 06:13:07 +00:00
|
|
|
#endif
|
2001-10-22 23:16:01 +00:00
|
|
|
|
2006-01-12 22:32:29 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
/* utf8 version of getenv, needed to get win32 filename paths */
|
|
|
|
char *getenv_utf8(const char *varname)
|
|
|
|
{
|
|
|
|
char *envvar;
|
|
|
|
wchar_t *envvarw;
|
|
|
|
wchar_t *varnamew;
|
|
|
|
|
|
|
|
envvar = getenv(varname);
|
|
|
|
|
|
|
|
/* since GLib 2.6 we need an utf8 version of the filename */
|
|
|
|
#if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6)
|
|
|
|
if (!G_WIN32_HAVE_WIDECHAR_API ()) {
|
|
|
|
/* Windows OT (9x, ME), convert from current code page to utf8 */
|
|
|
|
/* it's the best we can do here ... */
|
|
|
|
envvar = g_locale_to_utf8(envvar, -1, NULL, NULL, NULL);
|
|
|
|
/* XXX - memleak */
|
|
|
|
return envvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Windows NT, 2000, XP, ... */
|
|
|
|
/* using the wide char version of getenv should work under all circumstances */
|
|
|
|
|
|
|
|
/* convert given varname to utf16, needed by _wgetenv */
|
|
|
|
varnamew = g_utf8_to_utf16(varname, -1, NULL, NULL, NULL);
|
|
|
|
if (varnamew == NULL) {
|
|
|
|
return envvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use wide char version of getenv */
|
|
|
|
envvarw = _wgetenv(varnamew);
|
|
|
|
g_free(varnamew);
|
|
|
|
if (envvarw == NULL) {
|
|
|
|
return envvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert value to utf8 */
|
|
|
|
envvar = g_utf16_to_utf8(envvarw, -1, NULL, NULL, NULL);
|
|
|
|
/* XXX - memleak */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return envvar;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-22 23:16:01 +00:00
|
|
|
/*
|
|
|
|
* Get the directory in which personal configuration files reside;
|
2006-05-28 19:49:07 +00:00
|
|
|
* in UNIX-compatible systems, it's ".wireshark", under the user's home
|
|
|
|
* directory, and on Windows systems, it's "Wireshark", under %APPDATA%
|
2001-10-24 06:13:07 +00:00
|
|
|
* or, if %APPDATA% isn't set, it's "%USERPROFILE%\Application Data"
|
|
|
|
* (which is what %APPDATA% normally is on Windows 2000).
|
2001-10-22 23:16:01 +00:00
|
|
|
*/
|
2001-10-24 07:18:39 +00:00
|
|
|
static const char *
|
2001-10-22 23:16:01 +00:00
|
|
|
get_persconffile_dir(void)
|
2000-09-28 03:16:29 +00:00
|
|
|
{
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-24 06:13:07 +00:00
|
|
|
char *appdatadir;
|
|
|
|
char *userprofiledir;
|
|
|
|
#else
|
2005-07-23 11:41:25 +00:00
|
|
|
const char *homedir;
|
2000-09-28 03:16:29 +00:00
|
|
|
struct passwd *pwd;
|
|
|
|
#endif
|
2001-10-22 23:16:01 +00:00
|
|
|
static char *pf_dir = NULL;
|
2000-09-28 03:16:29 +00:00
|
|
|
|
|
|
|
/* Return the cached value, if available */
|
2001-10-22 23:16:01 +00:00
|
|
|
if (pf_dir != NULL)
|
|
|
|
return pf_dir;
|
|
|
|
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2000-09-28 03:16:29 +00:00
|
|
|
/*
|
2001-10-24 06:13:07 +00:00
|
|
|
* Use %APPDATA% or %USERPROFILE%, so that configuration files are
|
|
|
|
* stored in the user profile, rather than in the home directory.
|
2001-10-23 08:15:11 +00:00
|
|
|
* The Windows convention is to store configuration information
|
|
|
|
* in the user profile, and doing so means you can use
|
2006-05-28 19:49:07 +00:00
|
|
|
* Wireshark even if the home directory is an inaccessible
|
2001-10-23 08:15:11 +00:00
|
|
|
* network drive.
|
2000-09-28 03:16:29 +00:00
|
|
|
*/
|
2006-01-12 22:32:29 +00:00
|
|
|
appdatadir = getenv_utf8("APPDATA");
|
2001-10-24 06:13:07 +00:00
|
|
|
if (appdatadir != NULL) {
|
2000-09-28 03:16:29 +00:00
|
|
|
/*
|
2006-05-28 19:49:07 +00:00
|
|
|
* Concatenate %APPDATA% with "\Wireshark".
|
2000-09-28 03:16:29 +00:00
|
|
|
*/
|
2006-03-08 22:13:21 +00:00
|
|
|
pf_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
|
2005-08-08 18:50:39 +00:00
|
|
|
appdatadir, PF_DIR);
|
2001-10-24 06:13:07 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* OK, %APPDATA% wasn't set, so use
|
|
|
|
* %USERPROFILE%\Application Data.
|
|
|
|
*/
|
2006-01-12 22:32:29 +00:00
|
|
|
userprofiledir = getenv_utf8("USERPROFILE");
|
2001-10-24 06:13:07 +00:00
|
|
|
if (userprofiledir != NULL) {
|
2005-08-08 18:50:39 +00:00
|
|
|
pf_dir = g_strdup_printf(
|
2001-10-24 06:13:07 +00:00
|
|
|
"%s" G_DIR_SEPARATOR_S "Application Data" G_DIR_SEPARATOR_S "%s",
|
|
|
|
userprofiledir, PF_DIR);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Give up and use "C:".
|
|
|
|
*/
|
2005-08-08 18:50:39 +00:00
|
|
|
pf_dir = g_strdup_printf("C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
|
2001-10-24 06:13:07 +00:00
|
|
|
}
|
2000-09-28 03:16:29 +00:00
|
|
|
}
|
|
|
|
#else
|
2001-10-22 23:16:01 +00:00
|
|
|
/*
|
|
|
|
* If $HOME is set, use that.
|
|
|
|
*/
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
if (homedir == NULL) {
|
2000-09-28 03:16:29 +00:00
|
|
|
/*
|
|
|
|
* Get their home directory from the password file.
|
|
|
|
* If we can't even find a password file entry for them,
|
|
|
|
* use "/tmp".
|
|
|
|
*/
|
|
|
|
pwd = getpwuid(getuid());
|
|
|
|
if (pwd != NULL) {
|
|
|
|
/*
|
|
|
|
* This is cached, so we don't need to worry
|
|
|
|
* about allocating multiple ones of them.
|
|
|
|
*/
|
2001-10-22 23:16:01 +00:00
|
|
|
homedir = g_strdup(pwd->pw_dir);
|
2000-09-28 03:16:29 +00:00
|
|
|
} else
|
2001-10-22 23:16:01 +00:00
|
|
|
homedir = "/tmp";
|
2000-09-28 03:16:29 +00:00
|
|
|
}
|
2005-08-08 18:50:39 +00:00
|
|
|
pf_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
|
2001-10-24 06:13:07 +00:00
|
|
|
#endif
|
|
|
|
|
2001-10-22 22:59:26 +00:00
|
|
|
return pf_dir;
|
|
|
|
}
|
2001-10-23 05:01:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the directory that holds personal configuration files, if
|
|
|
|
* necessary. If we attempted to create it, and failed, return -1 and
|
2001-10-24 06:13:07 +00:00
|
|
|
* set "*pf_dir_path_return" to the pathname of the directory we failed
|
|
|
|
* to create (it's g_mallocated, so our caller should free it); otherwise,
|
2001-10-23 05:01:02 +00:00
|
|
|
* return 0.
|
|
|
|
*/
|
|
|
|
int
|
2001-10-24 06:13:07 +00:00
|
|
|
create_persconffile_dir(char **pf_dir_path_return)
|
2001-10-23 05:01:02 +00:00
|
|
|
{
|
|
|
|
const char *pf_dir_path;
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-24 09:22:23 +00:00
|
|
|
char *pf_dir_path_copy, *pf_dir_parent_path;
|
2002-01-04 21:50:26 +00:00
|
|
|
size_t pf_dir_parent_path_len;
|
2001-10-24 09:22:23 +00:00
|
|
|
#endif
|
2001-10-23 05:01:02 +00:00
|
|
|
struct stat s_buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pf_dir_path = get_persconffile_dir();
|
2005-11-06 22:43:25 +00:00
|
|
|
if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-24 06:13:07 +00:00
|
|
|
/*
|
|
|
|
* Does the parent directory of that directory
|
|
|
|
* exist? %APPDATA% may not exist even though
|
|
|
|
* %USERPROFILE% does.
|
2002-01-04 21:50:26 +00:00
|
|
|
*
|
|
|
|
* We check for the existence of the directory
|
|
|
|
* by first checking whether the parent directory
|
|
|
|
* is just a drive letter and, if it's not, by
|
|
|
|
* doing a "stat()" on it. If it's a drive letter,
|
|
|
|
* or if the "stat()" succeeds, we assume it exists.
|
2001-10-24 06:13:07 +00:00
|
|
|
*/
|
|
|
|
pf_dir_path_copy = g_strdup(pf_dir_path);
|
|
|
|
pf_dir_parent_path = get_dirname(pf_dir_path_copy);
|
2002-01-04 21:50:26 +00:00
|
|
|
pf_dir_parent_path_len = strlen(pf_dir_parent_path);
|
|
|
|
if (pf_dir_parent_path_len > 0
|
|
|
|
&& pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
|
2005-11-06 22:43:25 +00:00
|
|
|
&& eth_stat(pf_dir_parent_path, &s_buf) != 0) {
|
2001-10-24 06:13:07 +00:00
|
|
|
/*
|
2002-01-04 21:50:26 +00:00
|
|
|
* No, it doesn't exist - make it first.
|
2001-10-24 06:13:07 +00:00
|
|
|
*/
|
2005-11-06 22:43:25 +00:00
|
|
|
ret = eth_mkdir(pf_dir_parent_path, 0755);
|
2001-10-24 06:13:07 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
*pf_dir_path_return = pf_dir_parent_path;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(pf_dir_path_copy);
|
2005-11-06 22:43:25 +00:00
|
|
|
ret = eth_mkdir(pf_dir_path, 0755);
|
2001-10-23 05:01:02 +00:00
|
|
|
#else
|
2005-11-06 22:43:25 +00:00
|
|
|
ret = eth_mkdir(pf_dir_path, 0755);
|
2001-10-23 05:01:02 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Something with that pathname exists; if it's not
|
|
|
|
* a directory, we'll get an error if we try to put
|
|
|
|
* something in it, so we don't fail here, we wait
|
|
|
|
* for that attempt fo fail.
|
|
|
|
*/
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
if (ret == -1)
|
2001-10-24 06:13:07 +00:00
|
|
|
*pf_dir_path_return = g_strdup(pf_dir_path);
|
2001-10-23 05:01:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2001-10-24 07:18:39 +00:00
|
|
|
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-24 07:18:39 +00:00
|
|
|
/*
|
|
|
|
* 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?
|
|
|
|
*/
|
2006-01-12 22:32:29 +00:00
|
|
|
homedrive = getenv_utf8("HOMEDRIVE");
|
2001-10-24 07:18:39 +00:00
|
|
|
if (homedrive != NULL) {
|
2006-01-12 22:32:29 +00:00
|
|
|
homepath = getenv_utf8("HOMEPATH");
|
2001-10-24 07:18:39 +00:00
|
|
|
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:";
|
|
|
|
}
|
2001-10-24 09:22:23 +00:00
|
|
|
|
|
|
|
return home;
|
2001-10-24 07:18:39 +00:00
|
|
|
}
|
|
|
|
#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
|
2006-05-28 19:49:07 +00:00
|
|
|
* and, if not, construct a path name relative to the ".wireshark"
|
2001-10-24 07:18:39 +00:00
|
|
|
* 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 *
|
2002-03-02 20:51:46 +00:00
|
|
|
get_persconffile_path(const char *filename, gboolean for_writing
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifndef _WIN32
|
2002-03-02 20:51:46 +00:00
|
|
|
_U_
|
|
|
|
#endif
|
|
|
|
)
|
2001-10-24 07:18:39 +00:00
|
|
|
{
|
|
|
|
char *path;
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-24 07:18:39 +00:00
|
|
|
struct stat s_buf;
|
|
|
|
char *old_path;
|
|
|
|
#endif
|
|
|
|
|
2005-08-08 18:50:39 +00:00
|
|
|
path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
|
2001-10-24 07:18:39 +00:00
|
|
|
filename);
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
2001-10-24 07:18:39 +00:00
|
|
|
if (!for_writing) {
|
2005-11-06 22:43:25 +00:00
|
|
|
if (eth_stat(path, &s_buf) != 0 && errno == ENOENT) {
|
2001-10-24 07:18:39 +00:00
|
|
|
/*
|
|
|
|
* OK, it's not in the personal configuration file
|
2006-05-28 19:49:07 +00:00
|
|
|
* directory; is it in the ".wireshark" subdirectory
|
2001-10-24 07:18:39 +00:00
|
|
|
* of their home directory?
|
|
|
|
*/
|
2005-08-08 18:50:39 +00:00
|
|
|
old_path = g_strdup_printf(
|
2006-05-28 19:49:07 +00:00
|
|
|
"%s" G_DIR_SEPARATOR_S ".wireshark" G_DIR_SEPARATOR_S "%s",
|
2001-10-24 07:18:39 +00:00
|
|
|
get_home_dir(), filename);
|
2005-11-06 22:43:25 +00:00
|
|
|
if (eth_stat(old_path, &s_buf) == 0) {
|
2001-10-24 07:18:39 +00:00
|
|
|
/*
|
|
|
|
* OK, it exists; return it instead.
|
|
|
|
*/
|
|
|
|
g_free(path);
|
|
|
|
path = old_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return path;
|
2001-10-24 09:22:23 +00:00
|
|
|
}
|
2003-05-15 07:44:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct the path name of a global configuration file, given the
|
|
|
|
* file name.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
get_datafile_path(const char *filename)
|
|
|
|
{
|
|
|
|
|
2005-08-08 18:50:39 +00:00
|
|
|
return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
|
2003-05-15 07:44:54 +00:00
|
|
|
filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a file */
|
|
|
|
gboolean
|
|
|
|
deletefile(const char *path)
|
|
|
|
{
|
2005-11-06 22:43:25 +00:00
|
|
|
return eth_unlink(path) == 0;
|
2003-05-15 07:44:54 +00:00
|
|
|
}
|
2003-11-02 23:12:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct and return the path name of a file in the
|
2003-11-03 22:32:36 +00:00
|
|
|
* appropriate temporary file directory.
|
2003-11-02 23:12:35 +00:00
|
|
|
*/
|
2003-11-03 02:41:07 +00:00
|
|
|
char *get_tempfile_path(const char *filename)
|
2003-11-02 23:12:35 +00:00
|
|
|
{
|
2003-11-03 02:41:07 +00:00
|
|
|
|
2005-08-08 18:50:39 +00:00
|
|
|
return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
|
2003-11-02 23:12:35 +00:00
|
|
|
}
|
|
|
|
|
2004-01-24 01:44:29 +00:00
|
|
|
/*
|
2004-01-24 02:01:44 +00:00
|
|
|
* Return an error message for UNIX-style errno indications on open or
|
|
|
|
* create operations.
|
2004-01-24 01:44:29 +00:00
|
|
|
*/
|
2005-07-23 11:41:25 +00:00
|
|
|
const char *
|
2004-01-24 01:44:29 +00:00
|
|
|
file_open_error_message(int err, gboolean for_writing)
|
|
|
|
{
|
2005-07-23 11:41:25 +00:00
|
|
|
const char *errmsg;
|
2004-01-24 01:44:29 +00:00
|
|
|
static char errmsg_errno[1024+1];
|
|
|
|
|
|
|
|
switch (err) {
|
|
|
|
|
|
|
|
case ENOENT:
|
|
|
|
if (for_writing)
|
2004-12-29 01:08:20 +00:00
|
|
|
errmsg = "The path to the file \"%s\" doesn't exist.";
|
2004-01-24 01:44:29 +00:00
|
|
|
else
|
2004-12-29 01:08:20 +00:00
|
|
|
errmsg = "The file \"%s\" doesn't exist.";
|
2004-01-24 01:44:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EACCES:
|
|
|
|
if (for_writing)
|
2004-12-29 01:08:20 +00:00
|
|
|
errmsg = "You don't have permission to create or write to the file \"%s\".";
|
2004-01-24 01:44:29 +00:00
|
|
|
else
|
2004-12-29 01:08:20 +00:00
|
|
|
errmsg = "You don't have permission to read the file \"%s\".";
|
2004-01-24 01:44:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EISDIR:
|
|
|
|
errmsg = "\"%s\" is a directory (folder), not a file.";
|
|
|
|
break;
|
|
|
|
|
2004-01-24 02:01:44 +00:00
|
|
|
case ENOSPC:
|
|
|
|
errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef EDQUOT
|
|
|
|
case EDQUOT:
|
|
|
|
errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2004-01-24 01:44:29 +00:00
|
|
|
default:
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
|
2004-01-24 01:44:29 +00:00
|
|
|
"The file \"%%s\" could not be %s: %s.",
|
|
|
|
for_writing ? "created" : "opened",
|
|
|
|
strerror(err));
|
|
|
|
errmsg = errmsg_errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return errmsg;
|
|
|
|
}
|
2004-01-24 02:01:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an error message for UNIX-style errno indications on write
|
|
|
|
* operations.
|
|
|
|
*/
|
2005-07-23 11:41:25 +00:00
|
|
|
const char *
|
2004-01-24 02:01:44 +00:00
|
|
|
file_write_error_message(int err)
|
|
|
|
{
|
2005-07-23 11:41:25 +00:00
|
|
|
const char *errmsg;
|
2004-01-24 02:01:44 +00:00
|
|
|
static char errmsg_errno[1024+1];
|
|
|
|
|
|
|
|
switch (err) {
|
|
|
|
|
|
|
|
case ENOSPC:
|
|
|
|
errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef EDQUOT
|
|
|
|
case EDQUOT:
|
|
|
|
errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
|
2004-01-24 02:01:44 +00:00
|
|
|
"An error occurred while writing to the file \"%%s\": %s.",
|
|
|
|
strerror(err));
|
|
|
|
errmsg = errmsg_errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
2005-04-12 21:12:19 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
file_exists(const char *fname)
|
|
|
|
{
|
|
|
|
struct stat file_stat;
|
|
|
|
|
|
|
|
|
2006-02-08 00:06:38 +00:00
|
|
|
#ifdef _WIN32
|
2005-04-12 21:12:19 +00:00
|
|
|
/*
|
|
|
|
* This is a bit tricky on win32. The st_ino field is documented as:
|
|
|
|
* "The inode, and therefore st_ino, has no meaning in the FAT, ..."
|
|
|
|
* but it *is* set to zero if stat() returns without an error,
|
|
|
|
* so this is working, but maybe not quite the way expected. ULFL
|
|
|
|
*/
|
|
|
|
file_stat.st_ino = 1; /* this will make things work if an error occured */
|
2005-11-06 22:43:25 +00:00
|
|
|
eth_stat(fname, &file_stat);
|
2005-04-12 21:12:19 +00:00
|
|
|
if (file_stat.st_ino == 0) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-02-08 00:06:38 +00:00
|
|
|
#else
|
|
|
|
if (eth_stat(fname, &file_stat) != 0 && errno == ENOENT) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-08 22:13:21 +00:00
|
|
|
|
2005-04-12 21:12:19 +00:00
|
|
|
}
|
|
|
|
|
2006-02-08 02:06:09 +00:00
|
|
|
/*
|
|
|
|
* Check that the from file is not the same as to file
|
|
|
|
* We do it here so we catch all cases ...
|
|
|
|
* Unfortunately, the file requester gives us an absolute file
|
|
|
|
* name and the read file name may be relative (if supplied on
|
|
|
|
* the command line), so we can't just compare paths. From Joerg Mayer.
|
|
|
|
*/
|
2005-04-12 21:12:19 +00:00
|
|
|
gboolean
|
|
|
|
files_identical(const char *fname1, const char *fname2)
|
|
|
|
{
|
|
|
|
/* Two different implementations, because:
|
|
|
|
*
|
2006-02-08 03:25:22 +00:00
|
|
|
* - _fullpath is not available on UN*X, so we can't get full
|
|
|
|
* paths and compare them (which wouldn't work with hard links
|
|
|
|
* in any case);
|
|
|
|
*
|
|
|
|
* - st_ino isn't filled in with a meaningful value on Windows.
|
2005-04-12 21:12:19 +00:00
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
char full1[MAX_PATH], full2[MAX_PATH];
|
|
|
|
|
2006-02-08 02:06:09 +00:00
|
|
|
/*
|
|
|
|
* Get the absolute full paths of the file and compare them.
|
2006-02-08 03:25:22 +00:00
|
|
|
* That won't work if you have hard links, but those aren't
|
2006-02-08 02:06:09 +00:00
|
|
|
* much used on Windows, even though NTFS supports them.
|
2006-02-08 03:25:22 +00:00
|
|
|
*
|
|
|
|
* XXX - will _fullpath work with UNC?
|
2006-02-08 02:06:09 +00:00
|
|
|
*/
|
2005-04-12 21:12:19 +00:00
|
|
|
if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-03-08 22:13:21 +00:00
|
|
|
|
2005-04-12 21:12:19 +00:00
|
|
|
if(strcmp(full1, full2) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
2006-02-08 02:06:09 +00:00
|
|
|
struct stat filestat1, filestat2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare st_dev and st_ino.
|
|
|
|
*/
|
|
|
|
if (eth_stat(fname1, &filestat1) == -1)
|
|
|
|
return FALSE; /* can't get info about the first file */
|
|
|
|
if (eth_stat(fname2, &filestat2) == -1)
|
|
|
|
return FALSE; /* can't get info about the second file */
|
|
|
|
return (filestat1.st_dev == filestat2.st_dev &&
|
|
|
|
filestat1.st_ino == filestat2.st_ino);
|
2005-04-12 21:12:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|