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>
41 #include <sys/utime.h>
48 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
49 #error Please port this to your operating system
55 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
56 * @mode: as in access()
58 * A wrapper for the POSIX access() function. This function is used to
59 * test a pathname for one or several of read, write or execute
60 * permissions, or just existence. On Windows, the underlying access()
61 * function in the C library only checks the READONLY attribute, and
62 * does not look at the ACL at all. Software that needs to handle file
63 * permissions on Windows more exactly should use the Win32 API.
65 * See the C library manual for more details about access().
67 * Returns: zero if the pathname refers to an existing file system
68 * object that has all the tested permissions, or -1 otherwise or on
74 g_access (const gchar *filename,
78 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
82 if (wfilename == NULL)
88 retval = _waccess (wfilename, mode & ~X_OK);
96 return access (filename, mode);
102 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
103 * @mode: as in chmod()
105 * A wrapper for the POSIX chmod() function. The chmod() function is
106 * used to set the permissions of a file system object. Note that on
107 * Windows the file protection mechanism is not at all POSIX-like, and
108 * the underlying chmod() function in the C library just sets or
109 * clears the READONLY attribute. It does not touch any ACL. Software
110 * that needs to manage file permissions on Windows exactly should
113 * See the C library manual for more details about chmod().
115 * Returns: zero if the operation succeeded, -1 on error.
120 g_chmod (const gchar *filename,
124 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
128 if (wfilename == NULL)
134 retval = _wchmod (wfilename, mode);
142 return chmod (filename, mode);
148 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
149 * @flags: as in open()
150 * @mode: as in open()
152 * A wrapper for the POSIX open() function. The open() function is
153 * used to convert a pathname into a file descriptor. Note that on
154 * POSIX systems file descriptors are implemented by the operating
155 * system. On Windows, it's the C library that implements open() and
156 * file descriptors. The actual Windows API for opening files is
157 * something different.
159 * See the C library manual for more details about open().
161 * Returns: a new file descriptor, or -1 if an error occurred. The
162 * return value can be used exactly like the return value from open().
167 g_open (const gchar *filename,
172 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
176 if (wfilename == NULL)
182 retval = _wopen (wfilename, flags, mode);
190 return open (filename, flags, mode);
196 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
197 * @mode: as in creat()
199 * A wrapper for the POSIX creat() function. The creat() function is
200 * used to convert a pathname into a file descriptor, creating a file
201 * if necessary. Note that on POSIX systems file descriptors are
202 * implemented by the operating system. On Windows, it's the C library
203 * that implements creat() and file descriptors. The actual Windows
204 * API for opening files is something different.
206 * See the C library manual for more details about creat().
208 * Returns: a new file descriptor, or -1 if an error occurred. The
209 * return value can be used exactly like the return value from creat().
214 g_creat (const gchar *filename,
218 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
222 if (wfilename == NULL)
228 retval = _wcreat (wfilename, mode);
236 return creat (filename, mode);
242 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
243 * @newfilename: a pathname in the GLib file name encoding
245 * A wrapper for the POSIX rename() function. The rename() function
246 * renames a file, moving it between directories if required.
248 * See your C library manual for more details about how rename() works
249 * on your system. Note in particular that on Win9x it is not possible
250 * to rename a file if a file with the new name already exists. Also
251 * it is not possible in general on Windows to rename an open file.
253 * Returns: 0 if the renaming succeeded, -1 if an error occurred
258 g_rename (const gchar *oldfilename,
259 const gchar *newfilename)
262 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
263 wchar_t *wnewfilename;
267 if (woldfilename == NULL)
273 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
275 if (wnewfilename == NULL)
277 g_free (woldfilename);
282 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
287 switch (GetLastError ())
289 #define CASE(a,b) case ERROR_##a: save_errno = b; break
290 CASE (FILE_NOT_FOUND, ENOENT);
291 CASE (PATH_NOT_FOUND, ENOENT);
292 CASE (ACCESS_DENIED, EACCES);
293 CASE (NOT_SAME_DEVICE, EXDEV);
294 CASE (LOCK_VIOLATION, EACCES);
295 CASE (SHARING_VIOLATION, EACCES);
296 CASE (FILE_EXISTS, EEXIST);
297 CASE (ALREADY_EXISTS, EEXIST);
299 default: save_errno = EIO;
303 g_free (woldfilename);
304 g_free (wnewfilename);
309 return rename (oldfilename, newfilename);
315 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
316 * @mode: permissions to use for the newly created directory
318 * A wrapper for the POSIX mkdir() function. The mkdir() function
319 * attempts to create a directory with the given name and permissions.
320 * The mode argument is ignored on Windows.
322 * See the C library manual for more details about mkdir().
324 * Returns: 0 if the directory was successfully created, -1 if an error
330 g_mkdir (const gchar *filename,
334 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
338 if (wfilename == NULL)
344 retval = _wmkdir (wfilename);
352 return mkdir (filename, mode);
358 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
360 * A wrapper for the POSIX chdir() function. The function changes the
361 * current directory of the process to @path.
363 * See your C library manual for more details about chdir().
365 * Returns: 0 on success, -1 if an error occurred.
370 g_chdir (const gchar *path)
373 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
383 retval = _wchdir (wpath);
397 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
398 * @buf: a pointer to a <structname>stat</structname> struct, which
399 * will be filled with the file information
401 * A wrapper for the POSIX stat() function. The stat() function
402 * returns information about a file. On Windows the stat() function in
403 * the C library checks only the READONLY attribute and does not look
404 * at the ACL at all. Thus the protection bits in the st_mode field
405 * are a fabrication of little use.
407 * See the C library manual for more details about stat().
409 * Returns: 0 if the information was successfully retrieved, -1 if an error
415 g_stat (const gchar *filename,
419 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
424 if (wfilename == NULL)
430 len = wcslen (wfilename);
431 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
434 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
435 wfilename[len] = '\0';
437 retval = _wstat (wfilename, (struct _stat *) buf);
445 return stat (filename, buf);
451 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
452 * @buf: a pointer to a <structname>stat</structname> struct, which
453 * will be filled with the file information
455 * A wrapper for the POSIX lstat() function. The lstat() function is
456 * like stat() except that in the case of symbolic links, it returns
457 * information about the symbolic link itself and not the file that it
458 * refers to. If the system does not support symbolic links g_lstat()
459 * is identical to g_stat().
461 * See the C library manual for more details about lstat().
463 * Returns: 0 if the information was successfully retrieved, -1 if an error
469 g_lstat (const gchar *filename,
473 /* This can't be Win32, so don't do the widechar dance. */
474 return lstat (filename, buf);
476 return g_stat (filename, buf);
482 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
484 * A wrapper for the POSIX unlink() function. The unlink() function
485 * deletes a name from the filesystem. If this was the last link to the
486 * file and no processes have it opened, the diskspace occupied by the
489 * See your C library manual for more details about unlink(). Note
490 * that on Windows, it is in general not possible to delete files that
491 * are open to some process, or mapped into memory.
493 * Returns: 0 if the name was successfully deleted, -1 if an error
499 g_unlink (const gchar *filename)
502 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
506 if (wfilename == NULL)
512 retval = _wunlink (wfilename);
520 return unlink (filename);
526 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
528 * A wrapper for the POSIX remove() function. The remove() function
529 * deletes a name from the filesystem.
531 * See your C library manual for more details about how remove() works
532 * on your system. On Unix, remove() removes also directories, as it
533 * calls unlink() for files and rmdir() for directories. On Windows,
534 * although remove() in the C library only works for files, this
535 * function tries first remove() and then if that fails rmdir(), and
536 * thus works for both files and directories. Note however, that on
537 * Windows, it is in general not possible to remove a file that is
538 * open to some process, or mapped into memory.
540 * If this function fails on Windows you can't infer too much from the
541 * errno value. rmdir() is tried regardless of what caused remove() to
542 * fail. Any errno value set by remove() will be overwritten by that
545 * Returns: 0 if the file was successfully removed, -1 if an error
551 g_remove (const gchar *filename)
554 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
558 if (wfilename == NULL)
564 retval = _wremove (wfilename);
566 retval = _wrmdir (wfilename);
574 return remove (filename);
580 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
582 * A wrapper for the POSIX rmdir() function. The rmdir() function
583 * deletes a directory from the filesystem.
585 * See your C library manual for more details about how rmdir() works
588 * Returns: 0 if the directory was successfully removed, -1 if an error
594 g_rmdir (const gchar *filename)
597 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
601 if (wfilename == NULL)
607 retval = _wrmdir (wfilename);
615 return rmdir (filename);
621 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
622 * @mode: a string describing the mode in which the file should be
625 * A wrapper for the POSIX fopen() function. The fopen() function opens
626 * a file and associates a new stream with it.
628 * See the C library manual for more details about fopen().
630 * Returns: A <type>FILE</type> pointer if the file was successfully
631 * opened, or %NULL if an error occurred
636 g_fopen (const gchar *filename,
640 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
645 if (wfilename == NULL)
651 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
660 retval = _wfopen (wfilename, wmode);
669 return fopen (filename, mode);
675 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
676 * @mode: a string describing the mode in which the file should be
678 * @stream: an existing stream which will be reused, or %NULL
680 * A wrapper for the POSIX freopen() function. The freopen() function
681 * opens a file and associates it with an existing stream.
683 * See the C library manual for more details about freopen().
685 * Returns: A <type>FILE</type> pointer if the file was successfully
686 * opened, or %NULL if an error occurred.
691 g_freopen (const gchar *filename,
696 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
701 if (wfilename == NULL)
707 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
716 retval = _wfreopen (wfilename, wmode, stream);
725 return freopen (filename, mode, stream);
731 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
732 * @utb: a pointer to a struct utimbuf.
734 * A wrapper for the POSIX utime() function. The utime() function
735 * sets the access and modification timestamps of a file.
737 * See your C library manual for more details about how utime() works
740 * Returns: 0 if the operation was successful, -1 if an error
746 g_utime (const gchar *filename,
750 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
754 if (wfilename == NULL)
760 retval = _wutime (wfilename, (struct _utimbuf*) utb);
768 return utime (filename, utb);
772 #define __G_STDIO_C__
773 #include "galiasdef.c"