~ubuntu-branches/ubuntu/gutsy/wireshark/gutsy-security

« back to all changes in this revision

Viewing changes to wiretap/file_util.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2007-04-01 08:58:40 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070401085840-or3qhrpv8alt1bwg
Tags: 0.99.5-1
* New upstream release.
* debian/patches/09_idl2wrs.dpatch: updated to patch idl2wrs.sh.in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* file_util.c
 
2
 *
 
3
 * $Id: file_util.c 20402 2007-01-12 03:05:28Z ulfl $
 
4
 *
 
5
 * Wiretap Library
 
6
 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2
 
11
 * of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
21
 *
 
22
 */
 
23
 
 
24
/* file wrapper functions to prevent the file functions from GLib like g_open(),
 
25
 * as code compiled with MSVC 7 and above will collide with libs linked with msvcrt.dll (MSVC 6), lib GLib is
 
26
 *
 
27
 * DO NOT USE THESE FUNCTIONS DIRECTLY, USE eth_open() AND ALIKE FUNCTIONS FROM file_util.h INSTEAD!!!
 
28
 *
 
29
 * the following code is stripped down code copied from the GLib file glib/gstdio.h 
 
30
 * stipped down, because this is used on _WIN32 only and we use only wide char functions */
 
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
#include "config.h"
 
34
#endif
 
35
 
 
36
#include <glib.h>
 
37
 
 
38
#ifdef _WIN32
 
39
#include <windows.h>
 
40
#include <errno.h>
 
41
#include <wchar.h>
 
42
/*#include <direct.h>*/
 
43
#include <io.h>
 
44
#endif
 
45
 
 
46
#include "file_util.h"
 
47
 
 
48
 
 
49
 
 
50
 
 
51
/**
 
52
 * g_open:
 
53
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
54
 * @flags: as in open()
 
55
 * @mode: as in open()
 
56
 *
 
57
 * A wrapper for the POSIX open() function. The open() function is
 
58
 * used to convert a pathname into a file descriptor. Note that on
 
59
 * POSIX systems file descriptors are implemented by the operating
 
60
 * system. On Windows, it's the C library that implements open() and
 
61
 * file descriptors. The actual Windows API for opening files is
 
62
 * something different.
 
63
 *
 
64
 * See the C library manual for more details about open().
 
65
 *
 
66
 * Returns: a new file descriptor, or -1 if an error occurred. The
 
67
 * return value can be used exactly like the return value from open().
 
68
 * 
 
69
 * Since: 2.6
 
70
 */
 
71
int
 
72
eth_stdio_open (const gchar *filename,
 
73
        int          flags,
 
74
        int          mode)
 
75
{
 
76
#ifdef _WIN32
 
77
    {
 
78
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
 
79
      int retval;
 
80
      int save_errno;
 
81
      
 
82
      if (wfilename == NULL)
 
83
        {
 
84
          errno = EINVAL;
 
85
          return -1;
 
86
        }
 
87
 
 
88
      retval = _wopen (wfilename, flags, mode);
 
89
      save_errno = errno;
 
90
 
 
91
      g_free (wfilename);
 
92
 
 
93
      errno = save_errno;
 
94
      return retval;
 
95
    }
 
96
#else
 
97
  return open (filename, flags, mode);
 
98
#endif
 
99
}
 
100
 
 
101
 
 
102
/**
 
103
 * g_rename:
 
104
 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
105
 * @newfilename: a pathname in the GLib file name encoding
 
106
 *
 
107
 * A wrapper for the POSIX rename() function. The rename() function 
 
108
 * renames a file, moving it between directories if required.
 
109
 * 
 
110
 * See your C library manual for more details about how rename() works
 
111
 * on your system. Note in particular that on Win9x it is not possible
 
112
 * to rename a file if a file with the new name already exists. Also
 
113
 * it is not possible in general on Windows to rename an open file.
 
114
 *
 
115
 * Returns: 0 if the renaming succeeded, -1 if an error occurred
 
116
 * 
 
117
 * Since: 2.6
 
118
 */
 
119
int
 
120
eth_stdio_rename (const gchar *oldfilename,
 
121
          const gchar *newfilename)
 
122
{
 
123
#ifdef _WIN32
 
124
      wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
 
125
      wchar_t *wnewfilename;
 
126
      int retval;
 
127
      int save_errno = 0;
 
128
 
 
129
      if (woldfilename == NULL)
 
130
        {
 
131
          errno = EINVAL;
 
132
          return -1;
 
133
        }
 
134
 
 
135
      wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
 
136
 
 
137
      if (wnewfilename == NULL)
 
138
        {
 
139
          g_free (woldfilename);
 
140
          errno = EINVAL;
 
141
          return -1;
 
142
        }
 
143
 
 
144
      if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
 
145
        retval = 0;
 
146
      else
 
147
        {
 
148
          retval = -1;
 
149
          switch (GetLastError ())
 
150
            {
 
151
#define CASE(a,b) case ERROR_##a: save_errno = b; break
 
152
            CASE (FILE_NOT_FOUND, ENOENT);
 
153
            CASE (PATH_NOT_FOUND, ENOENT);
 
154
            CASE (ACCESS_DENIED, EACCES);
 
155
            CASE (NOT_SAME_DEVICE, EXDEV);
 
156
            CASE (LOCK_VIOLATION, EACCES);
 
157
            CASE (SHARING_VIOLATION, EACCES);
 
158
            CASE (FILE_EXISTS, EEXIST);
 
159
            CASE (ALREADY_EXISTS, EEXIST);
 
160
#undef CASE
 
161
            default: save_errno = EIO;
 
162
            }
 
163
        }
 
164
 
 
165
      g_free (woldfilename);
 
166
      g_free (wnewfilename);
 
167
      
 
168
      errno = save_errno;
 
169
      return retval;
 
170
#else
 
171
  return rename (oldfilename, newfilename);
 
172
#endif
 
173
}
 
174
 
 
175
/**
 
176
 * g_mkdir: 
 
177
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
178
 * @mode: permissions to use for the newly created directory
 
179
 *
 
180
 * A wrapper for the POSIX mkdir() function. The mkdir() function 
 
181
 * attempts to create a directory with the given name and permissions.
 
182
 * 
 
183
 * See the C library manual for more details about mkdir().
 
184
 *
 
185
 * Returns: 0 if the directory was successfully created, -1 if an error 
 
186
 *    occurred
 
187
 * 
 
188
 * Since: 2.6
 
189
 */
 
190
int
 
191
eth_stdio_mkdir (const gchar *filename,
 
192
         int          mode)
 
193
{
 
194
#ifdef _WIN32
 
195
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
 
196
      int retval;
 
197
      int save_errno;
 
198
 
 
199
      if (wfilename == NULL)
 
200
        {
 
201
          errno = EINVAL;
 
202
          return -1;
 
203
        }
 
204
 
 
205
      retval = _wmkdir (wfilename);
 
206
      save_errno = errno;
 
207
 
 
208
      g_free (wfilename);
 
209
      
 
210
      errno = save_errno;
 
211
      return retval;
 
212
#else
 
213
  return mkdir (filename, mode);
 
214
#endif
 
215
}
 
216
 
 
217
/**
 
218
 * g_stat: 
 
219
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
220
 * @buf: a pointer to a <structname>stat</structname> struct, which
 
221
 *    will be filled with the file information
 
222
 *
 
223
 * A wrapper for the POSIX stat() function. The stat() function 
 
224
 * returns information about a file.
 
225
 * 
 
226
 * See the C library manual for more details about stat().
 
227
 *
 
228
 * Returns: 0 if the information was successfully retrieved, -1 if an error 
 
229
 *    occurred
 
230
 * 
 
231
 * Since: 2.6
 
232
 */
 
233
int
 
234
eth_stdio_stat (const gchar *filename,
 
235
        struct stat *buf)
 
236
{
 
237
#ifdef _WIN32
 
238
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
 
239
      int retval;
 
240
      int save_errno;
 
241
      int len;
 
242
 
 
243
      if (wfilename == NULL)
 
244
        {
 
245
          errno = EINVAL;
 
246
          return -1;
 
247
        }
 
248
 
 
249
      len = wcslen (wfilename);
 
250
      while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
 
251
        len--;
 
252
      if (len > 0 &&
 
253
          (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
 
254
        wfilename[len] = '\0';
 
255
 
 
256
      retval = _wstat (wfilename, (struct _stat *) buf);
 
257
      save_errno = errno;
 
258
 
 
259
      g_free (wfilename);
 
260
 
 
261
      errno = save_errno;
 
262
      return retval;
 
263
#else
 
264
  return stat (filename, buf);
 
265
#endif
 
266
}
 
267
 
 
268
/**
 
269
 * g_unlink:
 
270
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
271
 *
 
272
 * A wrapper for the POSIX unlink() function. The unlink() function 
 
273
 * deletes a name from the filesystem. If this was the last link to the 
 
274
 * file and no processes have it opened, the diskspace occupied by the
 
275
 * file is freed.
 
276
 * 
 
277
 * See your C library manual for more details about unlink(). Note
 
278
 * that on Windows, it is in general not possible to delete files that
 
279
 * are open to some process, or mapped into memory.
 
280
 *
 
281
 * Returns: 0 if the name was successfully deleted, -1 if an error 
 
282
 *    occurred
 
283
 * 
 
284
 * Since: 2.6
 
285
 */
 
286
int
 
287
eth_stdio_unlink (const gchar *filename)
 
288
{
 
289
#ifdef _WIN32
 
290
      gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
 
291
      int retval;
 
292
      int save_errno;
 
293
 
 
294
      if (cp_filename == NULL)
 
295
        {
 
296
          errno = EINVAL;
 
297
          return -1;
 
298
        }
 
299
 
 
300
      retval = unlink (cp_filename);
 
301
      save_errno = errno;
 
302
 
 
303
      g_free (cp_filename);
 
304
 
 
305
      errno = save_errno;
 
306
      return retval;
 
307
#else
 
308
  return unlink (filename);
 
309
#endif
 
310
}
 
311
 
 
312
/**
 
313
 * g_remove:
 
314
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
315
 *
 
316
 * A wrapper for the POSIX remove() function. The remove() function
 
317
 * deletes a name from the filesystem.
 
318
 * 
 
319
 * See your C library manual for more details about how remove() works
 
320
 * on your system. On Unix, remove() removes also directories, as it
 
321
 * calls unlink() for files and rmdir() for directories. On Windows,
 
322
 * although remove() in the C library only works for files, this
 
323
 * function tries first remove() and then if that fails rmdir(), and
 
324
 * thus works for both files and directories. Note however, that on
 
325
 * Windows, it is in general not possible to remove a file that is
 
326
 * open to some process, or mapped into memory.
 
327
 *
 
328
 * If this function fails on Windows you can't infer too much from the
 
329
 * errno value. rmdir() is tried regardless of what caused remove() to
 
330
 * fail. Any errno value set by remove() will be overwritten by that
 
331
 * set by rmdir().
 
332
 *
 
333
 * Returns: 0 if the file was successfully removed, -1 if an error 
 
334
 *    occurred
 
335
 * 
 
336
 * Since: 2.6
 
337
 */
 
338
int
 
339
eth_stdio_remove (const gchar *filename)
 
340
{
 
341
#ifdef _WIN32
 
342
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
 
343
      int retval;
 
344
      int save_errno;
 
345
 
 
346
      if (wfilename == NULL)
 
347
        {
 
348
          errno = EINVAL;
 
349
          return -1;
 
350
        }
 
351
 
 
352
      retval = _wremove (wfilename);
 
353
      if (retval == -1)
 
354
        retval = _wrmdir (wfilename);
 
355
      save_errno = errno;
 
356
 
 
357
      g_free (wfilename);
 
358
 
 
359
      errno = save_errno;
 
360
      return retval;
 
361
#else
 
362
  return remove (filename);
 
363
#endif
 
364
}
 
365
 
 
366
/**
 
367
 * g_fopen:
 
368
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
369
 * @mode: a string describing the mode in which the file should be 
 
370
 *   opened
 
371
 *
 
372
 * A wrapper for the POSIX fopen() function. The fopen() function opens
 
373
 * a file and associates a new stream with it. 
 
374
 * 
 
375
 * See the C library manual for more details about fopen().
 
376
 *
 
377
 * Returns: A <type>FILE</type> pointer if the file was successfully
 
378
 *    opened, or %NULL if an error occurred
 
379
 * 
 
380
 * Since: 2.6
 
381
 */
 
382
FILE *
 
383
eth_stdio_fopen (const gchar *filename,
 
384
         const gchar *mode)
 
385
{
 
386
#ifdef _WIN32
 
387
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
 
388
      wchar_t *wmode;
 
389
      FILE *retval;
 
390
      int save_errno;
 
391
 
 
392
      if (wfilename == NULL)
 
393
        {
 
394
          errno = EINVAL;
 
395
          return NULL;
 
396
        }
 
397
 
 
398
      wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
 
399
 
 
400
      if (wmode == NULL)
 
401
        {
 
402
          g_free (wfilename);
 
403
          errno = EINVAL;
 
404
          return NULL;
 
405
        }
 
406
        
 
407
      retval = _wfopen (wfilename, wmode);
 
408
      save_errno = errno;
 
409
 
 
410
      g_free (wfilename);
 
411
      g_free (wmode);
 
412
 
 
413
      errno = save_errno;
 
414
      return retval;
 
415
#else
 
416
  return fopen (filename, mode);
 
417
#endif
 
418
}
 
419
 
 
420
/**
 
421
 * g_freopen:
 
422
 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
 
423
 * @mode: a string describing the mode in which the file should be 
 
424
 *   opened
 
425
 * @stream: an existing stream which will be reused, or %NULL
 
426
 *
 
427
 * A wrapper for the POSIX freopen() function. The freopen() function
 
428
 * opens a file and associates it with an existing stream.
 
429
 * 
 
430
 * See the C library manual for more details about freopen().
 
431
 *
 
432
 * Returns: A <type>FILE</type> pointer if the file was successfully
 
433
 *    opened, or %NULL if an error occurred.
 
434
 * 
 
435
 * Since: 2.6
 
436
 */
 
437
FILE *
 
438
eth_stdio_freopen (const gchar *filename,
 
439
           const gchar *mode,
 
440
           FILE        *stream)
 
441
{
 
442
#ifdef _WIN32
 
443
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
 
444
      wchar_t *wmode;
 
445
      FILE *retval;
 
446
      int save_errno;
 
447
 
 
448
      if (wfilename == NULL)
 
449
        {
 
450
          errno = EINVAL;
 
451
          return NULL;
 
452
        }
 
453
      
 
454
      wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
 
455
 
 
456
      if (wmode == NULL)
 
457
        {
 
458
          g_free (wfilename);
 
459
          errno = EINVAL;
 
460
          return NULL;
 
461
        }
 
462
      
 
463
      retval = _wfreopen (wfilename, wmode, stream);
 
464
      save_errno = errno;
 
465
 
 
466
      g_free (wfilename);
 
467
      g_free (wmode);
 
468
 
 
469
      errno = save_errno;
 
470
      return retval;
 
471
#else
 
472
  return freopen (filename, mode, stream);
 
473
#endif
 
474
}
 
475