Wireshark  2.9.0-477-g68ec514b
The Wireshark network protocol analyzer
file_util.h
1 /* file_util.h
2  * File utility definitions
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #ifndef __FILE_UTIL_H__
12 #define __FILE_UTIL_H__
13 
14 #include "ws_symbol_export.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif /* __cplusplus */
19 
20 #include <glib.h>
21 
22 #ifdef _WIN32
23 #include <io.h> /* for _read(), _write(), etc. */
24 #include <gmodule.h>
25 #endif
26 
27 #ifdef HAVE_FCNTL_H
28 #include <fcntl.h> /* for open() */
29 #endif
30 
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h> /* for read(), write(), close(), etc. */
33 #endif
34 
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h> /* for stat() and struct stat */
37 #endif
38 
39 /*
40  * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
41  * define them either.)
42  *
43  * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
44  */
45 #ifndef S_ISREG
46 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
47 #endif
48 #ifndef S_IFIFO
49 #define S_IFIFO _S_IFIFO
50 #endif
51 #ifndef S_ISFIFO
52 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
53 #endif
54 #ifndef S_ISDIR
55 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
56 #endif
57 
58 #include <stdio.h>
59 
60 #ifdef _WIN32
61 
62 /*
63  * The structure to pass to ws_stat64() and ws_fstat64().
64  */
65 #define ws_statb64 struct _stat64
66 
67 /* Win32 (and Win64): we use UTF-8 for filenames and pathnames throughout
68  * the code, so file functions must convert filenames and pathnames from
69  * UTF-8 to UTF-16 as we use NT Unicode (Win9x - now unsupported - used
70  * locale-based encoding here). Microsoft's UN*X-style wrappers don't
71  * do that - they expect locale-based encodings - so we need our own
72  * wrappers. (We don't use the wrappers from GLib as that would, at
73  * least for the wrappers that return file descriptors or take them
74  * as arguments, require that we use the version of the C runtime with
75  * which the GLib binaries were built, and we can't guarantee to do that.)
76  *
77  * Note also that ws_stdio_rename() uses MoveFileEx() with
78  * MOVEFILE_REPLACE_EXISTING, so that it acts like UN*X rename(),
79  * removing the target if necessary.
80  */
81 
82 WS_DLL_PUBLIC int ws_stdio_open (const gchar *filename, int flags, int mode);
83 WS_DLL_PUBLIC int ws_stdio_rename (const gchar *oldfilename, const gchar *newfilename);
84 WS_DLL_PUBLIC int ws_stdio_mkdir (const gchar *filename, int mode);
85 WS_DLL_PUBLIC int ws_stdio_stat64 (const gchar *filename, ws_statb64 *buf);
86 WS_DLL_PUBLIC int ws_stdio_unlink (const gchar *filename);
87 WS_DLL_PUBLIC int ws_stdio_remove (const gchar *filename);
88 
89 WS_DLL_PUBLIC FILE * ws_stdio_fopen (const gchar *filename, const gchar *mode);
90 WS_DLL_PUBLIC FILE * ws_stdio_freopen (const gchar *filename, const gchar *mode, FILE *stream);
91 
92 #define ws_open ws_stdio_open
93 #define ws_rename ws_stdio_rename
94 #define ws_mkdir ws_stdio_mkdir
95 #define ws_stat64 ws_stdio_stat64
96 #define ws_unlink ws_stdio_unlink
97 #define ws_remove ws_stdio_remove
98 #define ws_fopen ws_stdio_fopen
99 #define ws_freopen ws_stdio_freopen
100 
101 /*
102  * These routines don't take pathnames, so they don't require
103  * pathname-converting wrappers on Windows.
104  */
105 #define ws_read _read
106 #define ws_write _write
107 #define ws_close _close
108 #define ws_dup _dup
109 #define ws_fstat64 _fstati64 /* use _fstati64 for 64-bit size support */
110 #define ws_lseek64 _lseeki64 /* use _lseeki64 for 64-bit offset support */
111 #define ws_fdopen _fdopen
112 #define ws_fileno _fileno
113 #define ws_isatty _isatty
114 #define ws_getc_unlocked _fgetc_nolock
115 
116 /*
117  * Other CRT functions. getpid probably belongs in sys_util.h or proc_util.h
118  * but neither yet exist.
119  */
120 #define ws_getpid _getpid
121 #define ws_umask _umask
122 
123 /* DLL loading */
124 
130 WS_DLL_PUBLIC
131 gboolean ws_init_dll_search_path();
132 
140 WS_DLL_PUBLIC
141 void *ws_load_library(const gchar *library_name);
142 
150 WS_DLL_PUBLIC
151 GModule *ws_module_open(gchar *module_name, GModuleFlags flags);
152 
157 WS_DLL_PUBLIC void create_app_running_mutex();
158 
161 WS_DLL_PUBLIC void close_app_running_mutex();
162 
163 #else /* _WIN32 */
164 
165 /*
166  * The structure to pass to ws_fstat64().
167  */
168 #define ws_statb64 struct stat
169 
170 /* Not Windows, presumed to be UN*X-compatible */
171 #define ws_open open
172 #define ws_rename rename
173 #define ws_mkdir(dir,mode) mkdir(dir,mode)
174 #define ws_stat64 stat
175 #define ws_unlink unlink
176 #define ws_remove remove
177 #define ws_fopen fopen
178 #define ws_freopen freopen
179 
180 #define ws_read read
181 #define ws_write write
182 #ifdef __cplusplus
183 /*
184  * Just in case this is used in a class with a close method or member.
185  */
186 #define ws_close ::close
187 #else
188 #define ws_close close
189 #endif
190 #define ws_dup dup
191 #define ws_fstat64 fstat /* AC_SYS_LARGEFILE should make off_t 64-bit */
192 #define ws_lseek64 lseek /* AC_SYS_LARGEFILE should make off_t 64-bit */
193 #define ws_fdopen fdopen
194 #define ws_fileno fileno
195 #define ws_isatty isatty
196 #define ws_getc_unlocked getc_unlocked
197 #define O_BINARY 0 /* Win32 needs the O_BINARY flag for open() */
198 
199 /* Other CRT functions */
200 #define ws_getpid getpid
201 #define ws_umask umask
202 
203 #endif /* _WIN32 */
204 
205 /* directory handling */
206 #define WS_DIR GDir
207 #define WS_DIRENT const char
208 #define ws_dir_open g_dir_open
209 #define ws_dir_read_name g_dir_read_name
210 #define ws_dir_get_name(dirent) dirent
211 #define ws_dir_rewind g_dir_rewind
212 #define ws_dir_close g_dir_close
213 
214 /* XXX - remove include "sys/stat.h" from files that include this header */
215 /* XXX - update docs (e.g. README.developer) */
216 
217 #ifdef __cplusplus
218 }
219 #endif /* __cplusplus */
220 
221 #endif /* __FILE_UTIL_H__ */
Definition: stream.c:40