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>
46 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
47 #error Please port this to your operating system
53 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
54 * @mode: as in access()
56 * A wrapper for the POSIX access() function. This function is used to
57 * test a pathname for one or several of read, write or execute
58 * permissions, or just existence. On Windows, the underlying access()
59 * function in the C library only checks the READONLY attribute, and
60 * does not look at the ACL at all. Software that needs to handle file
61 * permissions on Windows more exactly should use the Win32 API.
63 * See the C library manual for more details about access().
65 * Returns: zero if the pathname refers to an existing file system
66 * object that has all the tested permissions, or -1 otherwise or on
72 g_access (const gchar *filename,
76 if (G_WIN32_HAVE_WIDECHAR_API ())
78 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
82 if (wfilename == NULL)
88 retval = _waccess (wfilename, mode);
98 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
102 if (cp_filename == NULL)
108 retval = access (cp_filename, mode);
111 g_free (cp_filename);
117 return access (filename, mode);
123 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
124 * @mode: as in chmod()
126 * A wrapper for the POSIX chmod() function. The chmod() function is
127 * used to set the permissions of a file system object. Note that on
128 * Windows the file protection mechanism is not at all POSIX-like, and
129 * the underlying chmod() function in the C library just sets or
130 * clears the READONLY attribute. It does not touch any ACL. Software
131 * that needs to manage file permissions on Windows exactly should
134 * See the C library manual for more details about chmod().
136 * Returns: zero if the operation succeeded, -1 on error.
141 g_chmod (const gchar *filename,
145 if (G_WIN32_HAVE_WIDECHAR_API ())
147 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
151 if (wfilename == NULL)
157 retval = _wchmod (wfilename, mode);
167 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
171 if (cp_filename == NULL)
177 retval = chmod (cp_filename, mode);
180 g_free (cp_filename);
186 return chmod (filename, mode);
192 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
193 * @flags: as in open()
194 * @mode: as in open()
196 * A wrapper for the POSIX open() function. The open() function is
197 * used to convert a pathname into a file descriptor. Note that on
198 * POSIX systems file descriptors are implemented by the operating
199 * system. On Windows, it's the C library that implements open() and
200 * file descriptors. The actual Windows API for opening files is
201 * something different.
203 * See the C library manual for more details about open().
205 * Returns: a new file descriptor, or -1 if an error occurred. The
206 * return value can be used exactly like the return value from open().
211 g_open (const gchar *filename,
216 if (G_WIN32_HAVE_WIDECHAR_API ())
218 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
222 if (wfilename == NULL)
228 retval = _wopen (wfilename, flags, mode);
238 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
242 if (cp_filename == NULL)
248 retval = open (cp_filename, flags, mode);
251 g_free (cp_filename);
257 return open (filename, flags, mode);
263 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
264 * @mode: as in creat()
266 * A wrapper for the POSIX creat() function. The creat() function is
267 * used to convert a pathname into a file descriptor, creating a file
268 * if necessar. Note that on POSIX systems file descriptors are
269 * implemented by the operating system. On Windows, it's the C library
270 * that implements creat() and file descriptors. The actual Windows
271 * API for opening files is something different.
273 * See the C library manual for more details about creat().
275 * Returns: a new file descriptor, or -1 if an error occurred. The
276 * return value can be used exactly like the return value from creat().
281 g_creat (const gchar *filename,
285 if (G_WIN32_HAVE_WIDECHAR_API ())
287 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
291 if (wfilename == NULL)
297 retval = _wcreat (wfilename, mode);
307 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
311 if (cp_filename == NULL)
317 retval = creat (cp_filename, mode);
320 g_free (cp_filename);
326 return creat (filename, mode);
332 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
333 * @newfilename: a pathname in the GLib file name encoding
335 * A wrapper for the POSIX rename() function. The rename() function
336 * renames a file, moving it between directories if required.
338 * See your C library manual for more details about how rename() works
339 * on your system. Note in particular that on Windows, it is in
340 * general not possible to rename a file if a file with the new name
341 * already exists. Also it is not possible in general to rename an
344 * Returns: 0 if the renaming succeeded, -1 if an error occurred
349 g_rename (const gchar *oldfilename,
350 const gchar *newfilename)
353 if (G_WIN32_HAVE_WIDECHAR_API ())
355 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
356 wchar_t *wnewfilename;
360 if (woldfilename == NULL)
366 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
368 if (wnewfilename == NULL)
370 g_free (woldfilename);
375 retval = _wrename (woldfilename, wnewfilename);
378 g_free (woldfilename);
379 g_free (wnewfilename);
386 gchar *cp_oldfilename = g_locale_from_utf8 (oldfilename, -1, NULL, NULL, NULL);
387 gchar *cp_newfilename;
391 if (cp_oldfilename == NULL)
397 cp_newfilename = g_locale_from_utf8 (newfilename, -1, NULL, NULL, NULL);
399 if (cp_newfilename == NULL)
401 g_free (cp_oldfilename);
406 retval = rename (cp_oldfilename, cp_newfilename);
409 g_free (cp_oldfilename);
410 g_free (cp_newfilename);
416 return rename (oldfilename, newfilename);
422 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
423 * @mode: permissions to use for the newly created directory
425 * A wrapper for the POSIX mkdir() function. The mkdir() function
426 * attempts to create a directory with the given name and permissions.
428 * See the C library manual for more details about mkdir().
430 * Returns: 0 if the directory was successfully created, -1 if an error
436 g_mkdir (const gchar *filename,
440 if (G_WIN32_HAVE_WIDECHAR_API ())
442 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
446 if (wfilename == NULL)
452 retval = _wmkdir (wfilename);
462 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
466 if (cp_filename == NULL)
472 retval = mkdir (cp_filename);
475 g_free (cp_filename);
481 return mkdir (filename, mode);
487 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
489 * A wrapper for the POSIX chdir() function. The function changes the
490 * current directory of the process to @path.
492 * See your C library manual for more details about chdir().
494 * Returns: 0 on success, -1 if an error occurred.
499 g_chdir (const gchar *path)
502 if (G_WIN32_HAVE_WIDECHAR_API ())
504 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
514 retval = _wchdir (wpath);
524 gchar *cp_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
534 retval = chdir (cp_path);
549 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
550 * @buf: a pointer to a <structname>stat</structname> struct, which
551 * will be filled with the file information
553 * A wrapper for the POSIX stat() function. The stat() function
554 * returns information about a file.
556 * See the C library manual for more details about stat().
558 * Returns: 0 if the information was successfully retrieved, -1 if an error
564 g_stat (const gchar *filename,
568 if (G_WIN32_HAVE_WIDECHAR_API ())
570 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
574 if (wfilename == NULL)
580 retval = _wstat (wfilename, (struct _stat *) buf);
590 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
594 if (cp_filename == NULL)
600 retval = stat (cp_filename, buf);
603 g_free (cp_filename);
609 return stat (filename, buf);
615 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
616 * @buf: a pointer to a <structname>stat</structname> struct, which
617 * will be filled with the file information
619 * A wrapper for the POSIX lstat() function. The lstat() function is
620 * like stat() except that in the case of symbolic links, it returns
621 * information about the symbolic link itself and not the file that it
622 * refers to. If the system does not support symbolic links g_lstat()
623 * is identical to g_stat().
625 * See the C library manual for more details about lstat().
627 * Returns: 0 if the information was successfully retrieved, -1 if an error
633 g_lstat (const gchar *filename,
637 /* This can't be Win32, so don't do the widechar dance. */
638 return lstat (filename, buf);
640 return g_stat (filename, buf);
646 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
648 * A wrapper for the POSIX unlink() function. The unlink() function
649 * deletes a name from the filesystem. If this was the last link to the
650 * file and no processes have it opened, the diskspace occupied by the
653 * See your C library manual for more details about unlink(). Note
654 * that on Windows, it is in general not possible to delete files that
655 * are open to some process, or mapped into memory.
657 * Returns: 0 if the name was successfully deleted, -1 if an error
663 g_unlink (const gchar *filename)
666 if (G_WIN32_HAVE_WIDECHAR_API ())
668 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
672 if (wfilename == NULL)
678 retval = _wunlink (wfilename);
688 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
692 if (cp_filename == NULL)
698 retval = unlink (cp_filename);
701 g_free (cp_filename);
707 return unlink (filename);
713 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
715 * A wrapper for the POSIX remove() function. The remove() function
716 * deletes a name from the filesystem.
718 * See your C library manual for more details about how remove() works
719 * on your system. On Unix, remove() removes also directories, as it
720 * calls unlink() for files and rmdir() for directories. On Windows,
721 * although remove() in the C library only works for files, this
722 * function tries first remove() and then if that fails rmdir(), and
723 * thus works for both files and directories. Note however, that on
724 * Windows, it is in general not possible to remove a file that is
725 * open to some process, or mapped into memory.
727 * Returns: 0 if the file was successfully removed, -1 if an error
733 g_remove (const gchar *filename)
736 if (G_WIN32_HAVE_WIDECHAR_API ())
738 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
742 if (wfilename == NULL)
748 retval = _wremove (wfilename);
750 retval = _wrmdir (wfilename);
760 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
764 if (cp_filename == NULL)
770 retval = remove (cp_filename);
772 retval = rmdir (cp_filename);
775 g_free (cp_filename);
781 return remove (filename);
787 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
789 * A wrapper for the POSIX rmdir() function. The rmdir() function
790 * deletes a directory from the filesystem.
792 * See your C library manual for more details about how rmdir() works
795 * Returns: 0 if the directory was successfully removed, -1 if an error
801 g_rmdir (const gchar *filename)
804 if (G_WIN32_HAVE_WIDECHAR_API ())
806 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
810 if (wfilename == NULL)
816 retval = _wrmdir (wfilename);
826 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
830 if (cp_filename == NULL)
836 retval = rmdir (cp_filename);
839 g_free (cp_filename);
845 return rmdir (filename);
851 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
852 * @mode: a string describing the mode in which the file should be
855 * A wrapper for the POSIX fopen() function. The fopen() function opens
856 * a file and associates a new stream with it.
858 * See the C library manual for more details about fopen().
860 * Returns: A <type>FILE</type> pointer if the file was successfully
861 * opened, or %NULL if an error occurred
866 g_fopen (const gchar *filename,
870 if (G_WIN32_HAVE_WIDECHAR_API ())
872 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
877 if (wfilename == NULL)
883 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
892 retval = _wfopen (wfilename, wmode);
903 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
907 if (cp_filename == NULL)
913 retval = fopen (cp_filename, mode);
916 g_free (cp_filename);
922 return fopen (filename, mode);
928 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
929 * @mode: a string describing the mode in which the file should be
931 * @stream: an existing stream which will be reused, or %NULL
933 * A wrapper for the POSIX freopen() function. The freopen() function
934 * opens a file and associates it with an existing stream.
936 * See the C library manual for more details about freopen().
938 * Returns: A <type>FILE</type> pointer if the file was successfully
939 * opened, or %NULL if an error occurred.
944 g_freopen (const gchar *filename,
949 if (G_WIN32_HAVE_WIDECHAR_API ())
951 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
956 if (wfilename == NULL)
962 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
971 retval = _wfreopen (wfilename, wmode, stream);
982 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
986 if (cp_filename == NULL)
992 retval = freopen (cp_filename, mode, stream);
995 g_free (cp_filename);
1001 return freopen (filename, mode, stream);
1005 #define __G_STDIO_C__
1006 #include "galiasdef.c"