3 * (Originally part of the Wiretap Library, now part of the Wireshark
5 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * File wrapper functions to replace the file functions from GLib like
27 * With MSVC, code using the C support library from one version of MSVC
28 * cannot use file descriptors or FILE *'s returned from code using
29 * the C support library from another version of MSVC.
31 * We therefore provide our own versions of the routines to open files,
32 * so that they're built to use the same C support library as our code
35 * (If both were built to use the Universal CRT:
37 * http://blogs.msdn.com/b/vcblog/archive/2015/03/03/introducing-the-universal-crt.aspx
39 * this would not be a problem.)
41 * DO NOT USE THESE FUNCTIONS DIRECTLY, USE ws_open() AND ALIKE FUNCTIONS
42 * FROM file_util.h INSTEAD!!!
44 * The following code is stripped down code copied from the GLib file
45 * glib/gstdio.h - stripped down because this is used only on Windows
46 * and we use only wide char functions.
48 * In addition, we have our own ws_stdio_stat64(), which uses
49 * _wstati64(), so that we can get file sizes for files > 4 GB in size.
51 * XXX - is there any reason why we supply our own versions of routines
52 * that *don't* return file descriptors, other than ws_stdio_stat64()?
53 * Is there an issue with UTF-16 support in _wmkdir() with some versions
54 * of the C runtime, so that if GLib is built to use that version, it
55 * won't handle UTF-16 paths?
59 #error "This is only for Windows"
72 #include "file_util.h"
74 static gchar *program_path = NULL;
75 static gchar *system_path = NULL;
79 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
80 * @flags: as in open()
83 * A wrapper for the POSIX open() function. The open() function is
84 * used to convert a pathname into a file descriptor. Note that on
85 * POSIX systems file descriptors are implemented by the operating
86 * system. On Windows, it's the C library that implements open() and
87 * file descriptors. The actual Windows API for opening files is
88 * something different.
90 * See the C library manual for more details about open().
92 * Returns: a new file descriptor, or -1 if an error occurred. The
93 * return value can be used exactly like the return value from open().
98 ws_stdio_open (const gchar *filename,
102 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
106 if (wfilename == NULL)
112 retval = _wopen (wfilename, flags, mode);
124 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
125 * @newfilename: a pathname in the GLib file name encoding
127 * A wrapper for the POSIX rename() function. The rename() function
128 * renames a file, moving it between directories if required.
130 * See your C library manual for more details about how rename() works
131 * on your system. Note in particular that on Win9x it is not possible
132 * to rename a file if a file with the new name already exists. Also
133 * it is not possible in general on Windows to rename an open file.
135 * Returns: 0 if the renaming succeeded, -1 if an error occurred
140 ws_stdio_rename (const gchar *oldfilename,
141 const gchar *newfilename)
143 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
144 wchar_t *wnewfilename;
148 if (woldfilename == NULL)
154 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
156 if (wnewfilename == NULL)
158 g_free (woldfilename);
163 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
168 switch (GetLastError ())
170 #define CASE(a,b) case ERROR_##a: save_errno = b; break
171 CASE (FILE_NOT_FOUND, ENOENT);
172 CASE (PATH_NOT_FOUND, ENOENT);
173 CASE (ACCESS_DENIED, EACCES);
174 CASE (NOT_SAME_DEVICE, EXDEV);
175 CASE (LOCK_VIOLATION, EACCES);
176 CASE (SHARING_VIOLATION, EACCES);
177 CASE (FILE_EXISTS, EEXIST);
178 CASE (ALREADY_EXISTS, EEXIST);
180 default: save_errno = EIO;
184 g_free (woldfilename);
185 g_free (wnewfilename);
193 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
194 * @mode: permissions to use for the newly created directory
196 * A wrapper for the POSIX mkdir() function. The mkdir() function
197 * attempts to create a directory with the given name and permissions.
199 * See the C library manual for more details about mkdir().
201 * Returns: 0 if the directory was successfully created, -1 if an error
207 ws_stdio_mkdir (const gchar *filename,
210 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
214 if (wfilename == NULL)
220 retval = _wmkdir (wfilename);
231 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
232 * @buf: a pointer to a <structname>stat</structname> struct, which
233 * will be filled with the file information
235 * A wrapper for the POSIX stat() function. The stat() function
236 * returns information about a file.
238 * See the C library manual for more details about stat().
240 * Returns: 0 if the information was successfully retrieved, -1 if an error
246 ws_stdio_stat64 (const gchar *filename,
249 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
254 if (wfilename == NULL)
260 len = wcslen (wfilename);
261 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
264 (!g_path_is_absolute (filename) || len > (size_t) (g_path_skip_root (filename) - filename)))
265 wfilename[len] = '\0';
267 retval = _wstati64 (wfilename, buf);
277 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
279 * A wrapper for the POSIX unlink() function. The unlink() function
280 * deletes a name from the filesystem. If this was the last link to the
281 * file and no processes have it opened, the diskspace occupied by the
284 * See your C library manual for more details about unlink(). Note
285 * that on Windows, it is in general not possible to delete files that
286 * are open to some process, or mapped into memory.
288 * Returns: 0 if the name was successfully deleted, -1 if an error
295 ws_stdio_unlink (const gchar *filename)
297 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
301 if (wfilename == NULL)
307 retval = _wunlink (wfilename);
318 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
320 * A wrapper for the POSIX remove() function. The remove() function
321 * deletes a name from the filesystem.
323 * See your C library manual for more details about how remove() works
324 * on your system. On Unix, remove() removes also directories, as it
325 * calls unlink() for files and rmdir() for directories. On Windows,
326 * although remove() in the C library only works for files, this
327 * function tries first remove() and then if that fails rmdir(), and
328 * thus works for both files and directories. Note however, that on
329 * Windows, it is in general not possible to remove a file that is
330 * open to some process, or mapped into memory.
332 * If this function fails on Windows you can't infer too much from the
333 * errno value. rmdir() is tried regardless of what caused remove() to
334 * fail. Any errno value set by remove() will be overwritten by that
337 * Returns: 0 if the file was successfully removed, -1 if an error
343 ws_stdio_remove (const gchar *filename)
345 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
349 if (wfilename == NULL)
355 retval = _wremove (wfilename);
357 retval = _wrmdir (wfilename);
368 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
369 * @mode: a string describing the mode in which the file should be
372 * A wrapper for the POSIX fopen() function. The fopen() function opens
373 * a file and associates a new stream with it.
375 * See the C library manual for more details about fopen().
377 * Returns: A <type>FILE</type> pointer if the file was successfully
378 * opened, or %NULL if an error occurred
383 ws_stdio_fopen (const gchar *filename,
386 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
391 if (wfilename == NULL)
397 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
406 retval = _wfopen (wfilename, wmode);
418 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
419 * @mode: a string describing the mode in which the file should be
421 * @stream: an existing stream which will be reused, or %NULL
423 * A wrapper for the POSIX freopen() function. The freopen() function
424 * opens a file and associates it with an existing stream.
426 * See the C library manual for more details about freopen().
428 * Returns: A <type>FILE</type> pointer if the file was successfully
429 * opened, or %NULL if an error occurred.
434 ws_stdio_freopen (const gchar *filename,
438 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
443 if (wfilename == NULL)
449 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
458 retval = _wfreopen (wfilename, wmode, stream);
471 init_dll_load_paths()
473 TCHAR path_w[MAX_PATH];
475 if (program_path && system_path)
478 /* XXX - Duplicate code in filesystem.c:init_progfile_dir */
479 if (GetModuleFileName(NULL, path_w, MAX_PATH) == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
485 app_path = g_utf16_to_utf8(path_w, -1, NULL, NULL, NULL);
486 /* We could use PathRemoveFileSpec here but we'd have to link to Shlwapi.dll */
487 program_path = g_path_get_dirname(app_path);
491 if (GetSystemDirectory(path_w, MAX_PATH) == 0) {
496 system_path = g_utf16_to_utf8(path_w, -1, NULL, NULL, NULL);
499 if (program_path && system_path)
506 ws_init_dll_search_path()
508 gboolean dll_dir_set = FALSE;
509 wchar_t *program_path_w;
511 typedef BOOL (WINAPI *SetDllDirectoryHandler)(LPCTSTR);
512 SetDllDirectoryHandler PSetDllDirectory;
514 PSetDllDirectory = (SetDllDirectoryHandler) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "SetDllDirectoryW");
515 if (PSetDllDirectory) {
516 dll_dir_set = PSetDllDirectory(_T(""));
519 if (!dll_dir_set && init_dll_load_paths()) {
520 program_path_w = g_utf8_to_utf16(program_path, -1, NULL, NULL, NULL);
521 SetCurrentDirectory(program_path_w);
522 g_free(program_path_w);
529 * Internally g_module_open uses LoadLibrary on Windows and returns an
530 * HMODULE cast to a GModule *. However there's no guarantee that this
531 * will always be the case, so we call LoadLibrary and g_module_open
536 ws_load_library(gchar *library_name)
539 wchar_t *full_path_w;
542 if (!init_dll_load_paths() || !library_name)
545 /* First try the program directory */
546 full_path = g_module_build_path(program_path, library_name);
547 full_path_w = g_utf8_to_utf16(full_path, -1, NULL, NULL, NULL);
549 if (full_path && full_path_w) {
550 dll_h = LoadLibraryW(full_path_w);
558 /* Next try the system directory */
559 full_path = g_module_build_path(system_path, library_name);
560 full_path_w = g_utf8_to_utf16(full_path, -1, NULL, NULL, NULL);
562 if (full_path && full_path_w) {
563 dll_h = LoadLibraryW(full_path_w);
575 ws_module_open(gchar *module_name, GModuleFlags flags)
580 if (!init_dll_load_paths() || !module_name)
583 /* First try the program directory */
584 full_path = g_module_build_path(program_path, module_name);
587 mod = g_module_open(full_path, flags);
594 /* Next try the system directory */
595 full_path = g_module_build_path(system_path, module_name);
598 mod = g_module_open(full_path, flags);
608 /* utf8 version of getenv, needed to get win32 filename paths */
610 getenv_utf8(const char *varname)
616 envvar = getenv(varname);
618 /* since GLib 2.6 we need an utf8 version of the filename */
619 /* using the wide char version of getenv should work under all circumstances */
621 /* convert given varname to utf16, needed by _wgetenv */
622 varnamew = g_utf8_to_utf16(varname, -1, NULL, NULL, NULL);
623 if (varnamew == NULL) {
627 /* use wide char version of getenv */
628 envvarw = _wgetenv(varnamew);
630 if (envvarw == NULL) {
634 /* convert value to utf8 */
635 envvar = g_utf16_to_utf8(envvarw, -1, NULL, NULL, NULL);
641 /** Create or open a "Wireshark is running" mutex.
643 #define WIRESHARK_IS_RUNNING_UUID "9CA78EEA-EA4D-4490-9240-FC01FCEF464B"
645 static SECURITY_ATTRIBUTES *sec_attributes_;
647 void create_app_running_mutex() {
648 SECURITY_ATTRIBUTES *sa = NULL;
650 if (!sec_attributes_) sec_attributes_ = g_new0(SECURITY_ATTRIBUTES, 1);
652 sec_attributes_->nLength = sizeof(SECURITY_ATTRIBUTES);
653 sec_attributes_->lpSecurityDescriptor = g_new0(SECURITY_DESCRIPTOR, 1);
654 sec_attributes_->bInheritHandle = TRUE;
655 if (InitializeSecurityDescriptor(sec_attributes_->lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION)) {
656 if (SetSecurityDescriptorDacl(sec_attributes_->lpSecurityDescriptor, TRUE, NULL, FALSE)) {
657 sa = sec_attributes_;
662 g_free(sec_attributes_->lpSecurityDescriptor);
663 g_free(sec_attributes_);
664 sec_attributes_ = NULL;
666 CreateMutex(sa, FALSE, _T("Wireshark-is-running-{") _T(WIRESHARK_IS_RUNNING_UUID) _T("}"));
667 CreateMutex(sa, FALSE, _T("Global\\Wireshark-is-running-{") _T(WIRESHARK_IS_RUNNING_UUID) _T("}"));
671 * Editor modelines - http://www.wireshark.org/tools/modelines.html
676 * indent-tabs-mode: nil
679 * vi: set shiftwidth=6 tabstop=8 expandtab:
680 * :indentSize=6:tabSize=8:noTabs=true: