1 /* gstdio.c - wrappers for C library functions
3 * Copyright 2004 Tor Lillqvist
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #define G_STDIO_NO_WRAP_ON_UNIX
27 #include <sys/types.h>
47 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
48 #error Please port this to your operating system
54 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
55 * @mode: as in access()
57 * A wrapper for the POSIX access() function. This function is used to
58 * test a pathname for one or several of read, write or execute
59 * permissions, or just existence. On Windows, the underlying access()
60 * function in the C library only checks the READONLY attribute, and
61 * does not look at the ACL at all. Software that needs to handle file
62 * permissions on Windows more exactly should use the Win32 API.
64 * See the C library manual for more details about access().
66 * Returns: zero if the pathname refers to an existing file system
67 * object that has all the tested permissions, or -1 otherwise or on
73 g_access (const gchar *filename,
77 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
81 if (wfilename == NULL)
87 retval = _waccess (wfilename, mode);
95 return access (filename, mode);
101 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
102 * @mode: as in chmod()
104 * A wrapper for the POSIX chmod() function. The chmod() function is
105 * used to set the permissions of a file system object. Note that on
106 * Windows the file protection mechanism is not at all POSIX-like, and
107 * the underlying chmod() function in the C library just sets or
108 * clears the READONLY attribute. It does not touch any ACL. Software
109 * that needs to manage file permissions on Windows exactly should
112 * See the C library manual for more details about chmod().
114 * Returns: zero if the operation succeeded, -1 on error.
119 g_chmod (const gchar *filename,
123 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
127 if (wfilename == NULL)
133 retval = _wchmod (wfilename, mode);
141 return chmod (filename, mode);
147 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
148 * @flags: as in open()
149 * @mode: as in open()
151 * A wrapper for the POSIX open() function. The open() function is
152 * used to convert a pathname into a file descriptor. Note that on
153 * POSIX systems file descriptors are implemented by the operating
154 * system. On Windows, it's the C library that implements open() and
155 * file descriptors. The actual Windows API for opening files is
156 * something different.
158 * See the C library manual for more details about open().
160 * Returns: a new file descriptor, or -1 if an error occurred. The
161 * return value can be used exactly like the return value from open().
166 g_open (const gchar *filename,
171 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
175 if (wfilename == NULL)
181 retval = _wopen (wfilename, flags, mode);
189 return open (filename, flags, mode);
195 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
196 * @mode: as in creat()
198 * A wrapper for the POSIX creat() function. The creat() function is
199 * used to convert a pathname into a file descriptor, creating a file
200 * if necessar. Note that on POSIX systems file descriptors are
201 * implemented by the operating system. On Windows, it's the C library
202 * that implements creat() and file descriptors. The actual Windows
203 * API for opening files is something different.
205 * See the C library manual for more details about creat().
207 * Returns: a new file descriptor, or -1 if an error occurred. The
208 * return value can be used exactly like the return value from creat().
213 g_creat (const gchar *filename,
217 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
221 if (wfilename == NULL)
227 retval = _wcreat (wfilename, mode);
235 return creat (filename, mode);
241 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
242 * @newfilename: a pathname in the GLib file name encoding
244 * A wrapper for the POSIX rename() function. The rename() function
245 * renames a file, moving it between directories if required.
247 * See your C library manual for more details about how rename() works
248 * on your system. Note in particular that on Win9x it is not possible
249 * to rename a file if a file with the new name already exists. Also
250 * it is not possible in general on Windows to rename an open file.
252 * Returns: 0 if the renaming succeeded, -1 if an error occurred
257 g_rename (const gchar *oldfilename,
258 const gchar *newfilename)
261 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
262 wchar_t *wnewfilename;
266 if (woldfilename == NULL)
272 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
274 if (wnewfilename == NULL)
276 g_free (woldfilename);
281 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
286 switch (GetLastError ())
288 #define CASE(a,b) case ERROR_##a: save_errno = b; break
289 CASE (FILE_NOT_FOUND, ENOENT);
290 CASE (PATH_NOT_FOUND, ENOENT);
291 CASE (ACCESS_DENIED, EACCES);
292 CASE (NOT_SAME_DEVICE, EXDEV);
293 CASE (LOCK_VIOLATION, EACCES);
294 CASE (SHARING_VIOLATION, EACCES);
295 CASE (FILE_EXISTS, EEXIST);
296 CASE (ALREADY_EXISTS, EEXIST);
298 default: save_errno = EIO;
302 g_free (woldfilename);
303 g_free (wnewfilename);
308 return rename (oldfilename, newfilename);
314 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
315 * @mode: permissions to use for the newly created directory
317 * A wrapper for the POSIX mkdir() function. The mkdir() function
318 * attempts to create a directory with the given name and permissions.
319 * The mode argument is ignored on Windows.
321 * See the C library manual for more details about mkdir().
323 * Returns: 0 if the directory was successfully created, -1 if an error
329 g_mkdir (const gchar *filename,
333 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
337 if (wfilename == NULL)
343 retval = _wmkdir (wfilename);
351 return mkdir (filename, mode);
357 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
359 * A wrapper for the POSIX chdir() function. The function changes the
360 * current directory of the process to @path.
362 * See your C library manual for more details about chdir().
364 * Returns: 0 on success, -1 if an error occurred.
369 g_chdir (const gchar *path)
372 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
382 retval = _wchdir (wpath);
396 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
397 * @buf: a pointer to a <structname>stat</structname> struct, which
398 * will be filled with the file information
400 * A wrapper for the POSIX stat() function. The stat() function
401 * returns information about a file. On Windows the stat() function in
402 * the C library checks only the READONLY attribute and does not look
403 * at the ACL at all. Thus the protection bits in the st_mode field
404 * are a fabrication of little use.
406 * See the C library manual for more details about stat().
408 * Returns: 0 if the information was successfully retrieved, -1 if an error
414 g_stat (const gchar *filename,
418 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
423 if (wfilename == NULL)
429 len = wcslen (wfilename);
430 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
433 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
434 wfilename[len] = '\0';
436 retval = _wstat (wfilename, (struct _stat *) buf);
444 return stat (filename, buf);
450 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
451 * @buf: a pointer to a <structname>stat</structname> struct, which
452 * will be filled with the file information
454 * A wrapper for the POSIX lstat() function. The lstat() function is
455 * like stat() except that in the case of symbolic links, it returns
456 * information about the symbolic link itself and not the file that it
457 * refers to. If the system does not support symbolic links g_lstat()
458 * is identical to g_stat().
460 * See the C library manual for more details about lstat().
462 * Returns: 0 if the information was successfully retrieved, -1 if an error
468 g_lstat (const gchar *filename,
472 /* This can't be Win32, so don't do the widechar dance. */
473 return lstat (filename, buf);
475 return g_stat (filename, buf);
481 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
483 * A wrapper for the POSIX unlink() function. The unlink() function
484 * deletes a name from the filesystem. If this was the last link to the
485 * file and no processes have it opened, the diskspace occupied by the
488 * See your C library manual for more details about unlink(). Note
489 * that on Windows, it is in general not possible to delete files that
490 * are open to some process, or mapped into memory.
492 * Returns: 0 if the name was successfully deleted, -1 if an error
498 g_unlink (const gchar *filename)
501 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
505 if (wfilename == NULL)
511 retval = _wunlink (wfilename);
519 return unlink (filename);
525 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
527 * A wrapper for the POSIX remove() function. The remove() function
528 * deletes a name from the filesystem.
530 * See your C library manual for more details about how remove() works
531 * on your system. On Unix, remove() removes also directories, as it
532 * calls unlink() for files and rmdir() for directories. On Windows,
533 * although remove() in the C library only works for files, this
534 * function tries first remove() and then if that fails rmdir(), and
535 * thus works for both files and directories. Note however, that on
536 * Windows, it is in general not possible to remove a file that is
537 * open to some process, or mapped into memory.
539 * If this function fails on Windows you can't infer too much from the
540 * errno value. rmdir() is tried regardless of what caused remove() to
541 * fail. Any errno value set by remove() will be overwritten by that
544 * Returns: 0 if the file was successfully removed, -1 if an error
550 g_remove (const gchar *filename)
553 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
557 if (wfilename == NULL)
563 retval = _wremove (wfilename);
565 retval = _wrmdir (wfilename);
573 return remove (filename);
579 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
581 * A wrapper for the POSIX rmdir() function. The rmdir() function
582 * deletes a directory from the filesystem.
584 * See your C library manual for more details about how rmdir() works
587 * Returns: 0 if the directory was successfully removed, -1 if an error
593 g_rmdir (const gchar *filename)
596 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
600 if (wfilename == NULL)
606 retval = _wrmdir (wfilename);
614 return rmdir (filename);
620 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
621 * @mode: a string describing the mode in which the file should be
624 * A wrapper for the POSIX fopen() function. The fopen() function opens
625 * a file and associates a new stream with it.
627 * See the C library manual for more details about fopen().
629 * Returns: A <type>FILE</type> pointer if the file was successfully
630 * opened, or %NULL if an error occurred
635 g_fopen (const gchar *filename,
639 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
644 if (wfilename == NULL)
650 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
659 retval = _wfopen (wfilename, wmode);
668 return fopen (filename, mode);
674 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
675 * @mode: a string describing the mode in which the file should be
677 * @stream: an existing stream which will be reused, or %NULL
679 * A wrapper for the POSIX freopen() function. The freopen() function
680 * opens a file and associates it with an existing stream.
682 * See the C library manual for more details about freopen().
684 * Returns: A <type>FILE</type> pointer if the file was successfully
685 * opened, or %NULL if an error occurred.
690 g_freopen (const gchar *filename,
695 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
700 if (wfilename == NULL)
706 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
715 retval = _wfreopen (wfilename, wmode, stream);
724 return freopen (filename, mode, stream);
728 #define __G_STDIO_C__
729 #include "galiasdef.c"