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>
49 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
50 #error Please port this to your operating system
53 #if defined (_MSC_VER) && !defined(_WIN64)
55 #define _wstat _wstat32
60 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
61 * @mode: as in access()
63 * A wrapper for the POSIX access() function. This function is used to
64 * test a pathname for one or several of read, write or execute
65 * permissions, or just existence.
67 * On Windows, the file protection mechanism is not at all POSIX-like,
68 * and the underlying function in the C library only checks the
69 * FAT-style READONLY attribute, and does not look at the ACL of a
70 * file at all. This function is this in practise almost useless on
71 * Windows. Software that needs to handle file permissions on Windows
72 * more exactly should use the Win32 API.
74 * See your C library manual for more details about access().
76 * Returns: zero if the pathname refers to an existing file system
77 * object that has all the tested permissions, or -1 otherwise or on
83 g_access (const gchar *filename,
87 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
91 if (wfilename == NULL)
101 retval = _waccess (wfilename, mode & ~X_OK);
109 return access (filename, mode);
115 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
116 * @mode: as in chmod()
118 * A wrapper for the POSIX chmod() function. The chmod() function is
119 * used to set the permissions of a file system object.
121 * On Windows the file protection mechanism is not at all POSIX-like,
122 * and the underlying chmod() function in the C library just sets or
123 * clears the FAT-style READONLY attribute. It does not touch any
124 * ACL. Software that needs to manage file permissions on Windows
125 * exactly should use the Win32 API.
127 * See your C library manual for more details about chmod().
129 * Returns: zero if the operation succeeded, -1 on error.
134 g_chmod (const gchar *filename,
138 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
142 if (wfilename == NULL)
148 retval = _wchmod (wfilename, mode);
156 return chmod (filename, mode);
161 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
162 * @flags: as in open()
163 * @mode: as in open()
165 * A wrapper for the POSIX open() function. The open() function is
166 * used to convert a pathname into a file descriptor.
168 * On POSIX systems file descriptors are implemented by the operating
169 * system. On Windows, it's the C library that implements open() and
170 * file descriptors. The actual Win32 API for opening files is quite
171 * different, see MSDN documentation for CreateFile(). The Win32 API
172 * uses file handles, which are more randomish integers, not small
173 * integers like file descriptors.
175 * Because file descriptors are specific to the C library on Windows,
176 * the file descriptor returned by this function makes sense only to
177 * functions in the same C library. Thus if the GLib-using code uses a
178 * different C library than GLib does, the file descriptor returned by
179 * this function cannot be passed to C library functions like write()
182 * See your C library manual for more details about open().
184 * Returns: a new file descriptor, or -1 if an error occurred. The
185 * return value can be used exactly like the return value from open().
190 g_open (const gchar *filename,
195 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
199 if (wfilename == NULL)
205 retval = _wopen (wfilename, flags, mode);
213 return open (filename, flags, mode);
219 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
220 * @mode: as in creat()
222 * A wrapper for the POSIX creat() function. The creat() function is
223 * used to convert a pathname into a file descriptor, creating a file
226 * On POSIX systems file descriptors are implemented by the operating
227 * system. On Windows, it's the C library that implements creat() and
228 * file descriptors. The actual Windows API for opening files is
229 * different, see MSDN documentation for CreateFile(). The Win32 API
230 * uses file handles, which are more randomish integers, not small
231 * integers like file descriptors.
233 * Because file descriptors are specific to the C library on Windows,
234 * the file descriptor returned by this function makes sense only to
235 * functions in the same C library. Thus if the GLib-using code uses a
236 * different C library than GLib does, the file descriptor returned by
237 * this function cannot be passed to C library functions like write()
240 * See your C library manual for more details about creat().
242 * Returns: a new file descriptor, or -1 if an error occurred. The
243 * return value can be used exactly like the return value from creat().
248 g_creat (const gchar *filename,
252 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
256 if (wfilename == NULL)
262 retval = _wcreat (wfilename, mode);
270 return creat (filename, mode);
276 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
277 * @newfilename: a pathname in the GLib file name encoding
279 * A wrapper for the POSIX rename() function. The rename() function
280 * renames a file, moving it between directories if required.
282 * See your C library manual for more details about how rename() works
283 * on your system. It is not possible in general on Windows to rename
284 * a file that is open to some process.
286 * Returns: 0 if the renaming succeeded, -1 if an error occurred
291 g_rename (const gchar *oldfilename,
292 const gchar *newfilename)
295 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
296 wchar_t *wnewfilename;
300 if (woldfilename == NULL)
306 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
308 if (wnewfilename == NULL)
310 g_free (woldfilename);
315 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
320 switch (GetLastError ())
322 #define CASE(a,b) case ERROR_##a: save_errno = b; break
323 CASE (FILE_NOT_FOUND, ENOENT);
324 CASE (PATH_NOT_FOUND, ENOENT);
325 CASE (ACCESS_DENIED, EACCES);
326 CASE (NOT_SAME_DEVICE, EXDEV);
327 CASE (LOCK_VIOLATION, EACCES);
328 CASE (SHARING_VIOLATION, EACCES);
329 CASE (FILE_EXISTS, EEXIST);
330 CASE (ALREADY_EXISTS, EEXIST);
332 default: save_errno = EIO;
336 g_free (woldfilename);
337 g_free (wnewfilename);
342 return rename (oldfilename, newfilename);
348 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
349 * @mode: permissions to use for the newly created directory
351 * A wrapper for the POSIX mkdir() function. The mkdir() function
352 * attempts to create a directory with the given name and permissions.
353 * The mode argument is ignored on Windows.
355 * See your C library manual for more details about mkdir().
357 * Returns: 0 if the directory was successfully created, -1 if an error
363 g_mkdir (const gchar *filename,
367 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
371 if (wfilename == NULL)
377 retval = _wmkdir (wfilename);
385 return mkdir (filename, mode);
391 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
393 * A wrapper for the POSIX chdir() function. The function changes the
394 * current directory of the process to @path.
396 * See your C library manual for more details about chdir().
398 * Returns: 0 on success, -1 if an error occurred.
403 g_chdir (const gchar *path)
406 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
416 retval = _wchdir (wpath);
430 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
431 * @buf: a pointer to a <structname>stat</structname> struct, which
432 * will be filled with the file information
434 * A wrapper for the POSIX stat() function. The stat() function
435 * returns information about a file. On Windows the stat() function in
436 * the C library checks only the FAT-style READONLY attribute and does
437 * not look at the ACL at all. Thus on Windows the protection bits in
438 * the st_mode field are a fabrication of little use.
440 * On Windows the Microsoft C libraries have several variants of the
441 * <structname>stat</structname> struct and stat() function with names
442 * like "_stat", "_stat32", "_stat32i64" and "_stat64i32". The one
443 * used here is for 32-bit code the one with 32-bit size and time
444 * fields, specifically called "_stat32".
446 * In Microsoft's compiler, by default "struct stat" means one with
447 * 64-bit time fields while in MinGW "struct stat" is the legacy one
448 * with 32-bit fields. To hopefully clear up this messs, the gstdio.h
449 * header defines a type GStatBuf which is the appropriate struct type
450 * depending on the platform and/or compiler being used. On POSIX it
451 * is just "struct stat", but note that even on POSIX platforms,
452 * "stat" might be a macro.
454 * See your C library manual for more details about stat().
456 * Returns: 0 if the information was successfully retrieved, -1 if an error
462 g_stat (const gchar *filename,
466 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
471 if (wfilename == NULL)
477 len = wcslen (wfilename);
478 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
481 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
482 wfilename[len] = '\0';
484 retval = _wstat (wfilename, buf);
492 return stat (filename, buf);
498 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
499 * @buf: a pointer to a <structname>stat</structname> struct, which
500 * will be filled with the file information
502 * A wrapper for the POSIX lstat() function. The lstat() function is
503 * like stat() except that in the case of symbolic links, it returns
504 * information about the symbolic link itself and not the file that it
505 * refers to. If the system does not support symbolic links g_lstat()
506 * is identical to g_stat().
508 * See your C library manual for more details about lstat().
510 * Returns: 0 if the information was successfully retrieved, -1 if an error
516 g_lstat (const gchar *filename,
520 /* This can't be Win32, so don't do the widechar dance. */
521 return lstat (filename, buf);
523 return g_stat (filename, buf);
529 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
531 * A wrapper for the POSIX unlink() function. The unlink() function
532 * deletes a name from the filesystem. If this was the last link to the
533 * file and no processes have it opened, the diskspace occupied by the
536 * See your C library manual for more details about unlink(). Note
537 * that on Windows, it is in general not possible to delete files that
538 * are open to some process, or mapped into memory.
540 * Returns: 0 if the name was successfully deleted, -1 if an error
546 g_unlink (const gchar *filename)
549 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
553 if (wfilename == NULL)
559 retval = _wunlink (wfilename);
567 return unlink (filename);
573 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
575 * A wrapper for the POSIX remove() function. The remove() function
576 * deletes a name from the filesystem.
578 * See your C library manual for more details about how remove() works
579 * on your system. On Unix, remove() removes also directories, as it
580 * calls unlink() for files and rmdir() for directories. On Windows,
581 * although remove() in the C library only works for files, this
582 * function tries first remove() and then if that fails rmdir(), and
583 * thus works for both files and directories. Note however, that on
584 * Windows, it is in general not possible to remove a file that is
585 * open to some process, or mapped into memory.
587 * If this function fails on Windows you can't infer too much from the
588 * errno value. rmdir() is tried regardless of what caused remove() to
589 * fail. Any errno value set by remove() will be overwritten by that
592 * Returns: 0 if the file was successfully removed, -1 if an error
598 g_remove (const gchar *filename)
601 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
605 if (wfilename == NULL)
611 retval = _wremove (wfilename);
613 retval = _wrmdir (wfilename);
621 return remove (filename);
627 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
629 * A wrapper for the POSIX rmdir() function. The rmdir() function
630 * deletes a directory from the filesystem.
632 * See your C library manual for more details about how rmdir() works
635 * Returns: 0 if the directory was successfully removed, -1 if an error
641 g_rmdir (const gchar *filename)
644 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
648 if (wfilename == NULL)
654 retval = _wrmdir (wfilename);
662 return rmdir (filename);
668 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
669 * @mode: a string describing the mode in which the file should be
672 * A wrapper for the stdio fopen() function. The fopen() function
673 * opens a file and associates a new stream with it.
675 * Because file descriptors are specific to the C library on Windows,
676 * and a file descriptor is partof the <type>FILE</type> struct, the
677 * <type>FILE</type> pointer returned by this function makes sense
678 * only to functions in the same C library. Thus if the GLib-using
679 * code uses a different C library than GLib does, the
680 * <type>FILE</type> pointer returned by this function cannot be
681 * passed to C library functions like fprintf() or fread().
683 * See your C library manual for more details about fopen().
685 * Returns: A <type>FILE</type> pointer if the file was successfully
686 * opened, or %NULL if an error occurred
691 g_fopen (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 = _wfopen (wfilename, wmode);
724 return fopen (filename, mode);
730 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
731 * @mode: a string describing the mode in which the file should be
733 * @stream: an existing stream which will be reused, or %NULL
735 * A wrapper for the POSIX freopen() function. The freopen() function
736 * opens a file and associates it with an existing stream.
738 * See your C library manual for more details about freopen().
740 * Returns: A <type>FILE</type> pointer if the file was successfully
741 * opened, or %NULL if an error occurred.
746 g_freopen (const gchar *filename,
751 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
756 if (wfilename == NULL)
762 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
771 retval = _wfreopen (wfilename, wmode, stream);
780 return freopen (filename, mode, stream);
786 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
787 * @utb: a pointer to a struct utimbuf.
789 * A wrapper for the POSIX utime() function. The utime() function
790 * sets the access and modification timestamps of a file.
792 * See your C library manual for more details about how utime() works
795 * Returns: 0 if the operation was successful, -1 if an error
801 g_utime (const gchar *filename,
805 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
809 if (wfilename == NULL)
815 retval = _wutime (wfilename, (struct _utimbuf*) utb);
823 return utime (filename, utb);