d0c194165bc7fe9368b8f6dc4efbf17cd88e740f
[platform/upstream/glib.git] / glib / gstdio.c
1 /* gstdio.c - wrappers for C library functions
2  *
3  * Copyright 2004 Tor Lillqvist
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "galias.h"
23
24 #include "glib.h"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #ifdef G_OS_WIN32
35 #include <errno.h>
36 #include <wchar.h>
37 #include <direct.h>
38 #include <io.h>
39 #endif
40
41 #include "gstdio.h"
42
43 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
44 #error Please port this to your operating system
45 #endif
46
47
48 /**
49  * g_open:
50  * @filename: a pathname in the GLib file name encoding
51  * @flags: as in open()
52  * @mode: as in open()
53  *
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.
60  *
61  * See the C library manual for more details about open().
62  *
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().
65  * 
66  * Since: 2.6
67  */
68 int
69 g_open (const gchar *filename,
70         int          flags,
71         int          mode)
72 {
73 #ifdef G_OS_WIN32
74   if (G_WIN32_HAVE_WIDECHAR_API ())
75     {
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;
79       
80       g_free (wfilename);
81
82       errno = save_errno;
83       return retval;
84     }
85   else
86     {    
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;
90
91       g_free (cp_filename);
92
93       errno = save_errno;
94       return retval;
95     }
96 #else
97   return open (filename, flags, mode);
98 #endif
99 }
100
101 /**
102  * g_rename:
103  * @oldfilename: a pathname in the GLib file name encoding
104  * @newfilename: a pathname in the GLib file name encoding
105  *
106  * A wrapper for the POSIX rename() function. The rename() function 
107  * renames a file, moving it between directories if required.
108  * 
109  * See the C library manual for more details about rename().
110  *
111  * Returns: 0 if the renaming succeeded, -1 if an error occurred
112  * 
113  * Since: 2.6
114  */
115 int
116 g_rename (const gchar *oldfilename,
117           const gchar *newfilename)
118 {
119 #ifdef G_OS_WIN32
120   if (G_WIN32_HAVE_WIDECHAR_API ())
121     {
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;
126       
127       g_free (woldfilename);
128       g_free (wnewfilename);
129       
130       errno = save_errno;
131       return retval;
132     }
133   else
134     {
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;
139
140       g_free (cp_oldfilename);
141       g_free (cp_newfilename);
142
143       errno = save_errno;
144       return retval;
145     }
146 #else
147   return rename (oldfilename, newfilename);
148 #endif
149 }
150
151 /**
152  * g_mkdir: 
153  * @filename: a pathname in the GLib file name encoding
154  * @mode: permissions to use for the newly created directory
155  *
156  * A wrapper for the POSIX mkdir() function. The mkdir() function 
157  * attempts to create a directory with the given name and permissions.
158  * 
159  * See the C library manual for more details about mkdir().
160  *
161  * Returns: 0 if the directory was successfully created, -1 if an error 
162  *    occurred
163  * 
164  * Since: 2.6
165  */
166 int
167 g_mkdir (const gchar *filename,
168          int          mode)
169 {
170 #ifdef G_OS_WIN32
171   if (G_WIN32_HAVE_WIDECHAR_API ())
172     {
173       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
174       int retval = _wmkdir (wfilename);
175       int save_errno = errno;
176
177       g_free (wfilename);
178       
179       errno = save_errno;
180       return retval;
181     }
182   else
183     {
184       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
185       int retval = mkdir (cp_filename);
186       int save_errno = errno;
187
188       g_free (cp_filename);
189
190       errno = save_errno;
191       return retval;
192     }
193 #else
194   return mkdir (filename, mode);
195 #endif
196 }
197
198 /**
199  * g_stat: 
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
203  *
204  * A wrapper for the POSIX stat() function. The stat() function 
205  * returns information about a file.
206  * 
207  * See the C library manual for more details about stat().
208  *
209  * Returns: 0 if the information was successfully retrieved, -1 if an error 
210  *    occurred
211  * 
212  * Since: 2.6
213  */
214 int
215 g_stat (const gchar *filename,
216         struct stat *buf)
217 {
218 #ifdef G_OS_WIN32
219   if (G_WIN32_HAVE_WIDECHAR_API ())
220     {
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;
224
225       g_free (wfilename);
226
227       errno = save_errno;
228       return retval;
229     }
230   else
231     {
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;
235
236       g_free (cp_filename);
237
238       errno = save_errno;
239       return retval;
240     }
241 #else
242   return stat (filename, buf);
243 #endif
244 }
245
246 /**
247  * g_lstat: 
248  * @filename: a pathname in the GLib file name encoding
249  * @buf: a pointer to a <structname>stat</structname> struct, which
250  *    will be filled with the file information
251  *
252  * A wrapper for the POSIX lstat() function. The lstat() function is
253  * like stat() except that in the case of symbolic links, it returns
254  * information about the symbolic link itself and not the file that it
255  * refers to. On Windows where there are no symbolic links g_lstat()
256  * is identical to g_stat().
257  * 
258  * See the C library manual for more details about lstat().
259  *
260  * Returns: 0 if the information was successfully retrieved, -1 if an error 
261  *    occurred
262  * 
263  * Since: 2.6
264  */
265 int
266 g_lstat (const gchar *filename,
267          struct stat *buf)
268 {
269 #ifdef G_OS_WIN32
270   return g_stat (filename, buf);
271 #else
272   return lstat (filename, buf);
273 #endif
274 }
275
276 /**
277  * g_unlink:
278  * @filename: a pathname in the GLib file name encoding
279  *
280  * A wrapper for the POSIX unlink() function. The unlink() function 
281  * deletes a name from the filesystem. If this was the last link to the 
282  * file and no processes have it opened, the diskspace occupied by the
283  * file is freed.
284  * 
285  * See the C library manual for more details about unlink().
286  *
287  * Returns: 0 if the directory was successfully created, -1 if an error 
288  *    occurred
289  * 
290  * Since: 2.6
291  */
292 int
293 g_unlink (const gchar *filename)
294 {
295 #ifdef G_OS_WIN32
296   if (G_WIN32_HAVE_WIDECHAR_API ())
297     {
298       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
299       int retval = _wunlink (wfilename);
300       int save_errno = errno;
301
302       g_free (wfilename);
303
304       errno = save_errno;
305       return retval;
306     }
307   else
308     {
309       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
310       int retval = unlink (cp_filename);
311       int save_errno = errno;
312
313       g_free (cp_filename);
314
315       errno = save_errno;
316       return retval;
317     }
318 #else
319   return unlink (filename);
320 #endif
321 }
322
323 /**
324  * g_remove:
325  * @filename: a pathname in the GLib file name encoding
326  *
327  * A wrapper for the POSIX remove() function. The remove() function 
328  * deletes a name from the filesystem. It calls unlink() for files
329  * and rmdir() for directories.
330  * 
331  * See the C library manual for more details about remove().
332  *
333  * Returns: 0 if the directory was successfully created, -1 if an error 
334  *    occurred
335  * 
336  * Since: 2.6
337  */
338 int
339 g_remove (const gchar *filename)
340 {
341 #ifdef G_OS_WIN32
342   if (G_WIN32_HAVE_WIDECHAR_API ())
343     {
344       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
345       int retval = _wremove (wfilename);
346       int save_errno = errno;
347
348       g_free (wfilename);
349
350       errno = save_errno;
351       return retval;
352     }
353   else
354     {
355       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
356       int retval = remove (cp_filename);
357       int save_errno = errno;
358
359       g_free (cp_filename);
360
361       errno = save_errno;
362       return retval;
363     }
364 #else
365   return remove (filename);
366 #endif
367 }
368
369 /**
370  * g_fopen:
371  * @filename: a pathname in the GLib file name encoding
372  * @mode: a string describing the mode in which the file should be 
373  *   opened
374  *
375  * A wrapper for the POSIX fopen() function. The fopen() function opens
376  * a file and associates a new stream with it. 
377  * 
378  * See the C library manual for more details about fopen().
379  *
380  * Returns: A <typename>FILE</typename> pointer if the file was successfully
381  *    opened, or %NULL if an error occurred
382  * 
383  * Since: 2.6
384  */
385 FILE *
386 g_fopen (const gchar *filename,
387          const gchar *mode)
388 {
389 #ifdef G_OS_WIN32
390   if (G_WIN32_HAVE_WIDECHAR_API ())
391     {
392       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
393       wchar_t *wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
394       FILE *retval = _wfopen (wfilename, wmode);
395       int save_errno = errno;
396
397       g_free (wfilename);
398       g_free (wmode);
399
400       errno = save_errno;
401       return retval;
402     }
403   else
404     {
405       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
406       FILE *retval = fopen (cp_filename, mode);
407       int save_errno = errno;
408
409       g_free (cp_filename);
410
411       errno = save_errno;
412       return retval;
413     }
414 #else
415   return fopen (filename, mode);
416 #endif
417 }
418
419 /**
420  * g_freopen:
421  * @filename: a pathname in the GLib file name encoding
422  * @mode: a string describing the mode in which the file should be 
423  *   opened
424  * @stream: an existing stream which will be reused, or %NULL
425  *
426  * A wrapper for the POSIX freopen() function. The freopen() function
427  * opens a file and associates it with an existing stream.
428  * 
429  * See the C library manual for more details about freopen().
430  *
431  * Returns: A <typename>FILE</typename> pointer if the file was successfully
432  *    opened, or %NULL if an error occurred.
433  * 
434  * Since: 2.6
435  */
436 FILE *
437 g_freopen (const gchar *filename,
438            const gchar *mode,
439            FILE        *stream)
440 {
441 #ifdef G_OS_WIN32
442   if (G_WIN32_HAVE_WIDECHAR_API ())
443     {
444       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
445       wchar_t *wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
446       FILE *retval = _wfreopen (wfilename, wmode, stream);
447       int save_errno = errno;
448
449       g_free (wfilename);
450       g_free (wmode);
451
452       errno = save_errno;
453       return retval;
454     }
455   else
456     {
457       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
458       FILE *retval = freopen (cp_filename, mode, stream);
459       int save_errno = errno;
460
461       g_free (cp_filename);
462
463       errno = save_errno;
464       return retval;
465     }
466 #else
467   return freopen (filename, mode, stream);
468 #endif
469 }