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)
92 retval = _waccess (wfilename, mode & ~X_OK);
100 return access (filename, mode);
106 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
107 * @mode: as in chmod()
109 * A wrapper for the POSIX chmod() function. The chmod() function is
110 * used to set the permissions of a file system object. Note that on
111 * Windows the file protection mechanism is not at all POSIX-like, and
112 * the underlying chmod() function in the C library just sets or
113 * clears the READONLY attribute. It does not touch any ACL. Software
114 * that needs to manage file permissions on Windows exactly should
117 * See the C library manual for more details about chmod().
119 * Returns: zero if the operation succeeded, -1 on error.
124 g_chmod (const gchar *filename,
128 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
132 if (wfilename == NULL)
138 retval = _wchmod (wfilename, mode);
146 return chmod (filename, mode);
152 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
153 * @flags: as in open()
154 * @mode: as in open()
156 * A wrapper for the POSIX open() function. The open() function is
157 * used to convert a pathname into a file descriptor. Note that on
158 * POSIX systems file descriptors are implemented by the operating
159 * system. On Windows, it's the C library that implements open() and
160 * file descriptors. The actual Windows API for opening files is
161 * something different.
163 * See the C library manual for more details about open().
165 * Returns: a new file descriptor, or -1 if an error occurred. The
166 * return value can be used exactly like the return value from open().
171 g_open (const gchar *filename,
176 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
180 if (wfilename == NULL)
186 retval = _wopen (wfilename, flags, mode);
194 return open (filename, flags, mode);
200 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
201 * @mode: as in creat()
203 * A wrapper for the POSIX creat() function. The creat() function is
204 * used to convert a pathname into a file descriptor, creating a file
205 * if necessary. Note that on POSIX systems file descriptors are
206 * implemented by the operating system. On Windows, it's the C library
207 * that implements creat() and file descriptors. The actual Windows
208 * API for opening files is something different.
210 * See the C library manual for more details about creat().
212 * Returns: a new file descriptor, or -1 if an error occurred. The
213 * return value can be used exactly like the return value from creat().
218 g_creat (const gchar *filename,
222 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
226 if (wfilename == NULL)
232 retval = _wcreat (wfilename, mode);
240 return creat (filename, mode);
246 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
247 * @newfilename: a pathname in the GLib file name encoding
249 * A wrapper for the POSIX rename() function. The rename() function
250 * renames a file, moving it between directories if required.
252 * See your C library manual for more details about how rename() works
253 * on your system. Note in particular that on Win9x it is not possible
254 * to rename a file if a file with the new name already exists. Also
255 * it is not possible in general on Windows to rename an open file.
257 * Returns: 0 if the renaming succeeded, -1 if an error occurred
262 g_rename (const gchar *oldfilename,
263 const gchar *newfilename)
266 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
267 wchar_t *wnewfilename;
271 if (woldfilename == NULL)
277 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
279 if (wnewfilename == NULL)
281 g_free (woldfilename);
286 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
291 switch (GetLastError ())
293 #define CASE(a,b) case ERROR_##a: save_errno = b; break
294 CASE (FILE_NOT_FOUND, ENOENT);
295 CASE (PATH_NOT_FOUND, ENOENT);
296 CASE (ACCESS_DENIED, EACCES);
297 CASE (NOT_SAME_DEVICE, EXDEV);
298 CASE (LOCK_VIOLATION, EACCES);
299 CASE (SHARING_VIOLATION, EACCES);
300 CASE (FILE_EXISTS, EEXIST);
301 CASE (ALREADY_EXISTS, EEXIST);
303 default: save_errno = EIO;
307 g_free (woldfilename);
308 g_free (wnewfilename);
313 return rename (oldfilename, newfilename);
319 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
320 * @mode: permissions to use for the newly created directory
322 * A wrapper for the POSIX mkdir() function. The mkdir() function
323 * attempts to create a directory with the given name and permissions.
324 * The mode argument is ignored on Windows.
326 * See the C library manual for more details about mkdir().
328 * Returns: 0 if the directory was successfully created, -1 if an error
334 g_mkdir (const gchar *filename,
338 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
342 if (wfilename == NULL)
348 retval = _wmkdir (wfilename);
356 return mkdir (filename, mode);
362 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
364 * A wrapper for the POSIX chdir() function. The function changes the
365 * current directory of the process to @path.
367 * See your C library manual for more details about chdir().
369 * Returns: 0 on success, -1 if an error occurred.
374 g_chdir (const gchar *path)
377 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
387 retval = _wchdir (wpath);
401 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
402 * @buf: a pointer to a <structname>stat</structname> struct, which
403 * will be filled with the file information
405 * A wrapper for the POSIX stat() function. The stat() function
406 * returns information about a file. On Windows the stat() function in
407 * the C library checks only the READONLY attribute and does not look
408 * at the ACL at all. Thus the protection bits in the st_mode field
409 * are a fabrication of little use.
411 * See the C library manual for more details about stat().
413 * Returns: 0 if the information was successfully retrieved, -1 if an error
419 g_stat (const gchar *filename,
423 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
428 if (wfilename == NULL)
434 len = wcslen (wfilename);
435 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
438 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
439 wfilename[len] = '\0';
441 retval = _wstat (wfilename, (struct _stat *) buf);
449 return stat (filename, buf);
455 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
456 * @buf: a pointer to a <structname>stat</structname> struct, which
457 * will be filled with the file information
459 * A wrapper for the POSIX lstat() function. The lstat() function is
460 * like stat() except that in the case of symbolic links, it returns
461 * information about the symbolic link itself and not the file that it
462 * refers to. If the system does not support symbolic links g_lstat()
463 * is identical to g_stat().
465 * See the C library manual for more details about lstat().
467 * Returns: 0 if the information was successfully retrieved, -1 if an error
473 g_lstat (const gchar *filename,
477 /* This can't be Win32, so don't do the widechar dance. */
478 return lstat (filename, buf);
480 return g_stat (filename, buf);
486 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
488 * A wrapper for the POSIX unlink() function. The unlink() function
489 * deletes a name from the filesystem. If this was the last link to the
490 * file and no processes have it opened, the diskspace occupied by the
493 * See your C library manual for more details about unlink(). Note
494 * that on Windows, it is in general not possible to delete files that
495 * are open to some process, or mapped into memory.
497 * Returns: 0 if the name was successfully deleted, -1 if an error
503 g_unlink (const gchar *filename)
506 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
510 if (wfilename == NULL)
516 retval = _wunlink (wfilename);
524 return unlink (filename);
530 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
532 * A wrapper for the POSIX remove() function. The remove() function
533 * deletes a name from the filesystem.
535 * See your C library manual for more details about how remove() works
536 * on your system. On Unix, remove() removes also directories, as it
537 * calls unlink() for files and rmdir() for directories. On Windows,
538 * although remove() in the C library only works for files, this
539 * function tries first remove() and then if that fails rmdir(), and
540 * thus works for both files and directories. Note however, that on
541 * Windows, it is in general not possible to remove a file that is
542 * open to some process, or mapped into memory.
544 * If this function fails on Windows you can't infer too much from the
545 * errno value. rmdir() is tried regardless of what caused remove() to
546 * fail. Any errno value set by remove() will be overwritten by that
549 * Returns: 0 if the file was successfully removed, -1 if an error
555 g_remove (const gchar *filename)
558 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
562 if (wfilename == NULL)
568 retval = _wremove (wfilename);
570 retval = _wrmdir (wfilename);
578 return remove (filename);
584 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
586 * A wrapper for the POSIX rmdir() function. The rmdir() function
587 * deletes a directory from the filesystem.
589 * See your C library manual for more details about how rmdir() works
592 * Returns: 0 if the directory was successfully removed, -1 if an error
598 g_rmdir (const gchar *filename)
601 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
605 if (wfilename == NULL)
611 retval = _wrmdir (wfilename);
619 return rmdir (filename);
625 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
626 * @mode: a string describing the mode in which the file should be
629 * A wrapper for the POSIX fopen() function. The fopen() function opens
630 * a file and associates a new stream with it.
632 * See the C library manual for more details about fopen().
634 * Returns: A <type>FILE</type> pointer if the file was successfully
635 * opened, or %NULL if an error occurred
640 g_fopen (const gchar *filename,
644 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
649 if (wfilename == NULL)
655 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
664 retval = _wfopen (wfilename, wmode);
673 return fopen (filename, mode);
679 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
680 * @mode: a string describing the mode in which the file should be
682 * @stream: an existing stream which will be reused, or %NULL
684 * A wrapper for the POSIX freopen() function. The freopen() function
685 * opens a file and associates it with an existing stream.
687 * See the C library manual for more details about freopen().
689 * Returns: A <type>FILE</type> pointer if the file was successfully
690 * opened, or %NULL if an error occurred.
695 g_freopen (const gchar *filename,
700 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
705 if (wfilename == NULL)
711 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
720 retval = _wfreopen (wfilename, wmode, stream);
729 return freopen (filename, mode, stream);
735 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
736 * @utb: a pointer to a struct utimbuf.
738 * A wrapper for the POSIX utime() function. The utime() function
739 * sets the access and modification timestamps of a file.
741 * See your C library manual for more details about how utime() works
744 * Returns: 0 if the operation was successful, -1 if an error
750 g_utime (const gchar *filename,
754 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
758 if (wfilename == NULL)
764 retval = _wutime (wfilename, (struct _utimbuf*) utb);
772 return utime (filename, utb);
776 #define __G_STDIO_C__
777 #include "galiasdef.c"