2 * Filesystem utility routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * Required with GNU libc to get dladdr().
27 * We define it here because <dlfcn.h> apparently gets included by
28 * one of the headers we include below.
43 #include <wsutil/unicode-utils.h>
46 #include <mach-o/dyld.h>
49 #include <sys/utsname.h>
52 #include <sys/types.h>
53 #include <sys/sysctl.h>
61 #include "filesystem.h"
62 #include <wsutil/report_message.h>
63 #include <wsutil/privileges.h>
64 #include <wsutil/file_util.h>
65 #include <wsutil/utf8_entities.h>
67 #include <wiretap/wtap.h> /* for WTAP_ERR_SHORT_WRITE */
69 #define PROFILES_DIR "profiles"
70 #define PLUGINS_DIR_NAME "plugins"
71 #define PROFILES_INFO_NAME "profile_files.txt"
73 char *persconffile_dir = NULL;
74 char *persdatafile_dir = NULL;
75 char *persconfprofile = NULL;
77 static gboolean do_store_persconffiles = FALSE;
78 static GHashTable *profile_files = NULL;
81 * Given a pathname, return a pointer to the last pathname separator
82 * character in the pathname, or NULL if the pathname contains no
86 find_last_pathname_separator(const char *path)
94 * We have to scan for '\' or '/'.
95 * Get to the end of the string.
97 separator = strchr(path, '\0'); /* points to ending '\0' */
98 while (separator > path) {
100 if (c == '\\' || c == '/')
101 return separator; /* found it */
105 * OK, we didn't find any, so no directories - but there might
106 * be a drive letter....
108 return strchr(path, ':');
110 separator = strrchr(path, '/');
116 * Given a pathname, return the last component.
119 get_basename(const char *path)
121 const char *filename;
123 g_assert(path != NULL);
124 filename = find_last_pathname_separator(path);
125 if (filename == NULL) {
127 * There're no directories, drive letters, etc. in the
128 * name; the pathname *is* the file name.
133 * Skip past the pathname or drive letter separator.
141 * Given a pathname, return a string containing everything but the
142 * last component. NOTE: this overwrites the pathname handed into
146 get_dirname(char *path)
150 g_assert(path != NULL);
151 separator = find_last_pathname_separator(path);
152 if (separator == NULL) {
154 * There're no directories, drive letters, etc. in the
155 * name; there is no directory path to return.
161 * Get rid of the last pathname separator and the final file
167 * "path" now contains the pathname of the directory containing
168 * the file/directory to which it referred.
174 * Given a pathname, return:
176 * the errno, if an attempt to "stat()" the file fails;
178 * EISDIR, if the attempt succeeded and the file turned out
181 * 0, if the attempt succeeded and the file turned out not
186 test_for_directory(const char *path)
190 if (ws_stat64(path, &statb) < 0)
193 if (S_ISDIR(statb.st_mode))
200 test_for_fifo(const char *path)
204 if (ws_stat64(path, &statb) < 0)
207 if (S_ISFIFO(statb.st_mode))
214 * Directory from which the executable came.
216 static char *progfile_dir;
220 * Directory of the application bundle in which we're contained,
221 * if we're contained in an application bundle. Otherwise, NULL.
223 * Note: Table 2-5 "Subdirectories of the Contents directory" of
225 * https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1
227 * says that the "Frameworks" directory
229 * Contains any private shared libraries and frameworks used by the
230 * executable. The frameworks in this directory are revision-locked
231 * to the application and cannot be superseded by any other, even
232 * newer, versions that may be available to the operating system. In
233 * other words, the frameworks included in this directory take precedence
234 * over any other similarly named frameworks found in other parts of
235 * the operating system. For information on how to add private
236 * frameworks to your application bundle, see Framework Programming Guide.
238 * so if we were to ship with any frameworks (e.g. Qt) we should
239 * perhaps put them in a Frameworks directory rather than under
242 * It also says that the "PlugIns" directory
244 * Contains loadable bundles that extend the basic features of your
245 * application. You use this directory to include code modules that
246 * must be loaded into your applicationbs process space in order to
247 * be used. You would not use this directory to store standalone
250 * Our plugins are just raw .so/.dylib files; I don't know whether by
251 * "bundles" they mean application bundles (i.e., directory hierarchies)
252 * or just "bundles" in the Mach-O sense (which are an image type that
253 * can be loaded with dlopen() but not linked as libraries; our plugins
254 * are, I think, built as dylibs and can be loaded either way).
256 * And it says that the "SharedSupport" directory
258 * Contains additional non-critical resources that do not impact the
259 * ability of the application to run. You might use this directory to
260 * include things like document templates, clip art, and tutorials
261 * that your application expects to be present but that do not affect
262 * the ability of your application to run.
264 * I don't think I'd put the files that currently go under Resources/share
265 * into that category; they're not, for example, sample Lua scripts that
266 * don't actually get run by Wireshark, they're configuration/data files
267 * for Wireshark whose absence might not prevent Wireshark from running
268 * but that would affect how it behaves when run.
270 static char *appbundle_dir;
274 * TRUE if we're running from the build directory and we aren't running
275 * with special privileges.
277 static gboolean running_in_build_directory_flag = FALSE;
281 * Get the pathname of the executable using various platform-
282 * dependent mechanisms for various UN*Xes.
284 * These calls all should return something independent of the argv[0]
285 * passed to the program, so it shouldn't be fooled by an argv[0]
286 * that doesn't match the executable path.
288 * Sadly, not all UN*Xes necessarily have dladdr(), and those that
289 * do don't necessarily have dladdr(main) return information about
290 * the executable image, and those that do aren't necessarily running
291 * on a platform wherein the executable image can get its own path
292 * from the kernel (either by a call or by it being handed to it along
293 * with argv[] and the environment), and those that can don't
294 * necessarily use that to supply the path you get from dladdr(main),
295 * so we try this first and, if that fails, use dladdr(main) if
298 * In particular, some dynamic linkers supply a dladdr() such that
299 * dladdr(main) just returns something derived from argv[0], so
300 * just using dladdr(main) is the wrong thing to do if there's
301 * another mechanism that can get you a more reliable version of
302 * the executable path.
304 * However, at least in newer versions of DragonFly BSD, the dynamic
305 * linker *does* get it from the aux vector passed to the program
306 * by the kernel, readlink /proc/curproc/file - which came first?
308 * On OpenBSD, dladdr(main) returns a value derived from argv[0],
309 * and there doesn't appear to be any way to get the executable path
310 * from the kernel, so we're out of luck there.
312 * So, on platforms where some versions have a version of dladdr()
313 * that gives an argv[0]-based path and that also have a mechanism
314 * to get a more reliable version of the path, we try that. On
315 * other platforms, we return NULL. If our caller gets back a NULL
316 * from us, it falls back on dladdr(main) if dladdr() is available,
317 * and if that fails or is unavailable, it falls back on processing
320 * This is not guaranteed to return an absolute path; if it doesn't,
321 * our caller must prepend the current directory if it's a path.
323 * This is not guaranteed to return the "real path"; it might return
324 * something with symbolic links in the path. Our caller must
325 * use realpath() if they want the real thing, but that's also true of
326 * something obtained by looking at argv[0].
329 get_executable_path(void)
331 #if defined(__APPLE__)
332 char *executable_path;
333 uint32_t path_buf_size;
335 path_buf_size = PATH_MAX;
336 executable_path = (char *)g_malloc(path_buf_size);
337 if (_NSGetExecutablePath(executable_path, &path_buf_size) == -1) {
338 executable_path = (char *)g_realloc(executable_path, path_buf_size);
339 if (_NSGetExecutablePath(executable_path, &path_buf_size) == -1)
342 return executable_path;
343 #elif defined(__linux__)
345 * In older versions of GNU libc's dynamic linker, as used on Linux,
346 * dladdr(main) supplies a path based on argv[0], so we use
347 * /proc/self/exe instead; there are Linux distributions with
348 * kernels that support /proc/self/exe and those older versions
349 * of the dynamic linker, and this will get a better answer on
352 * It only works on Linux 2.2 or later, so we just give up on
355 * XXX - are there OS versions that support "exe" but not "self"?
358 static char executable_path[PATH_MAX + 1];
361 if (uname(&name) == -1)
363 if (strncmp(name.release, "1.", 2) == 0)
364 return NULL; /* Linux 1.x */
365 if (strcmp(name.release, "2.0") == 0 ||
366 strncmp(name.release, "2.0.", 4) == 0 ||
367 strcmp(name.release, "2.1") == 0 ||
368 strncmp(name.release, "2.1.", 4) == 0)
369 return NULL; /* Linux 2.0.x or 2.1.x */
370 if ((r = readlink("/proc/self/exe", executable_path, PATH_MAX)) == -1)
372 executable_path[r] = '\0';
373 return executable_path;
374 #elif defined(__FreeBSD__) && defined(KERN_PROC_PATHNAME)
376 * In older versions of FreeBSD's dynamic linker, dladdr(main)
377 * supplies a path based on argv[0], so we use the KERN_PROC_PATHNAME
378 * sysctl instead; there are, I think, versions of FreeBSD
379 * that support the sysctl that have and those older versions
380 * of the dynamic linker, and this will get a better answer on
384 char *executable_path;
385 size_t path_buf_size;
389 mib[2] = KERN_PROC_PATHNAME;
391 path_buf_size = PATH_MAX;
392 executable_path = (char *)g_malloc(path_buf_size);
393 if (sysctl(mib, 4, executable_path, &path_buf_size, NULL, 0) == -1) {
396 executable_path = (char *)g_realloc(executable_path, path_buf_size);
397 if (sysctl(mib, 4, executable_path, &path_buf_size, NULL, 0) == -1)
400 return executable_path;
401 #elif defined(__NetBSD__)
403 * In all versions of NetBSD's dynamic linker as of 2013-08-12,
404 * dladdr(main) supplies a path based on argv[0], so we use
405 * /proc/curproc/exe instead.
407 * XXX - are there OS versions that support "exe" but not "curproc"
408 * or "self"? Are there any that support "self" but not "curproc"?
410 static char executable_path[PATH_MAX + 1];
413 if ((r = readlink("/proc/curproc/exe", executable_path, PATH_MAX)) == -1)
415 executable_path[r] = '\0';
416 return executable_path;
417 #elif defined(__DragonFly__)
419 * In older versions of DragonFly BSD's dynamic linker, dladdr(main)
420 * supplies a path based on argv[0], so we use /proc/curproc/file
421 * instead; it appears to be supported by all versions of DragonFly
424 static char executable_path[PATH_MAX + 1];
427 if ((r = readlink("/proc/curproc/file", executable_path, PATH_MAX)) == -1)
429 executable_path[r] = '\0';
430 return executable_path;
431 #elif (defined(sun) || defined(__sun)) && defined(HAVE_GETEXECNAME)
433 * It appears that getexecname() dates back to at least Solaris 8,
434 * but /proc/{pid}/path is first documented in the Solaris 10 documentation,
435 * so we use getexecname() if available, rather than /proc/self/path/a.out
436 * (which isn't documented, but appears to be a symlink to the
437 * executable image file).
439 return getexecname();
441 /* Fill in your favorite UN*X's code here, if there is something */
448 * Get the pathname of the directory from which the executable came,
449 * and save it for future use. Returns NULL on success, and a
450 * g_mallocated string containing an error on failure.
453 init_progfile_dir(const char *arg0
457 , int (*function_addr)(int, char **)
458 #if defined(_WIN32) || !defined(HAVE_DLADDR)
464 TCHAR prog_pathname_w[_MAX_PATH+2];
472 * Attempt to get the full pathname of the currently running
475 if (GetModuleFileName(NULL, prog_pathname_w, G_N_ELEMENTS(prog_pathname_w)) != 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
477 * XXX - Should we use g_utf16_to_utf8()?
479 prog_pathname = utf_16to8(prog_pathname_w);
481 * We got it; strip off the last component, which would be
482 * the file name of the executable, giving us the pathname
483 * of the directory where the executable resides.
485 progfile_dir = g_path_get_dirname(prog_pathname);
486 if (progfile_dir != NULL) {
487 return NULL; /* we succeeded */
490 * OK, no. What do we do now?
492 return g_strdup_printf("No \\ in executable pathname \"%s\"",
497 * Oh, well. Return an indication of the error.
499 error = GetLastError();
500 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
501 NULL, error, 0, (LPTSTR) &msg_w, 0, NULL) == 0) {
503 * Gak. We can't format the message.
505 return g_strdup_printf("GetModuleFileName failed: %u (FormatMessage failed: %u)",
506 error, GetLastError());
508 msg = utf_16to8(msg_w);
511 * "FormatMessage()" "helpfully" sticks CR/LF at the
512 * end of the message. Get rid of it.
514 msglen = strlen(msg);
516 msg[msglen - 1] = '\0';
517 msg[msglen - 2] = '\0';
519 return g_strdup_printf("GetModuleFileName failed: %s (%u)",
526 const char *execname;
531 const char *path_start, *path_end;
532 size_t path_component_len, path_len;
538 * Check whether WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set in the
539 * environment; if so, set running_in_build_directory_flag if we
540 * weren't started with special privileges. (If we were started
541 * with special privileges, it's not safe to allow the user to point
542 * us to some other directory; running_in_build_directory_flag, when
543 * set, causes us to look for plugins and the like in the build
546 if (g_getenv("WIRESHARK_RUN_FROM_BUILD_DIRECTORY") != NULL
547 && !started_with_special_privs())
548 running_in_build_directory_flag = TRUE;
550 execname = get_executable_path();
552 if (function_addr != NULL && execname == NULL) {
554 * Try to use dladdr() to find the pathname of the executable.
555 * dladdr() is not guaranteed to give you anything better than
556 * argv[0] (i.e., it might not contain a / at all, much less
557 * being an absolute path), and doesn't appear to do so on
558 * Linux, but on other platforms it could give you an absolute
559 * path and obviate the need for us to determine the absolute
563 if (dladdr((void *)function_addr, &info)) {
565 execname = info.dli_fname;
569 if (execname == NULL) {
571 * OK, guess based on argv[0].
577 * Try to figure out the directory in which the currently running
578 * program resides, given something purporting to be the executable
579 * name (from dladdr() or from the argv[0] it was started with.
580 * That might be the absolute path of the program, or a path relative
581 * to the current directory of the process that started it, or
582 * just a name for the program if it was started from the command
583 * line and was searched for in $PATH. It's not guaranteed to be
584 * any of those, however, so there are no guarantees....
586 if (execname[0] == '/') {
588 * It's an absolute path.
590 prog_pathname = g_strdup(execname);
591 } else if (strchr(execname, '/') != NULL) {
593 * It's a relative path, with a directory in it.
594 * Get the current directory, and combine it
595 * with that directory.
597 path_max = pathconf(".", _PC_PATH_MAX);
598 if (path_max == -1) {
600 * We have no idea how big a buffer to
601 * allocate for the current directory.
603 return g_strdup_printf("pathconf failed: %s\n",
606 curdir = (char *)g_malloc(path_max);
607 if (getcwd(curdir, path_max) == NULL) {
609 * It failed - give up, and just stick
613 return g_strdup_printf("getcwd failed: %s\n",
616 path = g_strdup_printf("%s/%s", curdir, execname);
618 prog_pathname = path;
621 * It's just a file name.
622 * Search the path for a file with that name
625 prog_pathname = NULL; /* haven't found it yet */
626 pathstr = g_getenv("PATH");
627 path_start = pathstr;
628 if (path_start != NULL) {
629 while (*path_start != '\0') {
630 path_end = strchr(path_start, ':');
631 if (path_end == NULL)
632 path_end = path_start + strlen(path_start);
633 path_component_len = path_end - path_start;
634 path_len = path_component_len + 1
635 + strlen(execname) + 1;
636 path = (char *)g_malloc(path_len);
637 memcpy(path, path_start, path_component_len);
638 path[path_component_len] = '\0';
639 g_strlcat(path, "/", path_len);
640 g_strlcat(path, execname, path_len);
641 if (access(path, X_OK) == 0) {
645 prog_pathname = path;
650 * That's not it. If there are more
651 * path components to test, try them.
653 if (*path_end == '\0') {
655 * There's nothing more to try.
659 if (*path_end == ':')
661 path_start = path_end;
664 if (prog_pathname == NULL) {
666 * Program not found in path.
668 return g_strdup_printf("\"%s\" not found in \"%s\"",
674 * XXX - should we pick a default?
676 return g_strdup("PATH isn't set");
681 * OK, we have what we think is the pathname
684 * First, find the last "/" in the directory,
685 * as that marks the end of the directory pathname.
687 dir_end = strrchr(prog_pathname, '/');
688 if (dir_end != NULL) {
690 * Found it. Strip off the last component,
691 * as that's the path of the program.
696 * Is there a "/.libs" at the end?
698 dir_end = strrchr(prog_pathname, '/');
699 if (dir_end != NULL) {
700 if (strcmp(dir_end, "/.libs") == 0) {
703 * Strip that off; it's an
704 * artifact of libtool.
709 * This presumably means we're run from
710 * the libtool wrapper, which probably
711 * means we're being run from the build
712 * directory. If we weren't started
713 * with special privileges, set
714 * running_in_build_directory_flag.
716 * XXX - should we check whether what
717 * follows ".libs/" begins with "lt-"?
719 if (!started_with_special_privs())
720 running_in_build_directory_flag = TRUE;
722 else if (!started_with_special_privs()) {
724 * Check for the CMake output directory. As people may name
725 * their directories "run" (really?), also check for the
726 * CMakeCache.txt file before assuming a CMake output dir.
728 if (strcmp(dir_end, "/run") == 0) {
730 cmake_file = g_strdup_printf("%.*s/CMakeCache.txt",
731 (int)(dir_end - prog_pathname),
733 if (file_exists(cmake_file))
734 running_in_build_directory_flag = TRUE;
738 if (!running_in_build_directory_flag) {
740 * Scan up the path looking for a component
741 * named "Contents". If we find it, we assume
742 * we're in a bundle, and that the top-level
743 * directory of the bundle is the one containing
746 * Not all executables are in the Contents/MacOS
747 * directory, so we can't just check for those
748 * in the path and strip them off.
750 * XXX - should we assume that it's either
751 * Contents/MacOS or Resources/bin?
753 char *component_end, *p;
755 component_end = strchr(prog_pathname, '\0');
758 while (p >= prog_pathname && *p != '/')
760 if (p == prog_pathname) {
762 * We're looking at the first component of
763 * the pathname now, so we're definitely
764 * not in a bundle, even if we're in
769 if (strncmp(p, "/Contents", component_end - p) == 0) {
771 appbundle_dir = (char *)g_malloc(p - prog_pathname + 1);
772 memcpy(appbundle_dir, prog_pathname, p - prog_pathname);
773 appbundle_dir[p - prog_pathname] = '\0';
785 * OK, we have the path we want.
787 progfile_dir = prog_pathname;
791 * This "shouldn't happen"; we apparently
792 * have no "/" in the pathname.
793 * Just free up prog_pathname.
795 retstr = g_strdup_printf("No / found in \"%s\"", prog_pathname);
796 g_free(prog_pathname);
803 * Get the directory in which the program resides.
806 get_progfile_dir(void)
812 * Get the directory in which the global configuration and data files are
815 * On Windows, we use the directory in which the executable for this
818 * On UN*X, we use the DATAFILE_DIR value supplied by the configure
819 * script, unless we think we're being run from the build directory,
820 * in which case we use the directory in which the executable for this
823 * XXX - if we ever make libwireshark a real library, used by multiple
824 * applications (more than just TShark and versions of Wireshark with
825 * various UIs), should the configuration files belong to the library
826 * (and be shared by all those applications) or to the applications?
828 * If they belong to the library, that could be done on UNIX by the
829 * configure script, but it's trickier on Windows, as you can't just
830 * use the pathname of the executable.
832 * If they belong to the application, that could be done on Windows
833 * by using the pathname of the executable, but we'd have to have it
834 * passed in as an argument, in some call, on UNIX.
836 * Note that some of those configuration files might be used by code in
837 * libwireshark, some of them might be used by dissectors (would they
838 * belong to libwireshark, the application, or a separate library?),
839 * and some of them might be used by other code (the Wireshark preferences
840 * file includes resolver preferences that control the behavior of code
841 * in libwireshark, dissector preferences, and UI preferences, for
845 get_datafile_dir(void)
847 static const char *datafile_dir = NULL;
849 if (datafile_dir != NULL)
854 * Do we have the pathname of the program? If so, assume we're
855 * running an installed version of the program. If we fail,
856 * we don't change "datafile_dir", and thus end up using the
859 * XXX - does NSIS put the installation directory into
860 * "\HKEY_LOCAL_MACHINE\SOFTWARE\Wireshark\InstallDir"?
861 * If so, perhaps we should read that from the registry,
864 if (progfile_dir != NULL) {
866 * Yes, we do; use that.
868 datafile_dir = progfile_dir;
872 * Fall back on the default installation directory.
874 datafile_dir = "C:\\Program Files\\Wireshark\\";
878 if (running_in_build_directory_flag) {
880 * We're (probably) being run from the build directory and
881 * weren't started with special privileges.
883 * (running_in_build_directory_flag is never set to TRUE
884 * if we're started with special privileges, so we need
885 * only check it; we don't need to call started_with_special_privs().)
887 * Use the top-level source directory as the datafile directory
888 * because most of our data files (radius/, COPYING) are there.
892 * When TOP_SRCDIR is defined, assume autotools where files are not
893 * copied to the build directory. This fallback location is relied on by
894 * wslua_get_actual_filename().
896 datafile_dir = TOP_SRCDIR;
899 * Otherwise assume CMake. Here, data files (console.lua, radius/, etc.)
900 * are copied to the build directory during the build.
902 datafile_dir = BUILD_TIME_DATAFILE_DIR;
906 if (g_getenv("WIRESHARK_DATA_DIR") && !started_with_special_privs()) {
908 * The user specified a different directory for data files
909 * and we aren't running with special privileges.
910 * XXX - We might be able to dispense with the priv check
912 datafile_dir = g_strdup(g_getenv("WIRESHARK_DATA_DIR"));
916 * If we're running from an app bundle and weren't started
917 * with special privileges, use the Contents/Resources/share/wireshark
918 * subdirectory of the app bundle.
920 * (appbundle_dir is not set to a non-null value if we're
921 * started with special privileges, so we need only check
922 * it; we don't need to call started_with_special_privs().)
924 else if (appbundle_dir != NULL) {
925 datafile_dir = g_strdup_printf("%s/Contents/Resources/share/wireshark",
930 datafile_dir = DATAFILE_DIR;
938 #if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
940 * Find the directory where the plugins are stored.
942 * On Windows, we use the plugin\{VERSION} subdirectory of the datafile
943 * directory, where {VERSION} is the version number of this version of
948 * if we appear to be run from the build directory, we use the
949 * "plugin" subdirectory of the datafile directory;
951 * otherwise, if the WIRESHARK_PLUGIN_DIR environment variable is
952 * set and we aren't running with special privileges, we use the
953 * value of that environment variable;
955 * otherwise, if we're running from an app bundle in macOS, we
956 * use the Contents/PlugIns/wireshark subdirectory of the app bundle;
958 * otherwise, we use the PLUGIN_INSTALL_DIR value supplied by the
961 static char *plugin_dir = NULL;
962 static char *plugin_pers_dir = NULL;
965 init_plugin_dir(void)
969 * On Windows, the data file directory is the installation
970 * directory; the plugins are stored under it.
972 * Assume we're running the installed version of Wireshark;
973 * on Windows, the data file directory is the directory
974 * in which the Wireshark binary resides.
976 plugin_dir = g_build_filename(get_datafile_dir(), "plugins", VERSION, (gchar *)NULL);
979 * Make sure that pathname refers to a directory.
981 if (test_for_directory(plugin_dir) != EISDIR) {
983 * Either it doesn't refer to a directory or it
984 * refers to something that doesn't exist.
986 * Assume that means we're running a version of
987 * Wireshark we've built in a build directory,
988 * in which case {datafile dir}\plugins is the
989 * top-level plugins source directory, and use
990 * that directory and set the "we're running in
991 * a build directory" flag, so the plugin
992 * scanner will check all subdirectories of that
993 * directory for plugins.
996 plugin_dir = g_build_filename(get_datafile_dir(), "plugins", (gchar *)NULL);
997 running_in_build_directory_flag = TRUE;
1000 if (running_in_build_directory_flag) {
1002 * We're (probably) being run from the build directory and
1003 * weren't started with special privileges, so we'll use
1004 * the "plugins" subdirectory of the directory where the program
1005 * we're running is (that's the build directory).
1007 plugin_dir = g_build_filename(get_progfile_dir(), "plugins", (gchar *)NULL);
1009 if (g_getenv("WIRESHARK_PLUGIN_DIR") && !started_with_special_privs()) {
1011 * The user specified a different directory for plugins
1012 * and we aren't running with special privileges.
1014 plugin_dir = g_strdup(g_getenv("WIRESHARK_PLUGIN_DIR"));
1018 * If we're running from an app bundle and weren't started
1019 * with special privileges, use the Contents/PlugIns/wireshark
1020 * subdirectory of the app bundle.
1022 * (appbundle_dir is not set to a non-null value if we're
1023 * started with special privileges, so we need only check
1024 * it; we don't need to call started_with_special_privs().)
1026 else if (appbundle_dir != NULL) {
1027 plugin_dir = g_build_filename(appbundle_dir, "Contents/PlugIns/wireshark", (gchar *)NULL);
1031 plugin_dir = g_strdup(PLUGIN_INSTALL_DIR);
1036 #endif /* HAVE_PLUGINS || HAVE_LUA */
1039 init_plugin_pers_dir(void)
1041 plugin_pers_dir = get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
1045 * Get the directory in which the plugins are stored.
1048 get_plugin_dir(void)
1050 #if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
1051 if (!plugin_dir) init_plugin_dir();
1058 /* Get the personal plugin dir */
1060 get_plugins_pers_dir(void)
1062 if (!plugin_pers_dir)
1063 init_plugin_pers_dir();
1064 return plugin_pers_dir;
1067 #if defined(HAVE_EXTCAP)
1069 * Find the directory where the extcap hooks are stored.
1071 * On Windows, we use the "extcap" subdirectory of the datafile directory.
1073 * On UN*X, we use the EXTCAP_DIR value supplied by the configure
1074 * script, unless we think we're being run from the build directory,
1075 * in which case we use the "extcap" subdirectory of the datafile directory.
1077 * In both cases, we then use the subdirectory of that directory whose
1078 * name is the version number.
1080 * XXX - if we think we're being run from the build directory, perhaps we
1081 * should have the extcap code not look in the version subdirectory
1082 * of the extcap directory, but look in all of the subdirectories
1083 * of the extcap directory, so it can just fetch the extcap hooks built
1084 * as part of the build process.
1086 static char *extcap_dir = NULL;
1088 static void init_extcap_dir(void) {
1090 const char *alt_extcap_path;
1093 * On Windows, the data file directory is the installation
1094 * directory; the extcap hooks are stored under it.
1096 * Assume we're running the installed version of Wireshark;
1097 * on Windows, the data file directory is the directory
1098 * in which the Wireshark binary resides.
1100 alt_extcap_path = g_getenv("WIRESHARK_EXTCAP_DIR");
1101 if (alt_extcap_path) {
1103 * The user specified a different directory for extcap hooks.
1105 extcap_dir = g_strdup(alt_extcap_path);
1107 extcap_dir = g_build_filename(get_datafile_dir(), "extcap", (gchar *)NULL);
1110 if (running_in_build_directory_flag) {
1112 * We're (probably) being run from the build directory and
1113 * weren't started with special privileges, so we'll use
1114 * the "extcap hooks" subdirectory of the directory where the program
1115 * we're running is (that's the build directory).
1117 extcap_dir = g_build_filename(get_progfile_dir(), "extcap", (gchar *)NULL);
1119 if (g_getenv("WIRESHARK_EXTCAP_DIR") && !started_with_special_privs()) {
1121 * The user specified a different directory for extcap hooks
1122 * and we aren't running with special privileges.
1124 extcap_dir = g_strdup(g_getenv("WIRESHARK_EXTCAP_DIR"));
1128 * If we're running from an app bundle and weren't started
1129 * with special privileges, use the Contents/MacOS/extcap
1130 * subdirectory of the app bundle.
1132 * (appbundle_dir is not set to a non-null value if we're
1133 * started with special privileges, so we need only check
1134 * it; we don't need to call started_with_special_privs().)
1136 else if (appbundle_dir != NULL) {
1137 extcap_dir = g_build_filename(appbundle_dir, "Contents/MacOS/extcap", (gchar *)NULL);
1141 extcap_dir = g_strdup(EXTCAP_DIR);
1146 #endif /* HAVE_EXTCAP */
1149 * Get the directory in which the extcap hooks are stored.
1151 * XXX - A fix instead of HAVE_EXTCAP must be found
1154 get_extcap_dir(void) {
1155 #if defined(HAVE_EXTCAP)
1165 * Get the flag indicating whether we're running from a build
1169 running_in_build_directory(void)
1171 return running_in_build_directory_flag;
1175 * Get the directory in which files that, at least on UNIX, are
1176 * system files (such as "/etc/ethers") are stored; on Windows,
1177 * there's no "/etc" directory, so we get them from the global
1178 * configuration and data file directory.
1181 get_systemfile_dir(void)
1184 return get_datafile_dir();
1191 set_profile_name(const gchar *profilename)
1193 g_free (persconfprofile);
1195 if (profilename && strlen(profilename) > 0 &&
1196 strcmp(profilename, DEFAULT_PROFILE) != 0) {
1197 persconfprofile = g_strdup (profilename);
1199 /* Default Profile */
1200 persconfprofile = NULL;
1205 get_profile_name(void)
1207 if (persconfprofile) {
1208 return persconfprofile;
1210 return DEFAULT_PROFILE;
1215 is_default_profile(void)
1217 return (!persconfprofile || strcmp(persconfprofile, DEFAULT_PROFILE) == 0) ? TRUE : FALSE;
1221 has_global_profiles(void)
1225 gchar *global_dir = get_global_profiles_dir();
1227 gboolean has_global = FALSE;
1229 if ((test_for_directory(global_dir) == EISDIR) &&
1230 ((dir = ws_dir_open(global_dir, 0, NULL)) != NULL))
1232 while ((file = ws_dir_read_name(dir)) != NULL) {
1233 filename = g_strdup_printf ("%s%s%s", global_dir, G_DIR_SEPARATOR_S,
1234 ws_dir_get_name(file));
1235 if (test_for_directory(filename) == EISDIR) {
1249 profile_store_persconffiles(gboolean store)
1252 profile_files = g_hash_table_new (g_str_hash, g_str_equal);
1254 do_store_persconffiles = store;
1258 * Get the directory in which personal configuration files reside.
1260 * On Windows, it's "Wireshark", under %APPDATA% or, if %APPDATA% isn't set,
1261 * it's "%USERPROFILE%\Application Data" (which is what %APPDATA% normally
1262 * is on Windows 2000).
1264 * On UNIX-compatible systems, we first look in XDG_CONFIG_HOME/wireshark
1265 * and, if that doesn't exist, ~/.wireshark, for backwards compatibility.
1266 * If neither exists, we use XDG_CONFIG_HOME/wireshark, so that the directory
1267 * is initially created as XDG_CONFIG_HOME/wireshark. We use that regardless
1268 * of whether the user is running under an XDG desktop or not, so that
1269 * if the user's home directory is on a server and shared between
1270 * different desktop environments on different machines, they can all
1271 * share the same configuration file directory.
1273 * XXX - what about stuff that shouldn't be shared between machines,
1274 * such as plugins in the form of shared loadable images?
1277 get_persconffile_dir_no_profile(void)
1282 char *xdg_path, *path;
1284 const char *homedir;
1287 /* Return the cached value, if available */
1288 if (persconffile_dir != NULL)
1289 return persconffile_dir;
1293 * See if the user has selected an alternate environment.
1295 env = g_getenv("WIRESHARK_APPDATA");
1297 persconffile_dir = g_strdup(env);
1298 return persconffile_dir;
1302 * Use %APPDATA% or %USERPROFILE%, so that configuration
1303 * files are stored in the user profile, rather than in
1304 * the home directory. The Windows convention is to store
1305 * configuration information in the user profile, and doing
1306 * so means you can use Wireshark even if the home directory
1307 * is an inaccessible network drive.
1309 env = g_getenv("APPDATA");
1312 * Concatenate %APPDATA% with "\Wireshark".
1314 persconffile_dir = g_build_filename(env, "Wireshark", NULL);
1315 return persconffile_dir;
1319 * OK, %APPDATA% wasn't set, so use %USERPROFILE%\Application Data.
1321 env = g_getenv("USERPROFILE");
1323 persconffile_dir = g_build_filename(env, "Application Data", "Wireshark", NULL);
1324 return persconffile_dir;
1328 * Give up and use "C:".
1330 persconffile_dir = g_build_filename("C:", "Wireshark", NULL);
1331 return persconffile_dir;
1334 * Check if XDG_CONFIG_HOME/wireshark exists and is a directory.
1336 xdg_path = g_build_filename(g_get_user_config_dir(), "wireshark", NULL);
1337 if (g_file_test(xdg_path, G_FILE_TEST_IS_DIR)) {
1338 persconffile_dir = xdg_path;
1339 return persconffile_dir;
1343 * It doesn't exist, or it does but isn't a directory, so try
1346 * If $HOME is set, use that for ~.
1348 * (Note: before GLib 2.36, g_get_home_dir() didn't look at $HOME,
1349 * but we always want to do so, so we don't use g_get_home_dir().)
1351 homedir = g_getenv("HOME");
1352 if (homedir == NULL) {
1356 * Get their home directory from the password file.
1357 * If we can't even find a password file entry for them,
1360 pwd = getpwuid(getuid());
1362 homedir = pwd->pw_dir;
1367 path = g_build_filename(homedir, ".wireshark", NULL);
1368 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
1370 persconffile_dir = path;
1371 return persconffile_dir;
1375 * Neither are directories that exist; use the XDG path, so we'll
1376 * create that as necessary.
1379 persconffile_dir = xdg_path;
1380 return persconffile_dir;
1385 set_persconffile_dir(const char *p)
1387 g_free(persconffile_dir);
1388 persconffile_dir = g_strdup(p);
1392 get_profiles_dir(void)
1394 return g_strdup_printf ("%s%s%s", get_persconffile_dir_no_profile (),
1395 G_DIR_SEPARATOR_S, PROFILES_DIR);
1399 create_profiles_dir(char **pf_dir_path_return)
1405 * Create the "Default" personal configuration files directory, if necessary.
1407 if (create_persconffile_profile (NULL, pf_dir_path_return) == -1) {
1412 * Check if profiles directory exists.
1413 * If not then create it.
1415 pf_dir_path = get_profiles_dir ();
1416 if (ws_stat64(pf_dir_path, &s_buf) != 0) {
1417 if (errno != ENOENT) {
1418 /* Some other problem; give up now. */
1419 *pf_dir_path_return = pf_dir_path;
1424 * It doesn't exist; try to create it.
1426 int ret = ws_mkdir(pf_dir_path, 0755);
1428 *pf_dir_path_return = pf_dir_path;
1432 g_free(pf_dir_path);
1438 get_global_profiles_dir(void)
1440 return g_strdup_printf ("%s%s%s", get_datafile_dir(),
1441 G_DIR_SEPARATOR_S, PROFILES_DIR);
1445 get_persconffile_dir(const gchar *profilename)
1447 char *persconffile_profile_dir = NULL, *profile_dir;
1449 if (profilename && strlen(profilename) > 0 &&
1450 strcmp(profilename, DEFAULT_PROFILE) != 0) {
1451 profile_dir = get_profiles_dir();
1452 persconffile_profile_dir = g_strdup_printf ("%s%s%s", profile_dir,
1453 G_DIR_SEPARATOR_S, profilename);
1454 g_free(profile_dir);
1456 persconffile_profile_dir = g_strdup (get_persconffile_dir_no_profile ());
1459 return persconffile_profile_dir;
1463 profile_exists(const gchar *profilename, gboolean global)
1465 gchar *path = NULL, *global_path;
1467 global_path = get_global_profiles_dir();
1468 path = g_strdup_printf ("%s%s%s", global_path,
1469 G_DIR_SEPARATOR_S, profilename);
1470 g_free(global_path);
1471 if (test_for_directory (path) == EISDIR) {
1476 path = get_persconffile_dir (profilename);
1477 if (test_for_directory (path) == EISDIR) {
1488 delete_directory (const char *directory, char **pf_dir_path_return)
1495 if ((dir = ws_dir_open(directory, 0, NULL)) != NULL) {
1496 while ((file = ws_dir_read_name(dir)) != NULL) {
1497 filename = g_strdup_printf ("%s%s%s", directory, G_DIR_SEPARATOR_S,
1498 ws_dir_get_name(file));
1499 if (test_for_directory(filename) != EISDIR) {
1500 ret = ws_remove(filename);
1503 /* The user has manually created a directory in the profile directory */
1504 /* I do not want to delete the directory recursively yet */
1505 ret = delete_directory (filename, pf_dir_path_return);
1509 *pf_dir_path_return = filename;
1517 if (ret == 0 && (ret = ws_remove(directory)) != 0) {
1518 *pf_dir_path_return = g_strdup (directory);
1525 reset_default_profile(char **pf_dir_path_return)
1527 char *profile_dir = get_persconffile_dir(NULL);
1528 gchar *filename, *del_file;
1529 GList *files, *file;
1532 files = g_hash_table_get_keys(profile_files);
1533 file = g_list_first(files);
1535 filename = (gchar *)file->data;
1536 del_file = g_strdup_printf("%s%s%s", profile_dir, G_DIR_SEPARATOR_S, filename);
1538 if (file_exists(del_file)) {
1539 ret = ws_remove(del_file);
1541 *pf_dir_path_return = profile_dir;
1548 file = g_list_next(file);
1552 g_free(profile_dir);
1557 delete_persconffile_profile(const char *profilename, char **pf_dir_path_return)
1559 if (strcmp(profilename, DEFAULT_PROFILE) == 0) {
1560 return reset_default_profile(pf_dir_path_return);
1563 char *profile_dir = get_persconffile_dir(profilename);
1566 if (test_for_directory (profile_dir) == EISDIR) {
1567 ret = delete_directory (profile_dir, pf_dir_path_return);
1570 g_free(profile_dir);
1575 rename_persconffile_profile(const char *fromname, const char *toname,
1576 char **pf_from_dir_path_return, char **pf_to_dir_path_return)
1578 char *from_dir = get_persconffile_dir(fromname);
1579 char *to_dir = get_persconffile_dir(toname);
1582 ret = ws_rename (from_dir, to_dir);
1584 *pf_from_dir_path_return = from_dir;
1585 *pf_to_dir_path_return = to_dir;
1596 * Create the directory that holds personal configuration files, if
1597 * necessary. If we attempted to create it, and failed, return -1 and
1598 * set "*pf_dir_path_return" to the pathname of the directory we failed
1599 * to create (it's g_mallocated, so our caller should free it); otherwise,
1603 create_persconffile_profile(const char *profilename, char **pf_dir_path_return)
1607 char *pf_dir_path_copy, *pf_dir_parent_path;
1608 size_t pf_dir_parent_path_len;
1616 * Create the personal profiles directory, if necessary.
1618 if (create_profiles_dir(pf_dir_path_return) == -1) {
1623 pf_dir_path = get_persconffile_dir(profilename);
1624 if (ws_stat64(pf_dir_path, &s_buf) != 0) {
1625 if (errno != ENOENT) {
1626 /* Some other problem; give up now. */
1627 *pf_dir_path_return = pf_dir_path;
1632 * Does the parent directory of that directory
1633 * exist? %APPDATA% may not exist even though
1634 * %USERPROFILE% does.
1636 * We check for the existence of the directory
1637 * by first checking whether the parent directory
1638 * is just a drive letter and, if it's not, by
1639 * doing a "stat()" on it. If it's a drive letter,
1640 * or if the "stat()" succeeds, we assume it exists.
1642 pf_dir_path_copy = g_strdup(pf_dir_path);
1643 pf_dir_parent_path = get_dirname(pf_dir_path_copy);
1644 pf_dir_parent_path_len = strlen(pf_dir_parent_path);
1645 if (pf_dir_parent_path_len > 0
1646 && pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
1647 && ws_stat64(pf_dir_parent_path, &s_buf) != 0) {
1649 * Not a drive letter and the stat() failed.
1651 if (errno != ENOENT) {
1652 /* Some other problem; give up now. */
1653 *pf_dir_path_return = pf_dir_path;
1655 g_free(pf_dir_path_copy);
1660 * No, it doesn't exist - make it first.
1662 ret = ws_mkdir(pf_dir_parent_path, 0755);
1664 *pf_dir_path_return = pf_dir_parent_path;
1666 g_free(pf_dir_path);
1671 g_free(pf_dir_path_copy);
1672 ret = ws_mkdir(pf_dir_path, 0755);
1674 ret = g_mkdir_with_parents(pf_dir_path, 0755);
1678 * Something with that pathname exists; if it's not
1679 * a directory, we'll get an error if we try to put
1680 * something in it, so we don't fail here, we wait
1681 * for that attempt fo fail.
1686 *pf_dir_path_return = pf_dir_path;
1688 g_free(pf_dir_path);
1694 create_persconffile_dir(char **pf_dir_path_return)
1696 return create_persconffile_profile(persconfprofile, pf_dir_path_return);
1700 copy_persconffile_profile(const char *toname, const char *fromname, gboolean from_global,
1701 char **pf_filename_return, char **pf_to_dir_path_return, char **pf_from_dir_path_return)
1704 gchar *to_dir = get_persconffile_dir(toname);
1705 gchar *filename, *from_file, *to_file, *global_path;
1706 GList *files, *file;
1709 if (strcmp(fromname, DEFAULT_PROFILE) == 0) {
1710 from_dir = get_global_profiles_dir();
1712 global_path = get_global_profiles_dir();
1713 from_dir = g_strdup_printf ("%s%s%s", global_path, G_DIR_SEPARATOR_S, fromname);
1714 g_free(global_path);
1717 from_dir = get_persconffile_dir(fromname);
1720 files = g_hash_table_get_keys(profile_files);
1721 file = g_list_first(files);
1723 filename = (gchar *)file->data;
1724 from_file = g_strdup_printf ("%s%s%s", from_dir, G_DIR_SEPARATOR_S, filename);
1725 to_file = g_strdup_printf ("%s%s%s", to_dir, G_DIR_SEPARATOR_S, filename);
1727 if (file_exists(from_file) && !copy_file_binary_mode(from_file, to_file)) {
1728 *pf_filename_return = g_strdup(filename);
1729 *pf_to_dir_path_return = to_dir;
1730 *pf_from_dir_path_return = from_dir;
1739 file = g_list_next(file);
1742 g_list_free (files);
1750 * Get the (default) directory in which personal data is stored.
1752 * On Win32, this is the "My Documents" folder in the personal profile.
1753 * On UNIX this is simply the current directory.
1755 /* XXX - should this and the get_home_dir() be merged? */
1757 get_persdatafile_dir(void)
1760 TCHAR tszPath[MAX_PATH];
1762 /* Return the cached value, if available */
1763 if (persdatafile_dir != NULL)
1764 return persdatafile_dir;
1767 * Hint: SHGetFolderPath is not available on MSVC 6 - without
1770 if (SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL, FALSE)) {
1771 persdatafile_dir = g_utf16_to_utf8(tszPath, -1, NULL, NULL, NULL);
1772 return persdatafile_dir;
1782 set_persdatafile_dir(const char *p)
1784 g_free(persdatafile_dir);
1785 persdatafile_dir = g_strdup(p);
1790 * Returns the user's home directory on Win32.
1795 static const char *home = NULL;
1796 const char *homedrive, *homepath;
1800 /* Return the cached value, if available */
1805 * XXX - should we use USERPROFILE anywhere in this process?
1806 * Is there a chance that it might be set but one or more of
1807 * HOMEDRIVE or HOMEPATH isn't set?
1809 homedrive = g_getenv("HOMEDRIVE");
1810 if (homedrive != NULL) {
1811 homepath = g_getenv("HOMEPATH");
1812 if (homepath != NULL) {
1814 * This is cached, so we don't need to worry about
1815 * allocating multiple ones of them.
1817 homestring = g_strdup_printf("%s%s", homedrive, homepath);
1820 * Trim off any trailing slash or backslash.
1822 lastsep = find_last_pathname_separator(homestring);
1823 if (lastsep != NULL && *(lastsep + 1) == '\0') {
1825 * Last separator is the last character
1826 * in the string. Nuke it.
1835 * Give up and use C:.
1845 * Construct the path name of a personal configuration file, given the
1848 * On Win32, if "for_writing" is FALSE, we check whether the file exists
1849 * and, if not, construct a path name relative to the ".wireshark"
1850 * subdirectory of the user's home directory, and check whether that
1851 * exists; if it does, we return that, so that configuration files
1852 * from earlier versions can be read.
1854 * The returned file name was g_malloc()'d so it must be g_free()d when the
1855 * caller is done with it.
1858 get_persconffile_path(const char *filename, gboolean from_profile)
1860 char *path, *dir = NULL;
1862 if (do_store_persconffiles && from_profile && !g_hash_table_lookup (profile_files, filename)) {
1863 /* Store filenames so we know which filenames belongs to a configuration profile */
1864 g_hash_table_insert (profile_files, g_strdup(filename), g_strdup(filename));
1868 dir = get_persconffile_dir(persconfprofile);
1870 dir = get_persconffile_dir(NULL);
1872 path = g_build_filename(dir, filename, NULL);
1879 * Construct the path name of a global configuration file, given the
1882 * The returned file name was g_malloc()'d so it must be g_free()d when the
1883 * caller is done with it.
1886 get_datafile_path(const char *filename)
1888 if (running_in_build_directory_flag &&
1889 (!strcmp(filename, "AUTHORS-SHORT") ||
1890 !strcmp(filename, "hosts"))) {
1891 /* We're running in the build directory and the requested file is a
1892 * generated (or a test) file. Return the file name in the build
1893 * directory (not in the source/data directory).
1894 * (Oh the things we do to keep the source directory pristine...)
1896 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_progfile_dir(), filename);
1898 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(), filename);
1903 * Return an error message for UNIX-style errno indications on open or
1904 * create operations.
1907 file_open_error_message(int err, gboolean for_writing)
1910 static char errmsg_errno[1024+1];
1916 errmsg = "The path to the file \"%s\" doesn't exist.";
1918 errmsg = "The file \"%s\" doesn't exist.";
1923 errmsg = "You don't have permission to create or write to the file \"%s\".";
1925 errmsg = "You don't have permission to read the file \"%s\".";
1929 errmsg = "\"%s\" is a directory (folder), not a file.";
1933 errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
1938 errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
1943 errmsg = "The file \"%s\" could not be created because an invalid filename was specified.";
1948 /* XXX Make sure we truncate on a character boundary. */
1949 errmsg = "The file name \"%.80s" UTF8_HORIZONTAL_ELLIPSIS "\" is too long.";
1955 * The problem probably has nothing to do with how much RAM the
1956 * user has on their machine, so don't confuse them by saying
1957 * "memory". The problem is probably either virtual address
1958 * space or swap space.
1960 #if GLIB_SIZEOF_VOID_P == 4
1962 * ILP32; we probably ran out of virtual address space.
1964 #define ENOMEM_REASON "it can't be handled by a 32-bit application"
1967 * LP64 or LLP64; we probably ran out of swap space.
1971 * You need to make the pagefile bigger.
1973 #define ENOMEM_REASON "the pagefile is too small"
1974 #elif defined(__APPLE__)
1976 * dynamic_pager couldn't, or wouldn't, create more swap files.
1978 #define ENOMEM_REASON "your system ran out of swap file space"
1981 * Either you have a fixed swap partition or a fixed swap file,
1982 * and it needs to be made bigger.
1984 * This is UN*X, but it's not macOS, so we assume the user is
1987 #define ENOMEM_REASON "your system is out of swap space"
1989 #endif /* GLIB_SIZEOF_VOID_P == 4 */
1991 errmsg = "The file \"%s\" could not be created because " ENOMEM_REASON ".";
1993 errmsg = "The file \"%s\" could not be opened because " ENOMEM_REASON ".";
1997 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1998 "The file \"%%s\" could not be %s: %s.",
1999 for_writing ? "created" : "opened",
2001 errmsg = errmsg_errno;
2008 * Return an error message for UNIX-style errno indications on write
2012 file_write_error_message(int err)
2015 static char errmsg_errno[1024+1];
2020 errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
2025 errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
2030 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
2031 "An error occurred while writing to the file \"%%s\": %s.",
2033 errmsg = errmsg_errno;
2041 file_exists(const char *fname)
2043 ws_statb64 file_stat;
2049 #if defined(_MSC_VER) && _MSC_VER < 1900
2052 * This is a bit tricky on win32. The st_ino field is documented as:
2053 * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
2054 * but it *is* set to zero if stat() returns without an error,
2055 * so this is working, but maybe not quite the way expected. ULFL
2057 file_stat.st_ino = 1; /* this will make things work if an error occurred */
2058 ws_stat64(fname, &file_stat);
2059 if (file_stat.st_ino == 0) {
2065 if (ws_stat64(fname, &file_stat) != 0 && errno == ENOENT) {
2074 * Check that the from file is not the same as to file
2075 * We do it here so we catch all cases ...
2076 * Unfortunately, the file requester gives us an absolute file
2077 * name and the read file name may be relative (if supplied on
2078 * the command line), so we can't just compare paths. From Joerg Mayer.
2081 files_identical(const char *fname1, const char *fname2)
2083 /* Two different implementations, because:
2085 * - _fullpath is not available on UN*X, so we can't get full
2086 * paths and compare them (which wouldn't work with hard links
2089 * - st_ino isn't filled in with a meaningful value on Windows.
2092 char full1[MAX_PATH], full2[MAX_PATH];
2095 * Get the absolute full paths of the file and compare them.
2096 * That won't work if you have hard links, but those aren't
2097 * much used on Windows, even though NTFS supports them.
2099 * XXX - will _fullpath work with UNC?
2101 if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
2105 if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
2109 if(strcmp(full1, full2) == 0) {
2115 ws_statb64 filestat1, filestat2;
2118 * Compare st_dev and st_ino.
2120 if (ws_stat64(fname1, &filestat1) == -1)
2121 return FALSE; /* can't get info about the first file */
2122 if (ws_stat64(fname2, &filestat2) == -1)
2123 return FALSE; /* can't get info about the second file */
2124 return (filestat1.st_dev == filestat2.st_dev &&
2125 filestat1.st_ino == filestat2.st_ino);
2130 * Copy a file in binary mode, for those operating systems that care about
2131 * such things. This should be OK for all files, even text files, as
2132 * we'll copy the raw bytes, and we don't look at the bytes as we copy
2135 * Returns TRUE on success, FALSE on failure. If a failure, it also
2136 * displays a simple dialog window with the error message.
2139 copy_file_binary_mode(const char *from_filename, const char *to_filename)
2141 int from_fd, to_fd, err;
2142 ssize_t nread, nwritten;
2145 /* Copy the raw bytes of the file. */
2146 from_fd = ws_open(from_filename, O_RDONLY | O_BINARY, 0000 /* no creation so don't matter */);
2148 report_open_failure(from_filename, errno, FALSE);
2152 /* Use open() instead of creat() so that we can pass the O_BINARY
2153 flag, which is relevant on Win32; it appears that "creat()"
2154 may open the file in text mode, not binary mode, but we want
2155 to copy the raw bytes of the file, so we need the output file
2156 to be open in binary mode. */
2157 to_fd = ws_open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
2159 report_open_failure(to_filename, errno, TRUE);
2164 #define FS_READ_SIZE 65536
2165 pd = (guint8 *)g_malloc(FS_READ_SIZE);
2166 while ((nread = ws_read(from_fd, pd, FS_READ_SIZE)) > 0) {
2167 nwritten = ws_write(to_fd, pd, nread);
2168 if (nwritten < nread) {
2172 err = WTAP_ERR_SHORT_WRITE;
2173 report_write_failure(to_filename, err);
2181 report_read_failure(from_filename, err);
2187 if (ws_close(to_fd) < 0) {
2188 report_write_failure(to_filename, errno);
2202 data_file_url(const gchar *filename)
2207 /* Absolute path? */
2208 if(g_path_is_absolute(filename)) {
2209 file_path = g_strdup(filename);
2210 } else if(running_in_build_directory()) {
2211 file_path = g_strdup_printf("%s/doc/%s", get_datafile_dir(), filename);
2213 file_path = g_strdup_printf("%s/%s", get_datafile_dir(), filename);
2216 /* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
2218 /* convert filename to uri */
2219 uri = g_filename_to_uri(file_path, NULL, NULL);
2227 g_free(persconffile_dir);
2228 persconffile_dir = NULL;
2229 g_free(persdatafile_dir);
2230 persdatafile_dir = NULL;
2231 g_free(persconfprofile);
2232 persconfprofile = NULL;
2233 g_free(progfile_dir);
2234 progfile_dir = NULL;
2235 #if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
2238 g_free(plugin_pers_dir);
2239 plugin_pers_dir = NULL;
2253 * indent-tabs-mode: nil
2256 * ex: set shiftwidth=4 tabstop=8 expandtab:
2257 * :indentSize=4:tabSize=8:noTabs=true: