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.
26 #include <sys/types.h>
43 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
44 #error Please port this to your operating system
50 * @filename: a pathname in the GLib file name encoding
51 * @flags: as in open()
54 * A wrapper for the POSIX open() function. The open() function is
55 * used to convert a pathname into a file descriptor. Note that on
56 * POSIX systems file descriptors are implemented by the operating
57 * system. On Windows, it's the C library that implements open() and
58 * file descriptors. The actual Windows API for opening files is
59 * something different.
61 * See the C library manual for more details about open().
63 * Returns: a new file descriptor, or -1 if an error occurred. The
64 * return value can be used exactly like the return value from open().
69 g_open (const gchar *filename,
74 if (G_WIN32_HAVE_WIDECHAR_API ())
76 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
80 if (wfilename == NULL)
86 retval = _wopen (wfilename, flags, mode);
96 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
100 if (cp_filename == NULL)
106 retval = open (cp_filename, flags, mode);
109 g_free (cp_filename);
115 return open (filename, flags, mode);
121 * @oldfilename: a pathname in the GLib file name encoding
122 * @newfilename: a pathname in the GLib file name encoding
124 * A wrapper for the POSIX rename() function. The rename() function
125 * renames a file, moving it between directories if required.
127 * See your C library manual for more details about how rename() works
128 * on your system. Note in particular that on Windows, it is in
129 * general not possible to rename a file if a file with the new name
130 * already exists. Also it is not possible in general to rename an
133 * Returns: 0 if the renaming succeeded, -1 if an error occurred
138 g_rename (const gchar *oldfilename,
139 const gchar *newfilename)
142 if (G_WIN32_HAVE_WIDECHAR_API ())
144 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
145 wchar_t *wnewfilename;
149 if (woldfilename == NULL)
155 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
157 if (wnewfilename == NULL)
159 g_free (woldfilename);
164 retval = _wrename (woldfilename, wnewfilename);
167 g_free (woldfilename);
168 g_free (wnewfilename);
175 gchar *cp_oldfilename = g_locale_from_utf8 (oldfilename, -1, NULL, NULL, NULL);
176 gchar *cp_newfilename;
180 if (cp_oldfilename == NULL)
186 cp_newfilename = g_locale_from_utf8 (newfilename, -1, NULL, NULL, NULL);
188 if (cp_newfilename == NULL)
190 g_free (cp_oldfilename);
195 retval = rename (cp_oldfilename, cp_newfilename);
198 g_free (cp_oldfilename);
199 g_free (cp_newfilename);
205 return rename (oldfilename, newfilename);
211 * @filename: a pathname in the GLib file name encoding
212 * @mode: permissions to use for the newly created directory
214 * A wrapper for the POSIX mkdir() function. The mkdir() function
215 * attempts to create a directory with the given name and permissions.
217 * See the C library manual for more details about mkdir().
219 * Returns: 0 if the directory was successfully created, -1 if an error
225 g_mkdir (const gchar *filename,
229 if (G_WIN32_HAVE_WIDECHAR_API ())
231 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
235 if (wfilename == NULL)
241 retval = _wmkdir (wfilename);
251 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
255 if (cp_filename == NULL)
261 retval = mkdir (cp_filename);
264 g_free (cp_filename);
270 return mkdir (filename, mode);
276 * @filename: a pathname in the GLib file name encoding
277 * @buf: a pointer to a <structname>stat</structname> struct, which
278 * will be filled with the file information
280 * A wrapper for the POSIX stat() function. The stat() function
281 * returns information about a file.
283 * See the C library manual for more details about stat().
285 * Returns: 0 if the information was successfully retrieved, -1 if an error
291 g_stat (const gchar *filename,
295 if (G_WIN32_HAVE_WIDECHAR_API ())
297 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
301 if (wfilename == NULL)
307 retval = _wstat (wfilename, (struct _stat *) buf);
317 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
321 if (cp_filename == NULL)
327 retval = stat (cp_filename, buf);
330 g_free (cp_filename);
336 return stat (filename, buf);
342 * @filename: a pathname in the GLib file name encoding
343 * @buf: a pointer to a <structname>stat</structname> struct, which
344 * will be filled with the file information
346 * A wrapper for the POSIX lstat() function. The lstat() function is
347 * like stat() except that in the case of symbolic links, it returns
348 * information about the symbolic link itself and not the file that it
349 * refers to. If the system does not support symbolic links g_lstat()
350 * is identical to g_stat().
352 * See the C library manual for more details about lstat().
354 * Returns: 0 if the information was successfully retrieved, -1 if an error
360 g_lstat (const gchar *filename,
364 /* This can't be Win32, so don't do the widechar dance. */
365 return lstat (filename, buf);
367 return g_stat (filename, buf);
373 * @filename: a pathname in the GLib file name encoding
375 * A wrapper for the POSIX unlink() function. The unlink() function
376 * deletes a name from the filesystem. If this was the last link to the
377 * file and no processes have it opened, the diskspace occupied by the
380 * See your C library manual for more details about unlink(). Note
381 * that on Windows, it is in general not possible to delete files that
382 * are open to some process, or mapped into memory.
384 * Returns: 0 if the name was successfully deleted, -1 if an error
390 g_unlink (const gchar *filename)
393 if (G_WIN32_HAVE_WIDECHAR_API ())
395 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
399 if (wfilename == NULL)
405 retval = _wunlink (wfilename);
415 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
419 if (cp_filename == NULL)
425 retval = unlink (cp_filename);
428 g_free (cp_filename);
434 return unlink (filename);
440 * @filename: a pathname in the GLib file name encoding
442 * A wrapper for the POSIX remove() function. The remove() function
443 * deletes a name from the filesystem.
445 * See your C library manual for more details about how remove() works
446 * on your system. On Unix, remove() removes also directories, as it
447 * calls unlink() for files and rmdir() for directories. On Windows,
448 * although remove() in the C library only works for files, this
449 * function tries first remove() and then if that fails rmdir(), and
450 * thus works for both files and directories. Note however, that on
451 * Windows, it is in general not possible to remove a file that is
452 * open to some process, or mapped into memory.
454 * Returns: 0 if the file was successfully removed, -1 if an error
460 g_remove (const gchar *filename)
463 if (G_WIN32_HAVE_WIDECHAR_API ())
465 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
469 if (wfilename == NULL)
475 retval = _wremove (wfilename);
477 retval = _wrmdir (wfilename);
487 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
491 if (cp_filename == NULL)
497 retval = remove (cp_filename);
499 retval = rmdir (cp_filename);
502 g_free (cp_filename);
508 return remove (filename);
514 * @filename: a pathname in the GLib file name encoding
516 * A wrapper for the POSIX rmdir() function. The rmdir() function
517 * deletes a directory from the filesystem.
519 * See your C library manual for more details about how rmdir() works
522 * Returns: 0 if the directory was successfully removed, -1 if an error
528 g_rmdir (const gchar *filename)
531 if (G_WIN32_HAVE_WIDECHAR_API ())
533 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
537 if (wfilename == NULL)
543 retval = _wrmdir (wfilename);
553 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
557 if (cp_filename == NULL)
563 retval = rmdir (cp_filename);
566 g_free (cp_filename);
572 return rmdir (filename);
578 * @filename: a pathname in the GLib file name encoding
579 * @mode: a string describing the mode in which the file should be
582 * A wrapper for the POSIX fopen() function. The fopen() function opens
583 * a file and associates a new stream with it.
585 * See the C library manual for more details about fopen().
587 * Returns: A <typename>FILE</typename> pointer if the file was successfully
588 * opened, or %NULL if an error occurred
593 g_fopen (const gchar *filename,
597 if (G_WIN32_HAVE_WIDECHAR_API ())
599 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
604 if (wfilename == NULL)
610 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
619 retval = _wfopen (wfilename, wmode);
630 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
634 if (cp_filename == NULL)
640 retval = fopen (cp_filename, mode);
643 g_free (cp_filename);
649 return fopen (filename, mode);
655 * @filename: a pathname in the GLib file name encoding
656 * @mode: a string describing the mode in which the file should be
658 * @stream: an existing stream which will be reused, or %NULL
660 * A wrapper for the POSIX freopen() function. The freopen() function
661 * opens a file and associates it with an existing stream.
663 * See the C library manual for more details about freopen().
665 * Returns: A <typename>FILE</typename> pointer if the file was successfully
666 * opened, or %NULL if an error occurred.
671 g_freopen (const gchar *filename,
676 if (G_WIN32_HAVE_WIDECHAR_API ())
678 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
683 if (wfilename == NULL)
689 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
698 retval = _wfreopen (wfilename, wmode, stream);
709 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
713 if (cp_filename == NULL)
719 retval = freopen (cp_filename, mode, stream);
722 g_free (cp_filename);
728 return freopen (filename, mode, stream);