As pointed by Evan: don't leak memory when the string pointer is NULL.
[metze/wireshark/wip.git] / epan / filesystem.h
1 /* filesystem.h
2  * Filesystem 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 FILESYSTEM_H
26 #define FILESYSTEM_H
27
28 #include "ws_symbol_export.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif /* __cplusplus */
33
34 /*
35  * Default profile name.
36  */
37 #define DEFAULT_PROFILE      "Default"
38
39
40 /*
41  * Get the pathname of the directory from which the executable came,
42  * and save it for future use.  Returns NULL on success, and a
43  * g_mallocated string containing an error on failure.
44  */
45 WS_DLL_PUBLIC char *init_progfile_dir(const char *arg0, int (*main)(int, char **));
46
47 /*
48  * Get the directory in which the program resides.
49  */
50 WS_DLL_PUBLIC const char *get_progfile_dir(void);
51
52 /*
53  * Get the directory in which plugins are stored; this must not be called
54  * before init_progfile_dir() is called, as they might be stored in a
55  * subdirectory of the program file directory.
56  */
57 WS_DLL_PUBLIC const char *get_plugin_dir(void);
58
59 /*
60  * Get the directory in which python plugins are stored; this must not be
61  * called before init_progfile_dir() is called, as they might be stored in a
62  * subdirectory of the program file directory.
63  */
64 WS_DLL_PUBLIC const char *get_wspython_dir(void);
65
66 /*
67  * Get the flag indicating whether we're running from a build
68  * directory.
69  */
70 WS_DLL_PUBLIC gboolean running_in_build_directory(void);
71
72 /*
73  * Get the directory in which global configuration files are
74  * stored.
75  */
76 WS_DLL_PUBLIC const char *get_datafile_dir(void);
77
78 /*
79  * Construct the path name of a global configuration file, given the
80  * file name.
81  *
82  * The returned file name was g_malloc()'d so it must be g_free()d when the
83  * caller is done with it.
84  */
85 WS_DLL_PUBLIC char *get_datafile_path(const char *filename);
86
87 /*
88  * Get the personal plugin dir.
89  */
90 WS_DLL_PUBLIC char *get_plugins_pers_dir(void);
91
92 /*
93  * Get the directory in which files that, at least on UNIX, are
94  * system files (such as "/etc/ethers") are stored; on Windows,
95  * there's no "/etc" directory, so we get them from the Wireshark
96  * global configuration and data file directory.
97  */
98 WS_DLL_PUBLIC const char *get_systemfile_dir(void);
99
100 /*
101  * Set the configuration profile name to be used for storing
102  * personal configuration files.
103  */
104 WS_DLL_PUBLIC void set_profile_name(const gchar *profilename);
105
106 /*
107  * Get the current configuration profile name used for storing
108  * personal configuration files.
109  */
110 WS_DLL_PUBLIC const char *get_profile_name(void);
111
112 /*
113  * Check if current profile is default profile.
114  */
115 WS_DLL_PUBLIC gboolean is_default_profile(void);
116
117 /*
118  * Check if we have global profiles.
119  */
120 WS_DLL_PUBLIC gboolean has_global_profiles(void);
121
122 /*
123  * Get the directory used to store configuration profile directories.
124  */
125 WS_DLL_PUBLIC const char *get_profiles_dir(void);
126
127 /*
128  * Get the directory used to store global configuration profile directories.
129  */
130 WS_DLL_PUBLIC const char *get_global_profiles_dir(void);
131
132
133 /*
134  * Store filenames used for personal config files so we know which
135  * files to copy when duplicate a configuration profile.
136  */
137 WS_DLL_PUBLIC void profile_store_persconffiles(gboolean store);
138
139 /*
140  * Check if given configuration profile exists.
141  */
142 WS_DLL_PUBLIC gboolean profile_exists(const gchar *profilename, gboolean global);
143
144 /*
145  * Create a directory for the given configuration profile.
146  * If we attempted to create it, and failed, return -1 and
147  * set "*pf_dir_path_return" to the pathname of the directory we failed
148  * to create (it's g_mallocated, so our caller should free it); otherwise,
149  * return 0.
150  */
151 WS_DLL_PUBLIC int create_persconffile_profile(const char *profilename,
152                                        char **pf_dir_path_return);
153
154 /*
155  * Delete the directory for the given configuration profile.
156  * If we attempted to delete it, and failed, return -1 and
157  * set "*pf_dir_path_return" to the pathname of the directory we failed
158  * to delete (it's g_mallocated, so our caller should free it); otherwise,
159  * return 0.
160  */
161 WS_DLL_PUBLIC int delete_persconffile_profile(const char *profilename,
162                                        char **pf_dir_path_return);
163
164 /*
165  * Rename the directory for the given confinguration profile.
166  */
167 WS_DLL_PUBLIC int rename_persconffile_profile(const char *fromname, const char *toname,
168                                        char **pf_from_dir_path_return,
169                                        char **pf_to_dir_path_return);
170
171 /*
172  * Copy files in one profile to the other.
173  */
174 WS_DLL_PUBLIC int copy_persconffile_profile(const char *toname, const char *fromname,
175                                      gboolean from_global,
176                                      char **pf_filename_return,
177                                      char **pf_to_dir_path_return,
178                                      char **pf_from_dir_path_return);
179
180 /*
181  * Create the directory that holds personal configuration files, if
182  * necessary.  If we attempted to create it, and failed, return -1 and
183  * set "*pf_dir_path_return" to the pathname of the directory we failed
184  * to create (it's g_mallocated, so our caller should free it); otherwise,
185  * return 0.
186  */
187 WS_DLL_PUBLIC int create_persconffile_dir(char **pf_dir_path_return);
188
189 /*
190  * Construct the path name of a personal configuration file, given the
191  * file name.  If using configuration profiles this directory will be
192  * used if "from_profile" is TRUE.
193  *
194  * The returned file name was g_malloc()'d so it must be g_free()d when the
195  * caller is done with it.
196  */
197 WS_DLL_PUBLIC char *get_persconffile_path(const char *filename, gboolean from_profile);
198
199 /*
200  * Get the (default) directory in which personal data is stored.
201  *
202  * On Win32, this is the "My Documents" folder in the personal profile.
203  * On UNIX this is simply the current directory.
204  */
205 WS_DLL_PUBLIC const char *get_persdatafile_dir(void);
206
207 /*
208  * Construct the path name of a file in $TMP/%TEMP% directory.
209  * Or "/tmp/<filename>" (C:\<filename>) if that fails.
210  *
211  * Return value is g_malloced so the caller should g_free it.
212  */
213 WS_DLL_PUBLIC char *get_tempfile_path(const char *filename);
214
215 /*
216  * process command line option belonging to the filesystem settings
217  */
218 WS_DLL_PUBLIC int filesystem_opt(int opt, const char *optstr);
219
220 /*
221  * Return an error message for UNIX-style errno indications on open or
222  * create operations.
223  */
224 WS_DLL_PUBLIC const char *file_open_error_message(int err, gboolean for_writing);
225
226 /*
227  * Return an error message for UNIX-style errno indications on write
228  * operations.
229  */
230 WS_DLL_PUBLIC const char *file_write_error_message(int err);
231
232 /*
233  * Given a pathname, return the last component.
234  */
235 WS_DLL_PUBLIC const char *get_basename(const char *);
236
237  /*
238   * Given a pathname, return a pointer to the last pathname separator
239   * character in the pathname, or NULL if the pathname contains no
240   * separators.
241   */
242 WS_DLL_PUBLIC char *find_last_pathname_separator(const char *path);
243
244 /*
245  * Given a pathname, return a string containing everything but the
246  * last component.  NOTE: this overwrites the pathname handed into
247  * it....
248  */
249 WS_DLL_PUBLIC char *get_dirname(char *);
250
251 /*
252  * Given a pathname, return:
253  *
254  *      the errno, if an attempt to "stat()" the file fails;
255  *
256  *      EISDIR, if the attempt succeeded and the file turned out
257  *      to be a directory;
258  *
259  *      0, if the attempt succeeded and the file turned out not
260  *      to be a directory.
261  */
262 WS_DLL_PUBLIC int test_for_directory(const char *);
263
264 /*
265  * Given a pathname, return:
266  *
267  *      the errno, if an attempt to "stat()" the file fails;
268  *
269  *      ESPIPE, if the attempt succeeded and the file turned out
270  *      to be a FIFO;
271  *
272  *      0, if the attempt succeeded and the file turned out not
273  *      to be a FIFO.
274  */
275 WS_DLL_PUBLIC int test_for_fifo(const char *);
276
277 /* Delete a file */
278 WS_DLL_PUBLIC gboolean deletefile (const char *path);
279
280 /*
281  * Check, if file is existing.
282  */
283 WS_DLL_PUBLIC gboolean file_exists(const char *fname);
284
285 /*
286  * Check if two filenames are identical (with absolute and relative paths).
287  */
288 WS_DLL_PUBLIC gboolean files_identical(const char *fname1, const char *fname2);
289
290 /*
291  * Copy a file in binary mode, for those operating systems that care about
292  * such things.  This should be OK for all files, even text files, as
293  * we'll copy the raw bytes, and we don't look at the bytes as we copy
294  * them.
295  *
296  * Returns TRUE on success, FALSE on failure. If a failure, it also
297  * displays a simple dialog window with the error message.
298  */
299 WS_DLL_PUBLIC gboolean copy_file_binary_mode(const char *from_filename,
300     const char *to_filename);
301
302 #ifdef __cplusplus
303 }
304 #endif /* __cplusplus */
305
306 #endif /* FILESYSTEM_H */