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);
77 int retval = _wopen (wfilename, flags, mode);
78 int save_errno = errno;
87 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
88 int retval = open (cp_filename, flags, mode);
89 int save_errno = errno;
97 return open (filename, flags, mode);
103 * @oldfilename: a pathname in the GLib file name encoding
104 * @newfilename: a pathname in the GLib file name encoding
106 * A wrapper for the POSIX rename() function. The rename() function
107 * renames a file, moving it between directories if required.
109 * See the C library manual for more details about rename().
111 * Returns: 0 if the renaming succeeded, -1 if an error occurred
116 g_rename (const gchar *oldfilename,
117 const gchar *newfilename)
120 if (G_WIN32_HAVE_WIDECHAR_API ())
122 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
123 wchar_t *wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
124 int retval = _wrename (woldfilename, wnewfilename);
125 int save_errno = errno;
127 g_free (woldfilename);
128 g_free (wnewfilename);
135 gchar *cp_oldfilename = g_locale_from_utf8 (oldfilename, -1, NULL, NULL, NULL);
136 gchar *cp_newfilename = g_locale_from_utf8 (newfilename, -1, NULL, NULL, NULL);
137 int retval = rename (cp_oldfilename, cp_newfilename);
138 int save_errno = errno;
140 g_free (cp_oldfilename);
141 g_free (cp_newfilename);
147 return rename (oldfilename, newfilename);
153 * @filename: a pathname in the GLib file name encoding
154 * @mode: permissions to use for the newly created directory
156 * A wrapper for the POSIX mkdir() function. The mkdir() function
157 * attempts to create a directory with the given name and permissions.
159 * See the C library manual for more details about mkdir().
161 * Returns: 0 if the directory was successfully created, -1 if an error
167 g_mkdir (const gchar *filename,
171 if (G_WIN32_HAVE_WIDECHAR_API ())
173 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
174 int retval = _wmkdir (wfilename);
175 int save_errno = errno;
184 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
185 int retval = mkdir (cp_filename);
186 int save_errno = errno;
188 g_free (cp_filename);
194 return mkdir (filename, mode);
200 * @filename: a pathname in the GLib file name encoding
201 * @buf: a pointer to a <structname>stat</structname> struct, which
202 * will be filled with the file information
204 * A wrapper for the POSIX stat() function. The stat() function
205 * returns information about a file.
207 * See the C library manual for more details about stat().
209 * Returns: 0 if the directory was successfully created, -1 if an error
215 g_stat (const gchar *filename,
219 if (G_WIN32_HAVE_WIDECHAR_API ())
221 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
222 int retval = _wstat (wfilename, (struct _stat *) buf);
223 int save_errno = errno;
232 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
233 int retval = stat (cp_filename, buf);
234 int save_errno = errno;
236 g_free (cp_filename);
242 return stat (filename, buf);
248 * @filename: a pathname in the GLib file name encoding
250 * A wrapper for the POSIX unlink() function. The unlink() function
251 * deletes a name from the filesystem. If this was the last link to the
252 * file and no processes have it opened, the diskspace occupied by the
255 * See the C library manual for more details about unlink().
257 * Returns: 0 if the directory was successfully created, -1 if an error
263 g_unlink (const gchar *filename)
266 if (G_WIN32_HAVE_WIDECHAR_API ())
268 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
269 int retval = _wunlink (wfilename);
270 int save_errno = errno;
279 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
280 int retval = unlink (cp_filename);
281 int save_errno = errno;
283 g_free (cp_filename);
289 return unlink (filename);
295 * @filename: a pathname in the GLib file name encoding
297 * A wrapper for the POSIX remove() function. The remove() function
298 * deletes a name from the filesystem. It calls unlink() for files
299 * and rmdir() for directories.
301 * See the C library manual for more details about remove().
303 * Returns: 0 if the directory was successfully created, -1 if an error
309 g_remove (const gchar *filename)
312 if (G_WIN32_HAVE_WIDECHAR_API ())
314 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
315 int retval = _wremove (wfilename);
316 int save_errno = errno;
325 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
326 int retval = remove (cp_filename);
327 int save_errno = errno;
329 g_free (cp_filename);
335 return remove (filename);
341 * @filename: a pathname in the GLib file name encoding
342 * @mode: a string describing the mode in which the file should be
345 * A wrapper for the POSIX fopen() function. The fopen() function opens
346 * a file and associates a new stream with it.
348 * See the C library manual for more details about fopen().
350 * Returns: A <typename>FILE</typename> pointer if the file was successfully
351 * opened, or %NULL if an error occurred
356 g_fopen (const gchar *filename,
360 if (G_WIN32_HAVE_WIDECHAR_API ())
362 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
363 wchar_t *wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
364 FILE *retval = _wfopen (wfilename, wmode);
365 int save_errno = errno;
375 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
376 FILE *retval = fopen (cp_filename, mode);
377 int save_errno = errno;
379 g_free (cp_filename);
385 return fopen (filename, mode);
391 * @filename: a pathname in the GLib file name encoding
392 * @mode: a string describing the mode in which the file should be
394 * @stream: an existing stream which will be reused, or %NULL
396 * A wrapper for the POSIX freopen() function. The freopen() function
397 * opens a file and associates it with an existing stream.
399 * See the C library manual for more details about freopen().
401 * Returns: A <typename>FILE</typename> pointer if the file was successfully
402 * opened, or %NULL if an error occurred.
407 g_freopen (const gchar *filename,
412 if (G_WIN32_HAVE_WIDECHAR_API ())
414 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
415 wchar_t *wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
416 FILE *retval = _wfreopen (wfilename, wmode, stream);
417 int save_errno = errno;
427 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
428 FILE *retval = freopen (cp_filename, mode, stream);
429 int save_errno = errno;
431 g_free (cp_filename);
437 return freopen (filename, mode, stream);