2 * File utility definitions
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #ifndef __FILE_UTIL_H__
12 #define __FILE_UTIL_H__
16 #include "ws_symbol_export.h"
19 #include <io.h> /* for _read(), _write(), etc. */
23 #include <fcntl.h> /* for open() */
26 #include <unistd.h> /* for read(), write(), close(), etc. */
29 #include <sys/stat.h> /* for stat() and struct stat */
35 #endif /* __cplusplus */
37 /* We set a larger IO Buffer size for the capture files */
38 #define IO_BUF_SIZE (64 * 1024)
41 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
42 * define them either.)
44 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
47 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
50 #define S_IFIFO _S_IFIFO
53 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
56 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
62 * The structure to pass to ws_stat64() and ws_fstat64().
64 #define ws_statb64 struct _stat64
66 /* Win32 (and Win64): we use UTF-8 for filenames and pathnames throughout
67 * the code, so file functions must convert filenames and pathnames from
68 * UTF-8 to UTF-16 as we use NT Unicode (Win9x - now unsupported - used
69 * locale-based encoding here). Microsoft's UN*X-style wrappers don't
70 * do that - they expect locale-based encodings - so we need our own
71 * wrappers. (We don't use the wrappers from GLib as that would, at
72 * least for the wrappers that return file descriptors or take them
73 * as arguments, require that we use the version of the C runtime with
74 * which the GLib binaries were built, and we can't guarantee to do that.)
76 * Note also that ws_stdio_rename() uses MoveFileEx() with
77 * MOVEFILE_REPLACE_EXISTING, so that it acts like UN*X rename(),
78 * removing the target if necessary.
81 WS_DLL_PUBLIC
int ws_stdio_open (const char *filename
, int flags
, int mode
);
82 WS_DLL_PUBLIC
int ws_stdio_rename (const char *oldfilename
, const char *newfilename
);
83 WS_DLL_PUBLIC
int ws_stdio_mkdir (const char *filename
, int mode
);
84 WS_DLL_PUBLIC
int ws_stdio_stat64 (const char *filename
, ws_statb64
*buf
);
85 WS_DLL_PUBLIC
int ws_stdio_unlink (const char *filename
);
86 WS_DLL_PUBLIC
int ws_stdio_remove (const char *filename
);
88 WS_DLL_PUBLIC
FILE * ws_stdio_fopen (const char *filename
, const char *mode
);
89 WS_DLL_PUBLIC
FILE * ws_stdio_freopen (const char *filename
, const char *mode
, FILE *stream
);
91 #define ws_open ws_stdio_open
92 #define ws_rename ws_stdio_rename
93 #define ws_mkdir ws_stdio_mkdir
94 #define ws_stat64 ws_stdio_stat64
95 #define ws_unlink ws_stdio_unlink
96 #define ws_remove ws_stdio_remove
97 #define ws_fopen ws_stdio_fopen
98 #define ws_freopen ws_stdio_freopen
101 * These routines don't take pathnames, so they don't require
102 * pathname-converting wrappers on Windows.
105 typedef unsigned int ws_file_size_t
;
106 typedef signed int ws_file_ssize_t
;
108 #define ws_read _read
109 #define ws_write _write
110 #define ws_close _close
112 #define ws_fseek64 _fseeki64 /* use _fseeki64 for 64-bit offset support */
113 #define ws_fstat64 _fstati64 /* use _fstati64 for 64-bit size support */
114 #define ws_ftell64 _ftelli64 /* use _ftelli64 for 64-bit offset support */
115 #define ws_lseek64 _lseeki64 /* use _lseeki64 for 64-bit offset support */
116 #define ws_fdopen _fdopen
117 #define ws_fileno _fileno
118 #define ws_isatty _isatty
119 #define ws_getc_unlocked _fgetc_nolock
122 * Other CRT functions. getpid probably belongs in sys_util.h or proc_util.h
123 * but neither yet exist.
125 #define ws_getpid _getpid
126 #define ws_umask _umask
130 /** Try to remove the current directory from the DLL search path.
131 * SetDllDirectory is tried, then SetCurrentDirectory(program_dir)
133 * @return true if we were able to call SetDllDirectory, false otherwise.
136 bool ws_init_dll_search_path(void);
138 /** Load a DLL using LoadLibrary.
139 * Only the system and program directories are searched.
141 * @param library_name The name of the DLL.
142 * @return A handle to the DLL if found, NULL on failure.
146 void *ws_load_library(const char *library_name
);
148 /** Load wpcap.dll using g_module_open.
149 * Only the system and program directories are searched.
151 * @return A handle to the DLL if found, NULL on failure.
154 GModule
*load_wpcap_module(void);
156 /** Create or open a "Wireshark is running" mutex.
157 * Create or open a mutex which signals that Wireshark or its associated
158 * executables is running. Used by the installer to test for a running application.
160 WS_DLL_PUBLIC
void create_app_running_mutex(void);
162 /** Close our "Wireshark is running" mutex.
164 WS_DLL_PUBLIC
void close_app_running_mutex(void);
166 /** Close a file descriptor if it is not open
168 WS_DLL_PUBLIC
int ws_close_if_possible(int fd
);
173 * The structure to pass to ws_fstat64().
175 #define ws_statb64 struct stat
177 /* Not Windows, presumed to be UN*X-compatible */
179 #define ws_rename rename
180 #define ws_mkdir(dir,mode) mkdir(dir,mode)
181 #define ws_stat64 stat
182 #define ws_unlink unlink
183 #define ws_remove remove
184 #define ws_fopen fopen
185 #define ws_freopen freopen
187 typedef size_t ws_file_size_t
;
188 typedef ssize_t ws_file_ssize_t
;
191 #define ws_write write
194 * Just in case this is used in a class with a close method or member.
196 #define ws_close ::close
198 #define ws_close close
201 #define ws_close_if_possible ws_close
205 #define ws_fseek64 fseeko /* AC_SYS_LARGEFILE should make off_t 64-bit */
206 #define ws_ftell64 ftello /* AC_SYS_LARGEFILE should make off_t 64-bit */
208 #define ws_fseek64(fh,offset,whence) fseek(fh,(long)(offset),whence)
209 #define ws_ftell64 ftell
211 #define ws_fstat64 fstat /* AC_SYS_LARGEFILE should make off_t 64-bit */
212 #define ws_lseek64 lseek /* AC_SYS_LARGEFILE should make off_t 64-bit */
213 #define ws_fdopen fdopen
214 #define ws_fileno fileno
215 #define ws_isatty isatty
216 #define ws_getc_unlocked getc_unlocked
217 #define O_BINARY 0 /* Win32 needs the O_BINARY flag for open() */
219 /* Other CRT functions */
220 #define ws_getpid getpid
221 #define ws_umask umask
225 /* directory handling */
227 #define WS_DIRENT const char
228 #define ws_dir_open g_dir_open
229 #define ws_dir_read_name g_dir_read_name
230 #define ws_dir_get_name(dirent) dirent
231 #define ws_dir_rewind g_dir_rewind
232 #define ws_dir_close g_dir_close
234 /* XXX - remove include "sys/stat.h" from files that include this header */
235 /* XXX - update docs (e.g. README.developer) */
239 #endif /* __cplusplus */
241 #endif /* __FILE_UTIL_H__ */