Introduce the idea of a filename encoding, which is *literally* the
[glib.git] / glib / gstdio.c
blobcdd393e9f3a7ab6a2402cae796c1d5dc6b07dd48
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.
21 #include "config.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
31 #ifdef G_OS_WIN32
32 #include <wchar.h>
33 #include <io.h>
34 #endif
36 #include "galias.h"
37 #include "glib.h"
38 #include "gstdio.h"
40 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
41 #error Please port this to your operating system
42 #endif
45 /**
46 * g_open:
47 * @filename: a pathname in the GLib file name encoding
48 * @flags: as in open()
49 * @mode: 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
73 * Since: 2.6
75 int
76 g_open (const gchar *filename,
77 int flags,
78 int mode)
80 #ifdef G_OS_WIN32
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;
87 g_free (wfilename);
89 errno = save_errno;
90 return retval;
92 else
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;
98 g_free (cp_filename);
100 errno = save_errno;
101 return retval;
103 #else
104 return open (filename, flags, mode);
105 #endif
109 * g_rename:
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
120 * Since: 2.6
123 g_rename (const gchar *oldfilename,
124 const gchar *newfilename)
126 #ifdef G_OS_WIN32
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);
137 errno = save_errno;
138 return retval;
140 else
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);
150 errno = save_errno;
151 return retval;
153 #else
154 return rename (oldfilename, newfilename);
155 #endif
159 * g_mkdir:
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
169 * occurred
171 * Since: 2.6
174 g_mkdir (const gchar *filename,
175 int mode)
177 #ifdef G_OS_WIN32
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;
184 g_free (wfilename);
186 errno = save_errno;
187 return retval;
189 else
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);
197 errno = save_errno;
198 return retval;
200 #else
201 return mkdir (filename, mode);
202 #endif
206 * g_stat:
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
217 * occurred
219 * Since: 2.6
222 g_stat (const gchar *filename,
223 struct stat *buf)
225 #ifdef G_OS_WIN32
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;
232 g_free (wfilename);
234 errno = save_errno;
235 return retval;
237 else
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);
245 errno = save_errno;
246 return retval;
248 #else
249 return stat (filename, buf);
250 #endif
254 * g_unlink:
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
260 * file is freed.
262 * See the C library manual for more details about unlink().
264 * Returns: 0 if the directory was successfully created, -1 if an error
265 * occurred
267 * Since: 2.6
270 g_unlink (const gchar *filename)
272 #ifdef G_OS_WIN32
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;
279 g_free (wfilename);
281 errno = save_errno;
282 return retval;
284 else
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);
292 errno = save_errno;
293 return retval;
295 #else
296 return unlink (filename);
297 #endif
301 * g_remove:
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
311 * occurred
313 * Since: 2.6
316 g_remove (const gchar *filename)
318 #ifdef G_OS_WIN32
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;
325 g_free (wfilename);
327 errno = save_errno;
328 return retval;
330 else
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);
338 errno = save_errno;
339 return retval;
341 #else
342 return remove (filename);
343 #endif
347 * g_fopen:
348 * @filename: a pathname in the GLib file name encoding
349 * @mode: a string describing the mode in which the file should be
350 * opened
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
360 * Since: 2.6
362 FILE *
363 g_fopen (const gchar *filename,
364 const gchar *mode)
366 #ifdef G_OS_WIN32
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;
374 g_free (wfilename);
375 g_free (wmode);
377 errno = save_errno;
378 return retval;
380 else
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);
388 errno = save_errno;
389 return retval;
391 #else
392 return fopen (filename, mode);
393 #endif
397 * g_freopen:
398 * @filename: a pathname in the GLib file name encoding
399 * @mode: a string describing the mode in which the file should be
400 * opened
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.
411 * Since: 2.6
413 FILE *
414 g_freopen (const gchar *filename,
415 const gchar *mode,
416 FILE *stream)
418 #ifdef G_OS_WIN32
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;
426 g_free (wfilename);
427 g_free (wmode);
429 errno = save_errno;
430 return retval;
432 else
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);
440 errno = save_errno;
441 return retval;
443 #else
444 return freopen (filename, mode, stream);
445 #endif