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 #include <sys/types.h>
40 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
41 #error Please port this to your operating system
47 * @filename: a pathname in the GLib file name encoding
48 * @flags: as in open()
51 * A wrapper for the POSIX open() function. The open() function is used
52 * to convert a pathname into a file descriptor.
54 * See the C library manual for more details about open().
56 * The point of these wrappers is to make it possible to handle file
57 * names with any Unicode characters in them on Windows without having
58 * to use ifdefs and the wide character API in the application code.
60 * The pathname argument should be in the GLib file name encoding. On
61 * POSIX this is the actual on-disk encoding which might correspond to
62 * the locale settings of the process (or the
63 * <envar>G_FILENAME_ENCODING</envar> environment variable), or not.
65 * On Windows the GLib file name encoding is UTF-8. Note that the
66 * Microsoft C library does not use UTF-8, but has separate APIs for
67 * current system code page and wide characters (UTF-16). The GLib
68 * wrappers call the wide character API if present (on modern Windows
69 * systems), otherwise convert to/from the system code page.
71 * Returns: a new file descriptor, or -1 if an error occurred
76 g_open (const gchar
*filename
,
81 if (G_WIN32_HAVE_WIDECHAR_API ())
83 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
84 int retval
= _wopen (wfilename
, flags
, mode
);
85 int save_errno
= errno
;
94 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
95 int retval
= open (cp_filename
, flags
, mode
);
96 int save_errno
= errno
;
104 return open (filename
, flags
, mode
);
110 * @oldfilename: a pathname in the GLib file name encoding
111 * @newfilename: a pathname in the GLib file name encoding
113 * A wrapper for the POSIX rename() function. The rename() function
114 * renames a file, moving it between directories if required.
116 * See the C library manual for more details about rename().
118 * Returns: 0 if the renaming succeeded, -1 if an error occurred
123 g_rename (const gchar
*oldfilename
,
124 const gchar
*newfilename
)
127 if (G_WIN32_HAVE_WIDECHAR_API ())
129 wchar_t *woldfilename
= g_utf8_to_utf16 (oldfilename
, -1, NULL
, NULL
, NULL
);
130 wchar_t *wnewfilename
= g_utf8_to_utf16 (newfilename
, -1, NULL
, NULL
, NULL
);
131 int retval
= _wrename (woldfilename
, wnewfilename
);
132 int save_errno
= errno
;
134 g_free (woldfilename
);
135 g_free (wnewfilename
);
142 gchar
*cp_oldfilename
= g_locale_from_utf8 (oldfilename
, -1, NULL
, NULL
, NULL
);
143 gchar
*cp_newfilename
= g_locale_from_utf8 (newfilename
, -1, NULL
, NULL
, NULL
);
144 int retval
= rename (cp_oldfilename
, cp_newfilename
);
145 int save_errno
= errno
;
147 g_free (cp_oldfilename
);
148 g_free (cp_newfilename
);
154 return rename (oldfilename
, newfilename
);
160 * @filename: a pathname in the GLib file name encoding
161 * @mode: permissions to use for the newly created directory
163 * A wrapper for the POSIX mkdir() function. The mkdir() function
164 * attempts to create a directory with the given name and permissions.
166 * See the C library manual for more details about mkdir().
168 * Returns: 0 if the directory was successfully created, -1 if an error
174 g_mkdir (const gchar
*filename
,
178 if (G_WIN32_HAVE_WIDECHAR_API ())
180 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
181 int retval
= _wmkdir (wfilename
);
182 int save_errno
= errno
;
191 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
192 int retval
= mkdir (cp_filename
);
193 int save_errno
= errno
;
195 g_free (cp_filename
);
201 return mkdir (filename
, mode
);
207 * @filename: a pathname in the GLib file name encoding
208 * @buf: a pointer to a <structname>stat</structname> struct, which
209 * will be filled with the file information
211 * A wrapper for the POSIX stat() function. The stat() function
212 * returns information about a file.
214 * See the C library manual for more details about stat().
216 * Returns: 0 if the directory was successfully created, -1 if an error
222 g_stat (const gchar
*filename
,
226 if (G_WIN32_HAVE_WIDECHAR_API ())
228 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
229 int retval
= _wstat (wfilename
, (struct _stat
*) buf
);
230 int save_errno
= errno
;
239 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
240 int retval
= stat (cp_filename
, buf
);
241 int save_errno
= errno
;
243 g_free (cp_filename
);
249 return stat (filename
, buf
);
255 * @filename: a pathname in the GLib file name encoding
257 * A wrapper for the POSIX unlink() function. The unlink() function
258 * deletes a name from the filesystem. If this was the last link to the
259 * file and no processes have it opened, the diskspace occupied by the
262 * See the C library manual for more details about unlink().
264 * Returns: 0 if the directory was successfully created, -1 if an error
270 g_unlink (const gchar
*filename
)
273 if (G_WIN32_HAVE_WIDECHAR_API ())
275 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
276 int retval
= _wunlink (wfilename
);
277 int save_errno
= errno
;
286 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
287 int retval
= unlink (cp_filename
);
288 int save_errno
= errno
;
290 g_free (cp_filename
);
296 return unlink (filename
);
302 * @filename: a pathname in the GLib file name encoding
304 * A wrapper for the POSIX remove() function. The remove() function
305 * deletes a name from the filesystem. It calls unlink() for files
306 * and rmdir() for directories.
308 * See the C library manual for more details about remove().
310 * Returns: 0 if the directory was successfully created, -1 if an error
316 g_remove (const gchar
*filename
)
319 if (G_WIN32_HAVE_WIDECHAR_API ())
321 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
322 int retval
= _wremove (wfilename
);
323 int save_errno
= errno
;
332 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
333 int retval
= remove (cp_filename
);
334 int save_errno
= errno
;
336 g_free (cp_filename
);
342 return remove (filename
);
348 * @filename: a pathname in the GLib file name encoding
349 * @mode: a string describing the mode in which the file should be
352 * A wrapper for the POSIX fopen() function. The fopen() function opens
353 * a file and associates a new stream with it.
355 * See the C library manual for more details about fopen().
357 * Returns: A <typename>FILE</typename> pointer if the file was successfully
358 * opened, or %NULL if an error occurred
363 g_fopen (const gchar
*filename
,
367 if (G_WIN32_HAVE_WIDECHAR_API ())
369 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
370 wchar_t *wmode
= g_utf8_to_utf16 (mode
, -1, NULL
, NULL
, NULL
);
371 FILE *retval
= _wfopen (wfilename
, wmode
);
372 int save_errno
= errno
;
382 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
383 FILE *retval
= fopen (cp_filename
, mode
);
384 int save_errno
= errno
;
386 g_free (cp_filename
);
392 return fopen (filename
, mode
);
398 * @filename: a pathname in the GLib file name encoding
399 * @mode: a string describing the mode in which the file should be
401 * @stream: an existing stream which will be reused, or %NULL
403 * A wrapper for the POSIX freopen() function. The freopen() function
404 * opens a file and associates it with an existing stream.
406 * See the C library manual for more details about freopen().
408 * Returns: A <typename>FILE</typename> pointer if the file was successfully
409 * opened, or %NULL if an error occurred.
414 g_freopen (const gchar
*filename
,
419 if (G_WIN32_HAVE_WIDECHAR_API ())
421 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
422 wchar_t *wmode
= g_utf8_to_utf16 (mode
, -1, NULL
, NULL
, NULL
);
423 FILE *retval
= _wfreopen (wfilename
, wmode
, stream
);
424 int save_errno
= errno
;
434 gchar
*cp_filename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
435 FILE *retval
= freopen (cp_filename
, mode
, stream
);
436 int save_errno
= errno
;
438 g_free (cp_filename
);
444 return freopen (filename
, mode
, stream
);