Move tempfile.{h,c} into wsutil.
[metze/wireshark/wip.git] / wsutil / file_util.h
1 /* file_util.h
2  * File utility definitions
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __FILE_UTIL_H__
26 #define __FILE_UTIL_H__
27
28 #include "ws_symbol_export.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif /* __cplusplus */
33
34 #include <glib.h>
35
36 #ifdef _WIN32
37 #include <io.h>
38 #include <gmodule.h>
39 #endif
40
41 #ifdef HAVE_SYS_STAT_H
42 #include <sys/stat.h>
43 #endif
44
45
46 #ifdef _WIN32
47
48 /*
49  * The structure to pass to ws_stat64() and ws_fstat64().
50  */
51 #define ws_statb64      struct _stat64
52
53 /*  Win32 (and Win64): we use UTF-8 for filenames and pathnames throughout
54  *  the code, so file functions must convert filenames and pathnames from
55  *  UTF-8 to UTF-16 as we use NT Unicode (Win9x - now unsupported - used
56  *  locale-based encoding here).  Microsoft's UN*X-style wrappers don't
57  *  do that - they expect locale-based encodings - so we need our own
58  *  wrappers.  (We don't use the wrappers from GLib as that would, at
59  *  least for the wrappers that return file descriptors or take them
60  *  as arguments, require that we use the version of the C runtime with
61  *  which the GLib binaries were built, and we can't guarantee to do that.)
62  *
63  *  Note also that ws_stdio_rename() uses MoveFileEx() with
64  *  MOVEFILE_REPLACE_EXISTING, so that it acts like UN*X rename(),
65  *  removing the target if necessary.
66  */
67
68 #include <stdio.h>
69
70 WS_DLL_PUBLIC int ws_stdio_open (const gchar *filename, int flags, int mode);
71 WS_DLL_PUBLIC int ws_stdio_rename (const gchar *oldfilename, const gchar *newfilename);
72 WS_DLL_PUBLIC int ws_stdio_mkdir (const gchar *filename, int mode);
73 WS_DLL_PUBLIC int ws_stdio_stat64 (const gchar *filename, ws_statb64 *buf);
74 WS_DLL_PUBLIC int ws_stdio_unlink (const gchar *filename);
75 WS_DLL_PUBLIC int ws_stdio_remove (const gchar *filename);
76
77 WS_DLL_PUBLIC FILE * ws_stdio_fopen (const gchar *filename, const gchar *mode);
78 WS_DLL_PUBLIC FILE * ws_stdio_freopen (const gchar *filename, const gchar *mode, FILE *stream);
79
80 #define ws_open         ws_stdio_open
81 #define ws_rename       ws_stdio_rename
82 #define ws_mkdir        ws_stdio_mkdir
83 #define ws_stat64       ws_stdio_stat64
84 #define ws_unlink       ws_stdio_unlink
85 #define ws_remove       ws_stdio_remove
86 #define ws_fopen        ws_stdio_fopen
87 #define ws_freopen      ws_stdio_freopen
88
89 /*
90  * These routines don't take pathnames, so they don't require
91  * pathname-converting wrappers on Windows.
92  */
93 #define ws_read    _read
94 #define ws_write   _write
95 #define ws_close   _close
96 #define ws_dup     _dup
97 #define ws_fstat64 _fstati64    /* use _fstati64 for 64-bit size support */
98 #define ws_lseek64 _lseeki64    /* use _lseeki64 for 64-bit offset support */
99 #define ws_fdopen  _fdopen
100
101 /* DLL loading */
102
103 /** Try to remove the current directory from the DLL search path.
104  * SetDllDirectory is tried, then SetCurrentDirectory(program_dir)
105  *
106  * @return TRUE if we were able to call SetDllDirectory, FALSE otherwise.
107  */
108 WS_DLL_PUBLIC
109 gboolean ws_init_dll_search_path();
110
111 /** Load a DLL using LoadLibrary.
112  * Only the system and program directories are searched.
113  *
114  * @param library_name The name of the DLL.
115  * @return A handle to the DLL if found, NULL on failure.
116  */
117
118 WS_DLL_PUBLIC
119 void *ws_load_library(gchar *library_name);
120
121 /** Load a DLL using g_module_open.
122  * Only the system and program directories are searched.
123  *
124  * @param module_name The name of the DLL.
125  * @param flags Flags to be passed to g_module_open.
126  * @return A handle to the DLL if found, NULL on failure.
127  */
128 WS_DLL_PUBLIC
129 GModule *ws_module_open(gchar *module_name, GModuleFlags flags);
130
131 /*
132  * utf8 version of getenv, needed to get win32 filename paths
133  */
134 WS_DLL_PUBLIC char *getenv_utf8(const char *varname);
135
136 /** Create or open a "Wireshark is running" mutex.
137  * Create or open a mutex which signals that Wireshark or its associated
138  * executables is running. Used by the installer to test for a running application.
139  */
140 WS_DLL_PUBLIC void create_app_running_mutex();
141
142 #else   /* _WIN32 */
143
144 /*
145  * The structure to pass to ws_fstat64().
146  */
147 #define ws_statb64      struct stat
148
149 /* Not Windows, presumed to be UN*X-compatible */
150 #define ws_open                 open
151 #define ws_rename               rename
152 #define ws_mkdir(dir,mode)      mkdir(dir,mode)
153 #define ws_stat64               stat
154 #define ws_unlink               unlink
155 #define ws_remove               remove
156 #define ws_fopen                fopen
157 #define ws_freopen              freopen
158
159 #define ws_read    read
160 #define ws_write   write
161 #define ws_close   close
162 #define ws_dup     dup
163 #define ws_fstat64 fstat        /* AC_SYS_LARGEFILE should make off_t 64-bit */
164 #define ws_lseek64 lseek        /* AC_SYS_LARGEFILE should make off_t 64-bit */
165 #define ws_fdopen  fdopen
166 #define O_BINARY   0            /* Win32 needs the O_BINARY flag for open() */
167
168 #endif /* _WIN32 */
169
170 /* directory handling */
171 #define WS_DIR                          GDir
172 #define WS_DIRENT                       const char
173 #define ws_dir_open                     g_dir_open
174 #define ws_dir_read_name                g_dir_read_name
175 #define ws_dir_get_name(dirent)         dirent
176 #define ws_dir_rewind                   g_dir_rewind
177 #define ws_dir_close                    g_dir_close
178
179 /* XXX - remove include "dirent.h" */
180 /* XXX - remove include "direct.h" */
181 /* XXX - remove include "sys/stat.h" */
182 /* XXX - update docs (e.g. README.developer) */
183
184 #ifdef __cplusplus
185 }
186 #endif /* __cplusplus */
187
188 #endif /* __FILE_UTIL_H__ */