| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
file_util.h
Go to the documentation of this file.
1
10
11#ifndef __FILE_UTIL_H__
12#define __FILE_UTIL_H__
13
14#include <stdbool.h>
15
16#include "ws_symbol_export.h"
17
18#ifdef _WIN32
19#include <io.h> /* for _read(), _write(), etc. */
20#include <gmodule.h>
21#endif
22
23#include <fcntl.h> /* for open() */
24
25#ifdef HAVE_UNISTD_H
26#include <unistd.h> /* for read(), write(), close(), etc. */
27#endif
28
29#include <sys/stat.h> /* for stat() and struct stat */
30
31#include <stdio.h>
32
33#ifdef __cplusplus
34 extern "C" {
35#endif /* __cplusplus */
36
37/* We set a larger IO Buffer size for the capture files */
38#define IO_BUF_SIZE (64 * 1024)
39
40/*
41 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
42 * define them either.)
43 *
44 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
45 */
46#ifndef S_ISREG
47#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
48#endif
49#ifndef S_IFIFO
50#define S_IFIFO _S_IFIFO
51#endif
52#ifndef S_ISFIFO
53#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
54#endif
55#ifndef S_ISDIR
56#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
57#endif
58
59#ifdef _WIN32
60
61/*
62 * The structure to pass to ws_stat64() and ws_fstat64().
63 */
64#define ws_statb64 struct _stat64
65
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.)
75 *
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.
79 */
80
81WS_DLL_PUBLIC int ws_stdio_open (const char *filename, int flags, int mode);
82WS_DLL_PUBLIC int ws_stdio_rename (const char *oldfilename, const char *newfilename);
83WS_DLL_PUBLIC int ws_stdio_mkdir (const char *filename, int mode);
84WS_DLL_PUBLIC int ws_stdio_stat64 (const char *filename, ws_statb64 *buf);
85WS_DLL_PUBLIC int ws_stdio_unlink (const char *filename);
86WS_DLL_PUBLIC int ws_stdio_remove (const char *filename);
87
88WS_DLL_PUBLIC FILE * ws_stdio_fopen (const char *filename, const char *mode);
89WS_DLL_PUBLIC FILE * ws_stdio_freopen (const char *filename, const char *mode, FILE *stream);
90
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
99
100/*
101 * These routines don't take pathnames, so they don't require
102 * pathname-converting wrappers on Windows.
103 */
104
105 typedef unsigned int ws_file_size_t;
106 typedef signed int ws_file_ssize_t;
107
108#define ws_read _read
109#define ws_write _write
110#define ws_close _close
111#define ws_dup _dup
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
120
121/*
122 * Other CRT functions. getpid probably belongs in sys_util.h or proc_util.h
123 * but neither yet exist.
124 */
125#define ws_getpid _getpid
126#define ws_umask _umask
127
128/* DLL loading */
129
135WS_DLL_PUBLIC
136 bool ws_init_dll_search_path(void);
137
144
145WS_DLL_PUBLIC
146 void *ws_load_library(const char *library_name);
147
153WS_DLL_PUBLIC
154GModule *load_wpcap_module(void);
155
160WS_DLL_PUBLIC void create_app_running_mutex(void);
161
164WS_DLL_PUBLIC void close_app_running_mutex(void);
165
168WS_DLL_PUBLIC int ws_close_if_possible(int fd);
169
170#else /* _WIN32 */
171
172/*
173 * The structure to pass to ws_fstat64().
174 */
175#define ws_statb64 struct stat
176
177/* Not Windows, presumed to be UN*X-compatible */
178#define ws_open open
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
186
187 typedef size_t ws_file_size_t;
188 typedef ssize_t ws_file_ssize_t;
189
190#define ws_read read
191#define ws_write write
192#ifdef __cplusplus
193/*
194 * Just in case this is used in a class with a close method or member.
195 */
196#define ws_close ::close
197#else
198#define ws_close close
199#endif
200
201#define ws_close_if_possible ws_close
202
203#define ws_dup dup
204#ifdef HAVE_FSEEKO
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 */
207#else
208#define ws_fseek64(fh,offset,whence) fseek(fh,(long)(offset),whence)
209#define ws_ftell64 ftell
210#endif
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() */
218
219/* Other CRT functions */
220#define ws_getpid getpid
221#define ws_umask umask
222
223#endif /* _WIN32 */
224
225/* directory handling */
226#define WS_DIR GDir
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
233
234/* XXX - remove include "sys/stat.h" from files that include this header */
235/* XXX - update docs (e.g. README.developer) */
236
237/* Error codes from file handling routines */
238#define FILE_ERR_CANT_OPEN -1
239#define FILE_ERR_CANT_COMPRESS -2
240#define FILE_ERR_SHORT_WRITE -3
241#define FILE_ERR_CANT_CLOSE -4
242#define FILE_ERR_INTERNAL -5
243
244#ifdef __cplusplus
245}
246#endif /* __cplusplus */
247
248#endif /* __FILE_UTIL_H__ */
stream
Definition stream.c:41