3
* $Id: file_util.c 20402 2007-01-12 03:05:28Z ulfl $
6
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
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
27
* DO NOT USE THESE FUNCTIONS DIRECTLY, USE eth_open() AND ALIKE FUNCTIONS FROM file_util.h INSTEAD!!!
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 */
42
/*#include <direct.h>*/
46
#include "file_util.h"
53
* @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
54
* @flags: as in open()
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.
64
* See the C library manual for more details about open().
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().
72
eth_stdio_open (const gchar *filename,
78
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
82
if (wfilename == NULL)
88
retval = _wopen (wfilename, flags, mode);
97
return open (filename, flags, mode);
104
* @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
105
* @newfilename: a pathname in the GLib file name encoding
107
* A wrapper for the POSIX rename() function. The rename() function
108
* renames a file, moving it between directories if required.
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.
115
* Returns: 0 if the renaming succeeded, -1 if an error occurred
120
eth_stdio_rename (const gchar *oldfilename,
121
const gchar *newfilename)
124
wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
125
wchar_t *wnewfilename;
129
if (woldfilename == NULL)
135
wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
137
if (wnewfilename == NULL)
139
g_free (woldfilename);
144
if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
149
switch (GetLastError ())
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);
161
default: save_errno = EIO;
165
g_free (woldfilename);
166
g_free (wnewfilename);
171
return rename (oldfilename, newfilename);
177
* @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
178
* @mode: permissions to use for the newly created directory
180
* A wrapper for the POSIX mkdir() function. The mkdir() function
181
* attempts to create a directory with the given name and permissions.
183
* See the C library manual for more details about mkdir().
185
* Returns: 0 if the directory was successfully created, -1 if an error
191
eth_stdio_mkdir (const gchar *filename,
195
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
199
if (wfilename == NULL)
205
retval = _wmkdir (wfilename);
213
return mkdir (filename, mode);
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
223
* A wrapper for the POSIX stat() function. The stat() function
224
* returns information about a file.
226
* See the C library manual for more details about stat().
228
* Returns: 0 if the information was successfully retrieved, -1 if an error
234
eth_stdio_stat (const gchar *filename,
238
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
243
if (wfilename == NULL)
249
len = wcslen (wfilename);
250
while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
253
(!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
254
wfilename[len] = '\0';
256
retval = _wstat (wfilename, (struct _stat *) buf);
264
return stat (filename, buf);
270
* @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
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
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.
281
* Returns: 0 if the name was successfully deleted, -1 if an error
287
eth_stdio_unlink (const gchar *filename)
290
gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
294
if (cp_filename == NULL)
300
retval = unlink (cp_filename);
303
g_free (cp_filename);
308
return unlink (filename);
314
* @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
316
* A wrapper for the POSIX remove() function. The remove() function
317
* deletes a name from the filesystem.
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.
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
333
* Returns: 0 if the file was successfully removed, -1 if an error
339
eth_stdio_remove (const gchar *filename)
342
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
346
if (wfilename == NULL)
352
retval = _wremove (wfilename);
354
retval = _wrmdir (wfilename);
362
return remove (filename);
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
372
* A wrapper for the POSIX fopen() function. The fopen() function opens
373
* a file and associates a new stream with it.
375
* See the C library manual for more details about fopen().
377
* Returns: A <type>FILE</type> pointer if the file was successfully
378
* opened, or %NULL if an error occurred
383
eth_stdio_fopen (const gchar *filename,
387
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
392
if (wfilename == NULL)
398
wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
407
retval = _wfopen (wfilename, wmode);
416
return fopen (filename, mode);
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
425
* @stream: an existing stream which will be reused, or %NULL
427
* A wrapper for the POSIX freopen() function. The freopen() function
428
* opens a file and associates it with an existing stream.
430
* See the C library manual for more details about freopen().
432
* Returns: A <type>FILE</type> pointer if the file was successfully
433
* opened, or %NULL if an error occurred.
438
eth_stdio_freopen (const gchar *filename,
443
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
448
if (wfilename == NULL)
454
wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
463
retval = _wfreopen (wfilename, wmode, stream);
472
return freopen (filename, mode, stream);