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)
47 #error Please port this to your operating system
53 * @filename: a pathname in the GLib file name encoding
54 * @flags: as in open()
57 * A wrapper for the POSIX open() function. The open() function is
58 * used to convert a pathname into a file descriptor. Note that on
59 * POSIX systems file descriptors are implemented by the operating
60 * system. On Windows, it's the C library that implements open() and
61 * file descriptors. The actual Windows API for opening files is
62 * something different.
64 * See the C library manual for more details about open().
66 * Returns: a new file descriptor, or -1 if an error occurred. The
67 * return value can be used exactly like the return value from open().
72 g_open (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
= _wopen (wfilename
, flags
, mode
);
99 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
103 if (cp_filename
== NULL
)
109 retval
= open (cp_filename
, flags
, mode
);
112 g_free (cp_filename
);
118 return open (filename
, flags
, mode
);
124 * @oldfilename: a pathname in the GLib file name encoding
125 * @newfilename: a pathname in the GLib file name encoding
127 * A wrapper for the POSIX rename() function. The rename() function
128 * renames a file, moving it between directories if required.
130 * See your C library manual for more details about how rename() works
131 * on your system. Note in particular that on Windows, it is in
132 * general not possible to rename a file if a file with the new name
133 * already exists. Also it is not possible in general to rename an
136 * Returns: 0 if the renaming succeeded, -1 if an error occurred
141 g_rename (const gchar
*oldfilename
,
142 const gchar
*newfilename
)
145 if (G_WIN32_HAVE_WIDECHAR_API ())
147 wchar_t *woldfilename
= g_utf8_to_utf16 (oldfilename
, -1, NULL
, NULL
, NULL
);
148 wchar_t *wnewfilename
;
152 if (woldfilename
== NULL
)
158 wnewfilename
= g_utf8_to_utf16 (newfilename
, -1, NULL
, NULL
, NULL
);
160 if (wnewfilename
== NULL
)
162 g_free (woldfilename
);
167 retval
= _wrename (woldfilename
, wnewfilename
);
170 g_free (woldfilename
);
171 g_free (wnewfilename
);
178 gchar
*cp_oldfilename
= g_locale_from_utf8 (oldfilename
, -1, NULL
, NULL
, NULL
);
179 gchar
*cp_newfilename
;
183 if (cp_oldfilename
== NULL
)
189 cp_newfilename
= g_locale_from_utf8 (newfilename
, -1, NULL
, NULL
, NULL
);
191 if (cp_newfilename
== NULL
)
193 g_free (cp_oldfilename
);
198 retval
= rename (cp_oldfilename
, cp_newfilename
);
201 g_free (cp_oldfilename
);
202 g_free (cp_newfilename
);
208 return rename (oldfilename
, newfilename
);
214 * @filename: a pathname in the GLib file name encoding
215 * @mode: permissions to use for the newly created directory
217 * A wrapper for the POSIX mkdir() function. The mkdir() function
218 * attempts to create a directory with the given name and permissions.
220 * See the C library manual for more details about mkdir().
222 * Returns: 0 if the directory was successfully created, -1 if an error
228 g_mkdir (const gchar
*filename
,
232 if (G_WIN32_HAVE_WIDECHAR_API ())
234 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
238 if (wfilename
== NULL
)
244 retval
= _wmkdir (wfilename
);
254 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
258 if (cp_filename
== NULL
)
264 retval
= mkdir (cp_filename
);
267 g_free (cp_filename
);
273 return mkdir (filename
, mode
);
279 * @filename: a pathname in the GLib file name encoding
280 * @buf: a pointer to a <structname>stat</structname> struct, which
281 * will be filled with the file information
283 * A wrapper for the POSIX stat() function. The stat() function
284 * returns information about a file.
286 * See the C library manual for more details about stat().
288 * Returns: 0 if the information was successfully retrieved, -1 if an error
294 g_stat (const gchar
*filename
,
298 if (G_WIN32_HAVE_WIDECHAR_API ())
300 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
304 if (wfilename
== NULL
)
310 retval
= _wstat (wfilename
, (struct _stat
*) buf
);
320 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
324 if (cp_filename
== NULL
)
330 retval
= stat (cp_filename
, buf
);
333 g_free (cp_filename
);
339 return stat (filename
, buf
);
345 * @filename: a pathname in the GLib file name encoding
346 * @buf: a pointer to a <structname>stat</structname> struct, which
347 * will be filled with the file information
349 * A wrapper for the POSIX lstat() function. The lstat() function is
350 * like stat() except that in the case of symbolic links, it returns
351 * information about the symbolic link itself and not the file that it
352 * refers to. If the system does not support symbolic links g_lstat()
353 * is identical to g_stat().
355 * See the C library manual for more details about lstat().
357 * Returns: 0 if the information was successfully retrieved, -1 if an error
363 g_lstat (const gchar
*filename
,
367 /* This can't be Win32, so don't do the widechar dance. */
368 return lstat (filename
, buf
);
370 return g_stat (filename
, buf
);
376 * @filename: a pathname in the GLib file name encoding
378 * A wrapper for the POSIX unlink() function. The unlink() function
379 * deletes a name from the filesystem. If this was the last link to the
380 * file and no processes have it opened, the diskspace occupied by the
383 * See your C library manual for more details about unlink(). Note
384 * that on Windows, it is in general not possible to delete files that
385 * are open to some process, or mapped into memory.
387 * Returns: 0 if the name was successfully deleted, -1 if an error
393 g_unlink (const gchar
*filename
)
396 if (G_WIN32_HAVE_WIDECHAR_API ())
398 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
402 if (wfilename
== NULL
)
408 retval
= _wunlink (wfilename
);
418 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
422 if (cp_filename
== NULL
)
428 retval
= unlink (cp_filename
);
431 g_free (cp_filename
);
437 return unlink (filename
);
443 * @filename: a pathname in the GLib file name encoding
445 * A wrapper for the POSIX remove() function. The remove() function
446 * deletes a name from the filesystem.
448 * See your C library manual for more details about how remove() works
449 * on your system. On Unix, remove() removes also directories, as it
450 * calls unlink() for files and rmdir() for directories. On Windows,
451 * although remove() in the C library only works for files, this
452 * function tries first remove() and then if that fails rmdir(), and
453 * thus works for both files and directories. Note however, that on
454 * Windows, it is in general not possible to remove a file that is
455 * open to some process, or mapped into memory.
457 * Returns: 0 if the file was successfully removed, -1 if an error
463 g_remove (const gchar
*filename
)
466 if (G_WIN32_HAVE_WIDECHAR_API ())
468 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
472 if (wfilename
== NULL
)
478 retval
= _wremove (wfilename
);
480 retval
= _wrmdir (wfilename
);
490 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
494 if (cp_filename
== NULL
)
500 retval
= remove (cp_filename
);
502 retval
= rmdir (cp_filename
);
505 g_free (cp_filename
);
511 return remove (filename
);
517 * @filename: a pathname in the GLib file name encoding
519 * A wrapper for the POSIX rmdir() function. The rmdir() function
520 * deletes a directory from the filesystem.
522 * See your C library manual for more details about how rmdir() works
525 * Returns: 0 if the directory was successfully removed, -1 if an error
531 g_rmdir (const gchar
*filename
)
534 if (G_WIN32_HAVE_WIDECHAR_API ())
536 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
540 if (wfilename
== NULL
)
546 retval
= _wrmdir (wfilename
);
556 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
560 if (cp_filename
== NULL
)
566 retval
= rmdir (cp_filename
);
569 g_free (cp_filename
);
575 return rmdir (filename
);
581 * @filename: a pathname in the GLib file name encoding
582 * @mode: a string describing the mode in which the file should be
585 * A wrapper for the POSIX fopen() function. The fopen() function opens
586 * a file and associates a new stream with it.
588 * See the C library manual for more details about fopen().
590 * Returns: A <typename>FILE</typename> pointer if the file was successfully
591 * opened, or %NULL if an error occurred
596 g_fopen (const gchar
*filename
,
600 if (G_WIN32_HAVE_WIDECHAR_API ())
602 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
607 if (wfilename
== NULL
)
613 wmode
= g_utf8_to_utf16 (mode
, -1, NULL
, NULL
, NULL
);
622 retval
= _wfopen (wfilename
, wmode
);
633 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
637 if (cp_filename
== NULL
)
643 retval
= fopen (cp_filename
, mode
);
646 g_free (cp_filename
);
652 return fopen (filename
, mode
);
658 * @filename: a pathname in the GLib file name encoding
659 * @mode: a string describing the mode in which the file should be
661 * @stream: an existing stream which will be reused, or %NULL
663 * A wrapper for the POSIX freopen() function. The freopen() function
664 * opens a file and associates it with an existing stream.
666 * See the C library manual for more details about freopen().
668 * Returns: A <typename>FILE</typename> pointer if the file was successfully
669 * opened, or %NULL if an error occurred.
674 g_freopen (const gchar
*filename
,
679 if (G_WIN32_HAVE_WIDECHAR_API ())
681 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
686 if (wfilename
== NULL
)
692 wmode
= g_utf8_to_utf16 (mode
, -1, NULL
, NULL
, NULL
);
701 retval
= _wfreopen (wfilename
, wmode
, stream
);
712 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
716 if (cp_filename
== NULL
)
722 retval
= freopen (cp_filename
, mode
, stream
);
725 g_free (cp_filename
);
731 return freopen (filename
, mode
, stream
);
735 #define __G_STDIO_C__
736 #include "galiasdef.c"