2 * Filesystem utility routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #ifdef HAVE_SYS_STAT_H
49 #include "epan/unicode-utils.h"
54 #include "filesystem.h"
55 #include "privileges.h"
56 #include <wiretap/file_util.h>
58 #define U3_MY_CAPTURES "\\My Captures"
60 char *persconffile_dir = NULL;
61 char *persdatafile_dir = NULL;
64 * Given a pathname, return a pointer to the last pathname separator
65 * character in the pathname, or NULL if the pathname contains no
69 find_last_pathname_separator(const char *path)
77 * We have to scan for '\' or '/'.
78 * Get to the end of the string.
80 separator = strchr(path, '\0'); /* points to ending '\0' */
81 while (separator > path) {
83 if (c == '\\' || c == '/')
84 return separator; /* found it */
88 * OK, we didn't find any, so no directories - but there might
89 * be a drive letter....
91 return strchr(path, ':');
93 separator = strrchr(path, '/');
99 * Given a pathname, return the last component.
102 get_basename(const char *path)
104 const char *filename;
106 g_assert(path != NULL);
107 filename = find_last_pathname_separator(path);
108 if (filename == NULL) {
110 * There're no directories, drive letters, etc. in the
111 * name; the pathname *is* the file name.
116 * Skip past the pathname or drive letter separator.
124 * Given a pathname, return a string containing everything but the
125 * last component. NOTE: this overwrites the pathname handed into
129 get_dirname(char *path)
133 g_assert(path != NULL);
134 separator = find_last_pathname_separator(path);
135 if (separator == NULL) {
137 * There're no directories, drive letters, etc. in the
138 * name; there is no directory path to return.
144 * Get rid of the last pathname separator and the final file
150 * "path" now contains the pathname of the directory containing
151 * the file/directory to which it referred.
157 * Given a pathname, return:
159 * the errno, if an attempt to "stat()" the file fails;
161 * EISDIR, if the attempt succeeded and the file turned out
164 * 0, if the attempt succeeded and the file turned out not
169 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
170 * define them either.)
172 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
175 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
178 #define S_IFIFO _S_IFIFO
181 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
184 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
188 test_for_directory(const char *path)
192 if (eth_stat(path, &statb) < 0)
195 if (S_ISDIR(statb.st_mode))
202 test_for_fifo(const char *path)
206 if (eth_stat(path, &statb) < 0)
209 if (S_ISFIFO(statb.st_mode))
216 * Directory from which the executable came.
218 static char *progfile_dir;
221 * TRUE if we're running from the build directory.
223 static gboolean running_in_build_directory_flag = FALSE;
226 * Get the pathname of the directory from which the executable came,
227 * and save it for future use. Returns NULL on success, and a
228 * g_mallocated string containing an error on failure.
231 init_progfile_dir(const char *arg0
240 TCHAR prog_pathname_w[_MAX_PATH+2];
241 size_t progfile_dir_len;
249 * Attempt to get the full pathname of the currently running
252 if (GetModuleFileName(NULL, prog_pathname_w, sizeof prog_pathname_w) != 0) {
254 * XXX - Should we use g_utf16_to_utf8(), as in
257 prog_pathname = utf_16to8(prog_pathname_w);
259 * We got it; strip off the last component, which would be
260 * the file name of the executable, giving us the pathname
261 * of the directory where the executable resies
263 * First, find the last "\" in the directory, as that
264 * marks the end of the directory pathname.
266 * XXX - Can the pathname be something such as
267 * "C:wireshark.exe"? Or is it always a full pathname
268 * beginning with "\" after the drive letter?
270 dir_end = strrchr(prog_pathname, '\\');
271 if (dir_end != NULL) {
273 * Found it - now figure out how long the program
274 * directory pathname will be.
276 progfile_dir_len = (dir_end - prog_pathname);
279 * Allocate a buffer for the program directory
280 * pathname, and construct it.
282 path = g_malloc(progfile_dir_len + 1);
283 strncpy(path, prog_pathname, progfile_dir_len);
284 path[progfile_dir_len] = '\0';
287 return NULL; /* we succeeded */
290 * OK, no \ - what do we do now?
292 return g_strdup_printf("No \\ in executable pathname \"%s\"",
297 * Oh, well. Return an indication of the error.
299 error = GetLastError();
300 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
301 NULL, error, 0, (LPTSTR) &msg_w, 0, NULL) == 0) {
303 * Gak. We can't format the message.
305 return g_strdup_printf("GetModuleFileName failed: %u (FormatMessage failed: %u)",
306 error, GetLastError());
308 msg = utf_16to8(msg_w);
311 * "FormatMessage()" "helpfully" sticks CR/LF at the
312 * end of the message. Get rid of it.
314 msglen = strlen(msg);
316 msg[msglen - 1] = '\0';
317 msg[msglen - 2] = '\0';
319 return g_strdup_printf("GetModuleFileName failed: %s (%u)",
327 char *path_start, *path_end;
328 size_t path_component_len;
332 * Check whether WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set in the
333 * environment; if so, set running_in_build_directory_flag if we
334 * weren't started with special privileges. (If we were started
335 * with special privileges, it's not safe to allow the user to point
336 * us to some other directory; running_in_build_directory_flag, when
337 * set, causes us to look for plugins and the like in the build
340 if (getenv("WIRESHARK_RUN_FROM_BUILD_DIRECTORY") != NULL
341 && !started_with_special_privs())
342 running_in_build_directory_flag = TRUE;
345 * Try to figure out the directory in which the currently running
346 * program resides, given the argv[0] it was started with. That
347 * might be the absolute path of the program, or a path relative
348 * to the current directory of the process that started it, or
349 * just a name for the program if it was started from the command
350 * line and was searched for in $PATH. It's not guaranteed to be
351 * any of those, however, so there are no guarantees....
353 if (arg0[0] == '/') {
355 * It's an absolute path.
357 prog_pathname = g_strdup(arg0);
358 } else if (strchr(arg0, '/') != NULL) {
360 * It's a relative path, with a directory in it.
361 * Get the current directory, and combine it
362 * with that directory.
364 path_max = pathconf(".", _PC_PATH_MAX);
365 if (path_max == -1) {
367 * We have no idea how big a buffer to
368 * allocate for the current directory.
370 return g_strdup_printf("pathconf failed: %s\n",
373 curdir = g_malloc(path_max);
374 if (getcwd(curdir, path_max) == NULL) {
376 * It failed - give up, and just stick
380 return g_strdup_printf("getcwd failed: %s\n",
383 path = g_malloc(strlen(curdir) + 1 + strlen(arg0) + 1);
384 strcpy(path, curdir);
388 prog_pathname = path;
391 * It's just a file name.
392 * Search the path for a file with that name
395 prog_pathname = NULL; /* haven't found it yet */
396 pathstr = getenv("PATH");
397 path_start = pathstr;
398 if (path_start != NULL) {
399 while (*path_start != '\0') {
400 path_end = strchr(path_start, ':');
401 if (path_end == NULL)
402 path_end = path_start + strlen(path_start);
403 path_component_len = path_end - path_start;
404 path = g_malloc(path_component_len + 1
406 memcpy(path, path_start, path_component_len);
407 path[path_component_len] = '\0';
410 if (access(path, X_OK) == 0) {
414 prog_pathname = path;
419 * That's not it. If there are more
420 * path components to test, try them.
422 if (*path_end == '\0') {
424 * There's nothing more to try.
428 if (*path_end == ':')
430 path_start = path_end;
433 if (prog_pathname == NULL) {
435 * Program not found in path.
437 return g_strdup_printf("\"%s\" not found in \"%s\"",
443 * XXX - should we pick a default?
445 return g_strdup("PATH isn't set");
450 * OK, we have what we think is the pathname
453 * First, find the last "/" in the directory,
454 * as that marks the end of the directory pathname.
456 dir_end = strrchr(prog_pathname, '/');
457 if (dir_end != NULL) {
459 * Found it. Strip off the last component,
460 * as that's the path of the program.
465 * Is there a "/.libs" at the end?
467 dir_end = strrchr(prog_pathname, '/');
468 if (dir_end != NULL) {
469 if (strcmp(dir_end, "/.libs") == 0) {
472 * Strip that off; it's an
473 * artifact of libtool.
478 * This presumably means we're run from
479 * the libtool wrapper, which probably
480 * means we're being run from the build
481 * directory. If we weren't started
482 * with special privileges, set
483 * running_in_build_directory_flag.
485 * XXX - should we check whether what
486 * follows ".libs/" begins with "lt-"?
488 if (!started_with_special_privs())
489 running_in_build_directory_flag = TRUE;
494 * OK, we have the path we want.
496 progfile_dir = prog_pathname;
500 * This "shouldn't happen"; we apparently
501 * have no "/" in the pathname.
502 * Just free up prog_pathname.
504 retstr = g_strdup_printf("No / found in \"%s\"", prog_pathname);
505 g_free(prog_pathname);
512 * Get the directory in which the program resides.
515 get_progfile_dir(void)
521 * Get the directory in which the global configuration and data files are
524 * On Windows, we use the directory in which the executable for this
527 * On UN*X, we use the DATAFILE_DIR value supplied by the configure
528 * script, unless we think we're being run from the build directory,
529 * in which case we use the directory in which the executable for this
532 * XXX - if we ever make libwireshark a real library, used by multiple
533 * applications (more than just TShark and versions of Wireshark with
534 * various UIs), should the configuration files belong to the library
535 * (and be shared by all those applications) or to the applications?
537 * If they belong to the library, that could be done on UNIX by the
538 * configure script, but it's trickier on Windows, as you can't just
539 * use the pathname of the executable.
541 * If they belong to the application, that could be done on Windows
542 * by using the pathname of the executable, but we'd have to have it
543 * passed in as an argument, in some call, on UNIX.
545 * Note that some of those configuration files might be used by code in
546 * libwireshark, some of them might be used by dissectors (would they
547 * belong to libwireshark, the application, or a separate library?),
548 * and some of them might be used by other code (the Wireshark preferences
549 * file includes resolver preferences that control the behavior of code
550 * in libwireshark, dissector preferences, and UI preferences, for
554 get_datafile_dir(void)
557 char *u3deviceexecpath;
559 static char *datafile_dir = NULL;
561 if (datafile_dir != NULL)
566 * See if we are running in a U3 environment.
568 u3deviceexecpath = getenv_utf8("U3_DEVICE_EXEC_PATH");
570 if (u3deviceexecpath != NULL) {
572 * We are; use the U3 device executable path.
574 datafile_dir = u3deviceexecpath;
577 * Do we have the pathname of the program? If so, assume we're
578 * running an installed version of the program. If we fail,
579 * we don't change "datafile_dir", and thus end up using the
582 * XXX - does NSIS put the installation directory into
583 * "\HKEY_LOCAL_MACHINE\SOFTWARE\Wireshark\InstallDir"?
584 * If so, perhaps we should read that from the registry,
587 if (progfile_dir != NULL) {
589 * Yes, we do; use that.
591 datafile_dir = progfile_dir;
595 * Fall back on the default installation directory.
597 datafile_dir = "C:\\Program Files\\Wireshark\\";
601 if (running_in_build_directory_flag && progfile_dir != NULL) {
603 * We're (probably) being run from the build directory and
604 * weren't started with special privileges, and we were
605 * able to determine the directory in which the program
606 * was found, so use that.
608 datafile_dir = progfile_dir;
611 * Return the directory specified when the build
614 datafile_dir = DATAFILE_DIR;
623 * Find the directory where the plugins are stored.
625 * On Windows, we use the "plugin" subdirectory of the datafile directory.
627 * On UN*X, we use the PLUGIN_DIR value supplied by the configure
628 * script, unless we think we're being run from the build directory,
629 * in which case we use the "plugin" subdirectory of the datafile directory.
631 * In both cases, we then use the subdirectory of that directory whose
632 * name is the version number.
634 * XXX - if we think we're being run from the build directory, perhaps we
635 * should have the plugin code not look in the version subdirectory
636 * of the plugin directory, but look in all of the subdirectories
637 * of the plugin directory, so it can just fetch the plugins built
638 * as part of the build process.
640 static const char *plugin_dir = NULL;
643 init_plugin_dir(void)
647 * On Windows, the data file directory is the installation
648 * directory; the plugins are stored under it.
650 * Assume we're running the installed version of Wireshark;
651 * on Windows, the data file directory is the directory
652 * in which the Wireshark binary resides.
654 plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(),
658 * Make sure that pathname refers to a directory.
660 if (test_for_directory(plugin_dir) != EISDIR) {
662 * Either it doesn't refer to a directory or it
663 * refers to something that doesn't exist.
665 * Assume that means we're running a version of
666 * Wireshark we've built in a build directory,
667 * in which case {datafile dir}\plugins is the
668 * top-level plugins source directory, and use
669 * that directory and set the "we're running in
670 * a build directory" flag, so the plugin
671 * scanner will check all subdirectories of that
672 * directory for plugins.
674 g_free( (gpointer) plugin_dir);
675 plugin_dir = g_strdup_printf("%s\\plugins", get_datafile_dir());
676 running_in_build_directory_flag = TRUE;
679 if (running_in_build_directory_flag) {
681 * We're (probably) being run from the build directory and
682 * weren't started with special privileges, so we'll use
683 * the "plugins" subdirectory of the datafile directory
684 * (the datafile directory is the build directory).
686 plugin_dir = g_strdup_printf("%s/plugins", get_datafile_dir());
688 plugin_dir = PLUGIN_DIR;
691 #endif /* HAVE_PLUGINS */
694 * Get the directory in which the plugins are stored.
700 if (!plugin_dir) init_plugin_dir();
708 * Get the flag indicating whether we're running from a build
712 running_in_build_directory(void)
714 return running_in_build_directory_flag;
718 * Get the directory in which files that, at least on UNIX, are
719 * system files (such as "/etc/ethers") are stored; on Windows,
720 * there's no "/etc" directory, so we get them from the global
721 * configuration and data file directory.
724 get_systemfile_dir(void)
727 return get_datafile_dir();
734 * Name of directory, under the user's home directory, in which
735 * personal configuration files are stored.
738 #define PF_DIR "Wireshark"
741 * XXX - should this be ".libepan"? For backwards-compatibility, I'll keep
742 * it ".wireshark" for now.
744 #define PF_DIR ".wireshark"
748 /* utf8 version of getenv, needed to get win32 filename paths */
749 char *getenv_utf8(const char *varname)
755 envvar = getenv(varname);
757 /* since GLib 2.6 we need an utf8 version of the filename */
758 #if (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
759 /* using the wide char version of getenv should work under all circumstances */
761 /* convert given varname to utf16, needed by _wgetenv */
762 varnamew = g_utf8_to_utf16(varname, -1, NULL, NULL, NULL);
763 if (varnamew == NULL) {
767 /* use wide char version of getenv */
768 envvarw = _wgetenv(varnamew);
770 if (envvarw == NULL) {
774 /* convert value to utf8 */
775 envvar = g_utf16_to_utf8(envvarw, -1, NULL, NULL, NULL);
784 * Get the directory in which personal configuration files reside;
785 * in UNIX-compatible systems, it's ".wireshark", under the user's home
786 * directory, and on Windows systems, it's "Wireshark", under %APPDATA%
787 * or, if %APPDATA% isn't set, it's "%USERPROFILE%\Application Data"
788 * (which is what %APPDATA% normally is on Windows 2000).
791 get_persconffile_dir(void)
795 char *userprofiledir;
802 /* Return the cached value, if available */
803 if (persconffile_dir != NULL)
804 return persconffile_dir;
808 * See if we are running in a U3 environment.
810 u3appdatapath = getenv_utf8("U3_APP_DATA_PATH");
811 if (u3appdatapath != NULL) {
813 * We are; use the U3 application data path.
815 persconffile_dir = u3appdatapath;
818 * Use %APPDATA% or %USERPROFILE%, so that configuration
819 * files are stored in the user profile, rather than in
820 * the home directory. The Windows convention is to store
821 * configuration information in the user profile, and doing
822 * so means you can use Wireshark even if the home directory
823 * is an inaccessible network drive.
825 appdatadir = getenv_utf8("APPDATA");
826 if (appdatadir != NULL) {
828 * Concatenate %APPDATA% with "\Wireshark".
830 persconffile_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
834 * OK, %APPDATA% wasn't set, so use
835 * %USERPROFILE%\Application Data.
837 userprofiledir = getenv_utf8("USERPROFILE");
838 if (userprofiledir != NULL) {
839 persconffile_dir = g_strdup_printf(
840 "%s" G_DIR_SEPARATOR_S "Application Data" G_DIR_SEPARATOR_S "%s",
841 userprofiledir, PF_DIR);
844 * Give up and use "C:".
846 persconffile_dir = g_strdup_printf("C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
852 * If $HOME is set, use that.
854 homedir = getenv("HOME");
855 if (homedir == NULL) {
857 * Get their home directory from the password file.
858 * If we can't even find a password file entry for them,
861 pwd = getpwuid(getuid());
864 * This is cached, so we don't need to worry
865 * about allocating multiple ones of them.
867 homedir = g_strdup(pwd->pw_dir);
871 persconffile_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
874 return persconffile_dir;
878 * Create the directory that holds personal configuration files, if
879 * necessary. If we attempted to create it, and failed, return -1 and
880 * set "*pf_dir_path_return" to the pathname of the directory we failed
881 * to create (it's g_mallocated, so our caller should free it); otherwise,
885 create_persconffile_dir(char **pf_dir_path_return)
887 const char *pf_dir_path;
889 char *pf_dir_path_copy, *pf_dir_parent_path;
890 size_t pf_dir_parent_path_len;
895 pf_dir_path = get_persconffile_dir();
896 if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
899 * Does the parent directory of that directory
900 * exist? %APPDATA% may not exist even though
901 * %USERPROFILE% does.
903 * We check for the existence of the directory
904 * by first checking whether the parent directory
905 * is just a drive letter and, if it's not, by
906 * doing a "stat()" on it. If it's a drive letter,
907 * or if the "stat()" succeeds, we assume it exists.
909 pf_dir_path_copy = g_strdup(pf_dir_path);
910 pf_dir_parent_path = get_dirname(pf_dir_path_copy);
911 pf_dir_parent_path_len = strlen(pf_dir_parent_path);
912 if (pf_dir_parent_path_len > 0
913 && pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
914 && eth_stat(pf_dir_parent_path, &s_buf) != 0) {
916 * No, it doesn't exist - make it first.
918 ret = eth_mkdir(pf_dir_parent_path, 0755);
920 *pf_dir_path_return = pf_dir_parent_path;
924 g_free(pf_dir_path_copy);
925 ret = eth_mkdir(pf_dir_path, 0755);
927 ret = eth_mkdir(pf_dir_path, 0755);
931 * Something with that pathname exists; if it's not
932 * a directory, we'll get an error if we try to put
933 * something in it, so we don't fail here, we wait
934 * for that attempt fo fail.
939 *pf_dir_path_return = g_strdup(pf_dir_path);
944 * Get the (default) directory in which personal data is stored.
946 * On Win32, this is the "My Documents" folder in the personal profile.
947 * On UNIX this is simply the current directory.
948 * On a U3 device this is "$U3_DEVICE_DOCUMENT_PATH\My Captures" folder.
950 /* XXX - should this and the get_home_dir() be merged? */
952 get_persdatafile_dir(void)
955 char *u3devicedocumentpath;
956 TCHAR tszPath[MAX_PATH];
961 /* Return the cached value, if available */
962 if (persdatafile_dir != NULL)
963 return persdatafile_dir;
966 * See if we are running in a U3 environment.
968 u3devicedocumentpath = getenv_utf8("U3_DEVICE_DOCUMENT_PATH");
970 if (u3devicedocumentpath != NULL) {
972 /* the "My Captures" sub-directory is created (if it doesn't exist)
973 by u3util.exe when the U3 Wireshark is first run */
975 szPath = g_malloc(strlen(u3devicedocumentpath) + strlen(U3_MY_CAPTURES) + 1);
976 strcpy(szPath, u3devicedocumentpath);
977 strcat(szPath, U3_MY_CAPTURES);
979 persdatafile_dir = szPath;
983 /* Hint: SHGetFolderPath is not available on MSVC 6 - without Platform SDK */
984 bRet = SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL, FALSE);
986 szPath = utf_16to8(tszPath);
987 persdatafile_dir = szPath;
1000 * Returns the user's home directory on Win32.
1005 static const char *home = NULL;
1006 char *homedrive, *homepath;
1010 /* Return the cached value, if available */
1015 * XXX - should we use USERPROFILE anywhere in this process?
1016 * Is there a chance that it might be set but one or more of
1017 * HOMEDRIVE or HOMEPATH isn't set?
1019 homedrive = getenv_utf8("HOMEDRIVE");
1020 if (homedrive != NULL) {
1021 homepath = getenv_utf8("HOMEPATH");
1022 if (homepath != NULL) {
1024 * This is cached, so we don't need to worry about
1025 * allocating multiple ones of them.
1028 g_malloc(strlen(homedrive) + strlen(homepath) + 1);
1029 strcpy(homestring, homedrive);
1030 strcat(homestring, homepath);
1033 * Trim off any trailing slash or backslash.
1035 lastsep = find_last_pathname_separator(homestring);
1036 if (lastsep != NULL && *(lastsep + 1) == '\0') {
1038 * Last separator is the last character
1039 * in the string. Nuke it.
1048 * Give up and use C:.
1058 * Construct the path name of a personal configuration file, given the
1061 * On Win32, if "for_writing" is FALSE, we check whether the file exists
1062 * and, if not, construct a path name relative to the ".wireshark"
1063 * subdirectory of the user's home directory, and check whether that
1064 * exists; if it does, we return that, so that configuration files
1065 * from earlier versions can be read.
1068 get_persconffile_path(const char *filename, gboolean for_writing
1080 path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
1084 if (eth_stat(path, &s_buf) != 0 && errno == ENOENT) {
1086 * OK, it's not in the personal configuration file
1087 * directory; is it in the ".wireshark" subdirectory
1088 * of their home directory?
1090 old_path = g_strdup_printf(
1091 "%s" G_DIR_SEPARATOR_S ".wireshark" G_DIR_SEPARATOR_S "%s",
1092 get_home_dir(), filename);
1093 if (eth_stat(old_path, &s_buf) == 0) {
1095 * OK, it exists; return it instead.
1108 * process command line option belonging to the filesystem settings
1109 * (move this e.g. to main.c and have set_persconffile_dir() instead in this file?)
1112 filesystem_opt(int opt _U_, const char *optarg)
1116 colonp = strchr(optarg, ':');
1117 if (colonp == NULL) {
1125 * Skip over any white space (there probably won't be any, but
1126 * as we allow it in the preferences file, we might as well
1129 while (isspace((guchar)*p))
1133 * Put the colon back, so if our caller uses, in an
1134 * error message, the string they passed us, the message
1141 /* directory should be existing */
1142 /* XXX - is this a requirement? */
1143 if(test_for_directory(p) != EISDIR) {
1145 * Put the colon back, so if our caller uses, in an
1146 * error message, the string they passed us, the message
1153 if (strcmp(optarg,"persconf") == 0) {
1154 persconffile_dir = p;
1155 } else if (strcmp(optarg,"persdata") == 0) {
1156 persdatafile_dir = p;
1157 /* XXX - might need to add the temp file path */
1161 *colonp = ':'; /* put the colon back */
1166 * Construct the path name of a global configuration file, given the
1170 get_datafile_path(const char *filename)
1173 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
1179 deletefile(const char *path)
1181 return eth_unlink(path) == 0;
1185 * Construct and return the path name of a file in the
1186 * appropriate temporary file directory.
1188 char *get_tempfile_path(const char *filename)
1191 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
1195 * Return an error message for UNIX-style errno indications on open or
1196 * create operations.
1199 file_open_error_message(int err, gboolean for_writing)
1202 static char errmsg_errno[1024+1];
1208 errmsg = "The path to the file \"%s\" doesn't exist.";
1210 errmsg = "The file \"%s\" doesn't exist.";
1215 errmsg = "You don't have permission to create or write to the file \"%s\".";
1217 errmsg = "You don't have permission to read the file \"%s\".";
1221 errmsg = "\"%s\" is a directory (folder), not a file.";
1225 errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
1230 errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
1235 errmsg = "The file \"%s\" could not be created because an invalid filename was specified.";
1239 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1240 "The file \"%%s\" could not be %s: %s.",
1241 for_writing ? "created" : "opened",
1243 errmsg = errmsg_errno;
1250 * Return an error message for UNIX-style errno indications on write
1254 file_write_error_message(int err)
1257 static char errmsg_errno[1024+1];
1262 errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
1267 errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
1272 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1273 "An error occurred while writing to the file \"%%s\": %s.",
1275 errmsg = errmsg_errno;
1283 file_exists(const char *fname)
1285 struct stat file_stat;
1290 * This is a bit tricky on win32. The st_ino field is documented as:
1291 * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
1292 * but it *is* set to zero if stat() returns without an error,
1293 * so this is working, but maybe not quite the way expected. ULFL
1295 file_stat.st_ino = 1; /* this will make things work if an error occured */
1296 eth_stat(fname, &file_stat);
1297 if (file_stat.st_ino == 0) {
1303 if (eth_stat(fname, &file_stat) != 0 && errno == ENOENT) {
1313 * Check that the from file is not the same as to file
1314 * We do it here so we catch all cases ...
1315 * Unfortunately, the file requester gives us an absolute file
1316 * name and the read file name may be relative (if supplied on
1317 * the command line), so we can't just compare paths. From Joerg Mayer.
1320 files_identical(const char *fname1, const char *fname2)
1322 /* Two different implementations, because:
1324 * - _fullpath is not available on UN*X, so we can't get full
1325 * paths and compare them (which wouldn't work with hard links
1328 * - st_ino isn't filled in with a meaningful value on Windows.
1331 char full1[MAX_PATH], full2[MAX_PATH];
1334 * Get the absolute full paths of the file and compare them.
1335 * That won't work if you have hard links, but those aren't
1336 * much used on Windows, even though NTFS supports them.
1338 * XXX - will _fullpath work with UNC?
1340 if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
1344 if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
1348 if(strcmp(full1, full2) == 0) {
1354 struct stat filestat1, filestat2;
1357 * Compare st_dev and st_ino.
1359 if (eth_stat(fname1, &filestat1) == -1)
1360 return FALSE; /* can't get info about the first file */
1361 if (eth_stat(fname2, &filestat2) == -1)
1362 return FALSE; /* can't get info about the second file */
1363 return (filestat1.st_dev == filestat2.st_dev &&
1364 filestat1.st_ino == filestat2.st_ino);