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>
50 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
51 #error Please port this to your operating system
54 #if defined (_MSC_VER) && !defined(_WIN64)
56 #define _wstat _wstat32
61 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
62 * @mode: as in access()
64 * A wrapper for the POSIX access() function. This function is used to
65 * test a pathname for one or several of read, write or execute
66 * permissions, or just existence.
68 * On Windows, the file protection mechanism is not at all POSIX-like,
69 * and the underlying function in the C library only checks the
70 * FAT-style READONLY attribute, and does not look at the ACL of a
71 * file at all. This function is this in practise almost useless on
72 * Windows. Software that needs to handle file permissions on Windows
73 * more exactly should use the Win32 API.
75 * See your C library manual for more details about access().
77 * Returns: zero if the pathname refers to an existing file system
78 * object that has all the tested permissions, or -1 otherwise or on
84 g_access (const gchar *filename,
88 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
92 if (wfilename == NULL)
102 retval = _waccess (wfilename, mode & ~X_OK);
110 return access (filename, mode);
116 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
117 * @mode: as in chmod()
119 * A wrapper for the POSIX chmod() function. The chmod() function is
120 * used to set the permissions of a file system object.
122 * On Windows the file protection mechanism is not at all POSIX-like,
123 * and the underlying chmod() function in the C library just sets or
124 * clears the FAT-style READONLY attribute. It does not touch any
125 * ACL. Software that needs to manage file permissions on Windows
126 * exactly should use the Win32 API.
128 * See your C library manual for more details about chmod().
130 * Returns: zero if the operation succeeded, -1 on error.
135 g_chmod (const gchar *filename,
139 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
143 if (wfilename == NULL)
149 retval = _wchmod (wfilename, mode);
157 return chmod (filename, mode);
162 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
163 * @flags: as in open()
164 * @mode: as in open()
166 * A wrapper for the POSIX open() function. The open() function is
167 * used to convert a pathname into a file descriptor.
169 * On POSIX systems file descriptors are implemented by the operating
170 * system. On Windows, it's the C library that implements open() and
171 * file descriptors. The actual Win32 API for opening files is quite
172 * different, see MSDN documentation for CreateFile(). The Win32 API
173 * uses file handles, which are more randomish integers, not small
174 * integers like file descriptors.
176 * Because file descriptors are specific to the C library on Windows,
177 * the file descriptor returned by this function makes sense only to
178 * functions in the same C library. Thus if the GLib-using code uses a
179 * different C library than GLib does, the file descriptor returned by
180 * this function cannot be passed to C library functions like write()
183 * See your C library manual for more details about open().
185 * Returns: a new file descriptor, or -1 if an error occurred. The
186 * return value can be used exactly like the return value from open().
191 g_open (const gchar *filename,
196 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
200 if (wfilename == NULL)
206 retval = _wopen (wfilename, flags, mode);
214 return open (filename, flags, mode);
220 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
221 * @mode: as in creat()
223 * A wrapper for the POSIX creat() function. The creat() function is
224 * used to convert a pathname into a file descriptor, creating a file
227 * On POSIX systems file descriptors are implemented by the operating
228 * system. On Windows, it's the C library that implements creat() and
229 * file descriptors. The actual Windows API for opening files is
230 * different, see MSDN documentation for CreateFile(). The Win32 API
231 * uses file handles, which are more randomish integers, not small
232 * integers like file descriptors.
234 * Because file descriptors are specific to the C library on Windows,
235 * the file descriptor returned by this function makes sense only to
236 * functions in the same C library. Thus if the GLib-using code uses a
237 * different C library than GLib does, the file descriptor returned by
238 * this function cannot be passed to C library functions like write()
241 * See your C library manual for more details about creat().
243 * Returns: a new file descriptor, or -1 if an error occurred. The
244 * return value can be used exactly like the return value from creat().
249 g_creat (const gchar *filename,
253 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
257 if (wfilename == NULL)
263 retval = _wcreat (wfilename, mode);
271 return creat (filename, mode);
277 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
278 * @newfilename: a pathname in the GLib file name encoding
280 * A wrapper for the POSIX rename() function. The rename() function
281 * renames a file, moving it between directories if required.
283 * See your C library manual for more details about how rename() works
284 * on your system. It is not possible in general on Windows to rename
285 * a file that is open to some process.
287 * Returns: 0 if the renaming succeeded, -1 if an error occurred
292 g_rename (const gchar *oldfilename,
293 const gchar *newfilename)
296 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
297 wchar_t *wnewfilename;
301 if (woldfilename == NULL)
307 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
309 if (wnewfilename == NULL)
311 g_free (woldfilename);
316 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
321 switch (GetLastError ())
323 #define CASE(a,b) case ERROR_##a: save_errno = b; break
324 CASE (FILE_NOT_FOUND, ENOENT);
325 CASE (PATH_NOT_FOUND, ENOENT);
326 CASE (ACCESS_DENIED, EACCES);
327 CASE (NOT_SAME_DEVICE, EXDEV);
328 CASE (LOCK_VIOLATION, EACCES);
329 CASE (SHARING_VIOLATION, EACCES);
330 CASE (FILE_EXISTS, EEXIST);
331 CASE (ALREADY_EXISTS, EEXIST);
333 default: save_errno = EIO;
337 g_free (woldfilename);
338 g_free (wnewfilename);
343 return rename (oldfilename, newfilename);
349 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
350 * @mode: permissions to use for the newly created directory
352 * A wrapper for the POSIX mkdir() function. The mkdir() function
353 * attempts to create a directory with the given name and permissions.
354 * The mode argument is ignored on Windows.
356 * See your C library manual for more details about mkdir().
358 * Returns: 0 if the directory was successfully created, -1 if an error
364 g_mkdir (const gchar *filename,
368 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
372 if (wfilename == NULL)
378 retval = _wmkdir (wfilename);
386 return mkdir (filename, mode);
392 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
394 * A wrapper for the POSIX chdir() function. The function changes the
395 * current directory of the process to @path.
397 * See your C library manual for more details about chdir().
399 * Returns: 0 on success, -1 if an error occurred.
404 g_chdir (const gchar *path)
407 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
417 retval = _wchdir (wpath);
431 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
432 * @buf: a pointer to a <structname>stat</structname> struct, which
433 * will be filled with the file information
435 * A wrapper for the POSIX stat() function. The stat() function
436 * returns information about a file. On Windows the stat() function in
437 * the C library checks only the FAT-style READONLY attribute and does
438 * not look at the ACL at all. Thus on Windows the protection bits in
439 * the st_mode field are a fabrication of little use.
441 * See your C library manual for more details about stat().
443 * Returns: 0 if the information was successfully retrieved, -1 if an error
449 g_stat (const gchar *filename,
451 struct _g_stat_struct *buf
458 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
463 if (wfilename == NULL)
469 len = wcslen (wfilename);
470 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
473 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
474 wfilename[len] = '\0';
476 retval = _wstat (wfilename, buf);
484 return stat (filename, buf);
490 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
491 * @buf: a pointer to a <structname>stat</structname> struct, which
492 * will be filled with the file information
494 * A wrapper for the POSIX lstat() function. The lstat() function is
495 * like stat() except that in the case of symbolic links, it returns
496 * information about the symbolic link itself and not the file that it
497 * refers to. If the system does not support symbolic links g_lstat()
498 * is identical to g_stat().
500 * See your C library manual for more details about lstat().
502 * Returns: 0 if the information was successfully retrieved, -1 if an error
508 g_lstat (const gchar *filename,
510 struct _g_stat_struct *buf
517 /* This can't be Win32, so don't do the widechar dance. */
518 return lstat (filename, buf);
520 return g_stat (filename, buf);
526 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
528 * A wrapper for the POSIX unlink() function. The unlink() function
529 * deletes a name from the filesystem. If this was the last link to the
530 * file and no processes have it opened, the diskspace occupied by the
533 * See your C library manual for more details about unlink(). Note
534 * that on Windows, it is in general not possible to delete files that
535 * are open to some process, or mapped into memory.
537 * Returns: 0 if the name was successfully deleted, -1 if an error
543 g_unlink (const gchar *filename)
546 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
550 if (wfilename == NULL)
556 retval = _wunlink (wfilename);
564 return unlink (filename);
570 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
572 * A wrapper for the POSIX remove() function. The remove() function
573 * deletes a name from the filesystem.
575 * See your C library manual for more details about how remove() works
576 * on your system. On Unix, remove() removes also directories, as it
577 * calls unlink() for files and rmdir() for directories. On Windows,
578 * although remove() in the C library only works for files, this
579 * function tries first remove() and then if that fails rmdir(), and
580 * thus works for both files and directories. Note however, that on
581 * Windows, it is in general not possible to remove a file that is
582 * open to some process, or mapped into memory.
584 * If this function fails on Windows you can't infer too much from the
585 * errno value. rmdir() is tried regardless of what caused remove() to
586 * fail. Any errno value set by remove() will be overwritten by that
589 * Returns: 0 if the file was successfully removed, -1 if an error
595 g_remove (const gchar *filename)
598 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
602 if (wfilename == NULL)
608 retval = _wremove (wfilename);
610 retval = _wrmdir (wfilename);
618 return remove (filename);
624 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
626 * A wrapper for the POSIX rmdir() function. The rmdir() function
627 * deletes a directory from the filesystem.
629 * See your C library manual for more details about how rmdir() works
632 * Returns: 0 if the directory was successfully removed, -1 if an error
638 g_rmdir (const gchar *filename)
641 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
645 if (wfilename == NULL)
651 retval = _wrmdir (wfilename);
659 return rmdir (filename);
665 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
666 * @mode: a string describing the mode in which the file should be
669 * A wrapper for the stdio fopen() function. The fopen() function
670 * opens a file and associates a new stream with it.
672 * Because file descriptors are specific to the C library on Windows,
673 * and a file descriptor is partof the <type>FILE</type> struct, the
674 * <type>FILE</type> pointer returned by this function makes sense
675 * only to functions in the same C library. Thus if the GLib-using
676 * code uses a different C library than GLib does, the
677 * <type>FILE</type> pointer returned by this function cannot be
678 * passed to C library functions like fprintf() or fread().
680 * See your C library manual for more details about fopen().
682 * Returns: A <type>FILE</type> pointer if the file was successfully
683 * opened, or %NULL if an error occurred
688 g_fopen (const gchar *filename,
692 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
697 if (wfilename == NULL)
703 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
712 retval = _wfopen (wfilename, wmode);
721 return fopen (filename, mode);
727 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
728 * @mode: a string describing the mode in which the file should be
730 * @stream: an existing stream which will be reused, or %NULL
732 * A wrapper for the POSIX freopen() function. The freopen() function
733 * opens a file and associates it with an existing stream.
735 * See your C library manual for more details about freopen().
737 * Returns: A <type>FILE</type> pointer if the file was successfully
738 * opened, or %NULL if an error occurred.
743 g_freopen (const gchar *filename,
748 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
753 if (wfilename == NULL)
759 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
768 retval = _wfreopen (wfilename, wmode, stream);
777 return freopen (filename, mode, stream);
783 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
784 * @utb: a pointer to a struct utimbuf.
786 * A wrapper for the POSIX utime() function. The utime() function
787 * sets the access and modification timestamps of a file.
789 * See your C library manual for more details about how utime() works
792 * Returns: 0 if the operation was successful, -1 if an error
798 g_utime (const gchar *filename,
802 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
806 if (wfilename == NULL)
812 retval = _wutime (wfilename, (struct _utimbuf*) utb);
820 return utime (filename, utb);
824 #define __G_STDIO_C__
825 #include "galiasdef.c"