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>
47 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
48 #error Please port this to your operating system
54 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
55 * @mode: as in access()
57 * A wrapper for the POSIX access() function. This function is used to
58 * test a pathname for one or several of read, write or execute
59 * permissions, or just existence. On Windows, the underlying access()
60 * function in the C library only checks the READONLY attribute, and
61 * does not look at the ACL at all. Software that needs to handle file
62 * permissions on Windows more exactly should use the Win32 API.
64 * See the C library manual for more details about access().
66 * Returns: zero if the pathname refers to an existing file system
67 * object that has all the tested permissions, or -1 otherwise or on
73 g_access (const gchar *filename,
77 if (G_WIN32_HAVE_WIDECHAR_API ())
79 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
83 if (wfilename == NULL)
89 retval = _waccess (wfilename, mode);
99 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
103 if (cp_filename == NULL)
109 retval = access (cp_filename, mode);
112 g_free (cp_filename);
118 return access (filename, mode);
124 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
125 * @mode: as in chmod()
127 * A wrapper for the POSIX chmod() function. The chmod() function is
128 * used to set the permissions of a file system object. Note that on
129 * Windows the file protection mechanism is not at all POSIX-like, and
130 * the underlying chmod() function in the C library just sets or
131 * clears the READONLY attribute. It does not touch any ACL. Software
132 * that needs to manage file permissions on Windows exactly should
135 * See the C library manual for more details about chmod().
137 * Returns: zero if the operation succeeded, -1 on error.
142 g_chmod (const gchar *filename,
146 if (G_WIN32_HAVE_WIDECHAR_API ())
148 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
152 if (wfilename == NULL)
158 retval = _wchmod (wfilename, mode);
168 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
172 if (cp_filename == NULL)
178 retval = chmod (cp_filename, mode);
181 g_free (cp_filename);
187 return chmod (filename, mode);
193 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
194 * @flags: as in open()
195 * @mode: as in open()
197 * A wrapper for the POSIX open() function. The open() function is
198 * used to convert a pathname into a file descriptor. Note that on
199 * POSIX systems file descriptors are implemented by the operating
200 * system. On Windows, it's the C library that implements open() and
201 * file descriptors. The actual Windows API for opening files is
202 * something different.
204 * See the C library manual for more details about open().
206 * Returns: a new file descriptor, or -1 if an error occurred. The
207 * return value can be used exactly like the return value from open().
212 g_open (const gchar *filename,
217 if (G_WIN32_HAVE_WIDECHAR_API ())
219 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
223 if (wfilename == NULL)
229 retval = _wopen (wfilename, flags, mode);
239 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
243 if (cp_filename == NULL)
249 retval = open (cp_filename, flags, mode);
252 g_free (cp_filename);
258 return open (filename, flags, mode);
264 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
265 * @mode: as in creat()
267 * A wrapper for the POSIX creat() function. The creat() function is
268 * used to convert a pathname into a file descriptor, creating a file
269 * if necessar. Note that on POSIX systems file descriptors are
270 * implemented by the operating system. On Windows, it's the C library
271 * that implements creat() and file descriptors. The actual Windows
272 * API for opening files is something different.
274 * See the C library manual for more details about creat().
276 * Returns: a new file descriptor, or -1 if an error occurred. The
277 * return value can be used exactly like the return value from creat().
282 g_creat (const gchar *filename,
286 if (G_WIN32_HAVE_WIDECHAR_API ())
288 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
292 if (wfilename == NULL)
298 retval = _wcreat (wfilename, mode);
308 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
312 if (cp_filename == NULL)
318 retval = creat (cp_filename, mode);
321 g_free (cp_filename);
327 return creat (filename, mode);
333 * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
334 * @newfilename: a pathname in the GLib file name encoding
336 * A wrapper for the POSIX rename() function. The rename() function
337 * renames a file, moving it between directories if required.
339 * See your C library manual for more details about how rename() works
340 * on your system. Note in particular that on Win9x it is not possible
341 * to rename a file if a file with the new name already exists. Also
342 * it is not possible in general on Windows to rename an open file.
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 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
380 switch (GetLastError ())
382 #define CASE(a,b) case ERROR_##a: save_errno = b; break
383 CASE (FILE_NOT_FOUND, ENOENT);
384 CASE (PATH_NOT_FOUND, ENOENT);
385 CASE (ACCESS_DENIED, EACCES);
386 CASE (NOT_SAME_DEVICE, EXDEV);
387 CASE (LOCK_VIOLATION, EACCES);
388 CASE (SHARING_VIOLATION, EACCES);
389 CASE (FILE_EXISTS, EEXIST);
390 CASE (ALREADY_EXISTS, EEXIST);
392 default: save_errno = EIO;
396 g_free (woldfilename);
397 g_free (wnewfilename);
404 gchar *cp_oldfilename = g_locale_from_utf8 (oldfilename, -1, NULL, NULL, NULL);
405 gchar *cp_newfilename;
409 if (cp_oldfilename == NULL)
415 cp_newfilename = g_locale_from_utf8 (newfilename, -1, NULL, NULL, NULL);
417 if (cp_newfilename == NULL)
419 g_free (cp_oldfilename);
424 retval = rename (cp_oldfilename, cp_newfilename);
427 g_free (cp_oldfilename);
428 g_free (cp_newfilename);
434 return rename (oldfilename, newfilename);
440 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
441 * @mode: permissions to use for the newly created directory
443 * A wrapper for the POSIX mkdir() function. The mkdir() function
444 * attempts to create a directory with the given name and permissions.
446 * See the C library manual for more details about mkdir().
448 * Returns: 0 if the directory was successfully created, -1 if an error
454 g_mkdir (const gchar *filename,
458 if (G_WIN32_HAVE_WIDECHAR_API ())
460 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
464 if (wfilename == NULL)
470 retval = _wmkdir (wfilename);
480 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
484 if (cp_filename == NULL)
490 retval = mkdir (cp_filename);
493 g_free (cp_filename);
499 return mkdir (filename, mode);
505 * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
507 * A wrapper for the POSIX chdir() function. The function changes the
508 * current directory of the process to @path.
510 * See your C library manual for more details about chdir().
512 * Returns: 0 on success, -1 if an error occurred.
517 g_chdir (const gchar *path)
520 if (G_WIN32_HAVE_WIDECHAR_API ())
522 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
532 retval = _wchdir (wpath);
542 gchar *cp_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
552 retval = chdir (cp_path);
567 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
568 * @buf: a pointer to a <structname>stat</structname> struct, which
569 * will be filled with the file information
571 * A wrapper for the POSIX stat() function. The stat() function
572 * returns information about a file.
574 * See the C library manual for more details about stat().
576 * Returns: 0 if the information was successfully retrieved, -1 if an error
582 g_stat (const gchar *filename,
586 if (G_WIN32_HAVE_WIDECHAR_API ())
588 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
593 if (wfilename == NULL)
599 len = wcslen (wfilename);
600 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
603 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
604 wfilename[len] = '\0';
606 retval = _wstat (wfilename, (struct _stat *) buf);
616 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
621 if (cp_filename == NULL)
627 len = strlen (cp_filename);
628 while (len > 0 && G_IS_DIR_SEPARATOR (cp_filename[len-1]))
631 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
632 cp_filename[len] = '\0';
634 retval = stat (cp_filename, buf);
637 g_free (cp_filename);
643 return stat (filename, buf);
649 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
650 * @buf: a pointer to a <structname>stat</structname> struct, which
651 * will be filled with the file information
653 * A wrapper for the POSIX lstat() function. The lstat() function is
654 * like stat() except that in the case of symbolic links, it returns
655 * information about the symbolic link itself and not the file that it
656 * refers to. If the system does not support symbolic links g_lstat()
657 * is identical to g_stat().
659 * See the C library manual for more details about lstat().
661 * Returns: 0 if the information was successfully retrieved, -1 if an error
667 g_lstat (const gchar *filename,
671 /* This can't be Win32, so don't do the widechar dance. */
672 return lstat (filename, buf);
674 return g_stat (filename, buf);
680 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
682 * A wrapper for the POSIX unlink() function. The unlink() function
683 * deletes a name from the filesystem. If this was the last link to the
684 * file and no processes have it opened, the diskspace occupied by the
687 * See your C library manual for more details about unlink(). Note
688 * that on Windows, it is in general not possible to delete files that
689 * are open to some process, or mapped into memory.
691 * Returns: 0 if the name was successfully deleted, -1 if an error
697 g_unlink (const gchar *filename)
700 if (G_WIN32_HAVE_WIDECHAR_API ())
702 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
706 if (wfilename == NULL)
712 retval = _wunlink (wfilename);
722 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
726 if (cp_filename == NULL)
732 retval = unlink (cp_filename);
735 g_free (cp_filename);
741 return unlink (filename);
747 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
749 * A wrapper for the POSIX remove() function. The remove() function
750 * deletes a name from the filesystem.
752 * See your C library manual for more details about how remove() works
753 * on your system. On Unix, remove() removes also directories, as it
754 * calls unlink() for files and rmdir() for directories. On Windows,
755 * although remove() in the C library only works for files, this
756 * function tries first remove() and then if that fails rmdir(), and
757 * thus works for both files and directories. Note however, that on
758 * Windows, it is in general not possible to remove a file that is
759 * open to some process, or mapped into memory.
761 * Returns: 0 if the file was successfully removed, -1 if an error
767 g_remove (const gchar *filename)
770 if (G_WIN32_HAVE_WIDECHAR_API ())
772 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
776 if (wfilename == NULL)
782 retval = _wremove (wfilename);
784 retval = _wrmdir (wfilename);
794 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
798 if (cp_filename == NULL)
804 retval = remove (cp_filename);
806 retval = rmdir (cp_filename);
809 g_free (cp_filename);
815 return remove (filename);
821 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
823 * A wrapper for the POSIX rmdir() function. The rmdir() function
824 * deletes a directory from the filesystem.
826 * See your C library manual for more details about how rmdir() works
829 * Returns: 0 if the directory was successfully removed, -1 if an error
835 g_rmdir (const gchar *filename)
838 if (G_WIN32_HAVE_WIDECHAR_API ())
840 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
844 if (wfilename == NULL)
850 retval = _wrmdir (wfilename);
860 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
864 if (cp_filename == NULL)
870 retval = rmdir (cp_filename);
873 g_free (cp_filename);
879 return rmdir (filename);
885 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
886 * @mode: a string describing the mode in which the file should be
889 * A wrapper for the POSIX fopen() function. The fopen() function opens
890 * a file and associates a new stream with it.
892 * See the C library manual for more details about fopen().
894 * Returns: A <type>FILE</type> pointer if the file was successfully
895 * opened, or %NULL if an error occurred
900 g_fopen (const gchar *filename,
904 if (G_WIN32_HAVE_WIDECHAR_API ())
906 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
911 if (wfilename == NULL)
917 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
926 retval = _wfopen (wfilename, wmode);
937 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
941 if (cp_filename == NULL)
947 retval = fopen (cp_filename, mode);
950 g_free (cp_filename);
956 return fopen (filename, mode);
962 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
963 * @mode: a string describing the mode in which the file should be
965 * @stream: an existing stream which will be reused, or %NULL
967 * A wrapper for the POSIX freopen() function. The freopen() function
968 * opens a file and associates it with an existing stream.
970 * See the C library manual for more details about freopen().
972 * Returns: A <type>FILE</type> pointer if the file was successfully
973 * opened, or %NULL if an error occurred.
978 g_freopen (const gchar *filename,
983 if (G_WIN32_HAVE_WIDECHAR_API ())
985 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
990 if (wfilename == NULL)
996 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
1005 retval = _wfreopen (wfilename, wmode, stream);
1016 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
1020 if (cp_filename == NULL)
1026 retval = freopen (cp_filename, mode, stream);
1029 g_free (cp_filename);
1035 return freopen (filename, mode, stream);
1039 #define __G_STDIO_C__
1040 #include "galiasdef.c"