more atomic ops pointer cast fixes. this time it'll work with atomic op
[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
23 #define G_STDIO_NO_WRAP_ON_UNIX
24
25 #include "glib.h"
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #ifdef G_OS_WIN32
36 #include <windows.h>
37 #include <errno.h>
38 #include <wchar.h>
39 #include <direct.h>
40 #include <io.h>
41 #endif
42
43 #include "gstdio.h"
44
45 #include "galias.h"
46
47 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
48 #error Please port this to your operating system
49 #endif
50
51
52 /**
53  * g_access:
54  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
55  * @mode: as in access()
56  *
57  * A wrapper for the POSIX access() function. This function is used to
58  * test a pathname for one or several of read, write or execute
59  * permissions, or just existence. On Windows, the underlying access()
60  * function in the C library only checks the READONLY attribute, and
61  * does not look at the ACL at all. Software that needs to handle file
62  * permissions on Windows more exactly should use the Win32 API.
63  *
64  * See the C library manual for more details about access().
65  *
66  * Returns: zero if the pathname refers to an existing file system
67  * object that has all the tested permissions, or -1 otherwise or on
68  * error.
69  * 
70  * Since: 2.8
71  */
72 int
73 g_access (const gchar *filename,
74           int          mode)
75 {
76 #ifdef G_OS_WIN32
77   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
78   int retval;
79   int save_errno;
80     
81   if (wfilename == NULL)
82     {
83       errno = EINVAL;
84       return -1;
85     }
86
87   retval = _waccess (wfilename, mode);
88   save_errno = errno;
89
90   g_free (wfilename);
91
92   errno = save_errno;
93   return retval;
94 #else
95   return access (filename, mode);
96 #endif
97 }
98
99 /**
100  * g_chmod:
101  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
102  * @mode: as in chmod()
103  *
104  * A wrapper for the POSIX chmod() function. The chmod() function is
105  * used to set the permissions of a file system object. Note that on
106  * Windows the file protection mechanism is not at all POSIX-like, and
107  * the underlying chmod() function in the C library just sets or
108  * clears the READONLY attribute. It does not touch any ACL. Software
109  * that needs to manage file permissions on Windows exactly should
110  * use the Win32 API.
111  *
112  * See the C library manual for more details about chmod().
113  *
114  * Returns: zero if the operation succeeded, -1 on error.
115  * 
116  * Since: 2.8
117  */
118 int
119 g_chmod (const gchar *filename,
120          int          mode)
121 {
122 #ifdef G_OS_WIN32
123   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
124   int retval;
125   int save_errno;
126     
127   if (wfilename == NULL)
128     {
129       errno = EINVAL;
130       return -1;
131     }
132
133   retval = _wchmod (wfilename, mode);
134   save_errno = errno;
135
136   g_free (wfilename);
137
138   errno = save_errno;
139   return retval;
140 #else
141   return chmod (filename, mode);
142 #endif
143 }
144
145 /**
146  * g_open:
147  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
148  * @flags: as in open()
149  * @mode: as in open()
150  *
151  * A wrapper for the POSIX open() function. The open() function is
152  * used to convert a pathname into a file descriptor. Note that on
153  * POSIX systems file descriptors are implemented by the operating
154  * system. On Windows, it's the C library that implements open() and
155  * file descriptors. The actual Windows API for opening files is
156  * something different.
157  *
158  * See the C library manual for more details about open().
159  *
160  * Returns: a new file descriptor, or -1 if an error occurred. The
161  * return value can be used exactly like the return value from open().
162  * 
163  * Since: 2.6
164  */
165 int
166 g_open (const gchar *filename,
167         int          flags,
168         int          mode)
169 {
170 #ifdef G_OS_WIN32
171   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
172   int retval;
173   int save_errno;
174     
175   if (wfilename == NULL)
176     {
177       errno = EINVAL;
178       return -1;
179     }
180
181   retval = _wopen (wfilename, flags, mode);
182   save_errno = errno;
183
184   g_free (wfilename);
185
186   errno = save_errno;
187   return retval;
188 #else
189   return open (filename, flags, mode);
190 #endif
191 }
192
193 /**
194  * g_creat:
195  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
196  * @mode: as in creat()
197  *
198  * A wrapper for the POSIX creat() function. The creat() function is
199  * used to convert a pathname into a file descriptor, creating a file
200  * if necessar. Note that on POSIX systems file descriptors are
201  * implemented by the operating system. On Windows, it's the C library
202  * that implements creat() and file descriptors. The actual Windows
203  * API for opening files is something different.
204  *
205  * See the C library manual for more details about creat().
206  *
207  * Returns: a new file descriptor, or -1 if an error occurred. The
208  * return value can be used exactly like the return value from creat().
209  * 
210  * Since: 2.8
211  */
212 int
213 g_creat (const gchar *filename,
214          int          mode)
215 {
216 #ifdef G_OS_WIN32
217   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
218   int retval;
219   int save_errno;
220     
221   if (wfilename == NULL)
222     {
223       errno = EINVAL;
224       return -1;
225     }
226
227   retval = _wcreat (wfilename, mode);
228   save_errno = errno;
229
230   g_free (wfilename);
231
232   errno = save_errno;
233   return retval;
234 #else
235   return creat (filename, mode);
236 #endif
237 }
238
239 /**
240  * g_rename:
241  * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
242  * @newfilename: a pathname in the GLib file name encoding
243  *
244  * A wrapper for the POSIX rename() function. The rename() function 
245  * renames a file, moving it between directories if required.
246  * 
247  * See your C library manual for more details about how rename() works
248  * on your system. Note in particular that on Win9x it is not possible
249  * to rename a file if a file with the new name already exists. Also
250  * it is not possible in general on Windows to rename an open file.
251  *
252  * Returns: 0 if the renaming succeeded, -1 if an error occurred
253  * 
254  * Since: 2.6
255  */
256 int
257 g_rename (const gchar *oldfilename,
258           const gchar *newfilename)
259 {
260 #ifdef G_OS_WIN32
261   wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
262   wchar_t *wnewfilename;
263   int retval;
264   int save_errno = 0;
265
266   if (woldfilename == NULL)
267     {
268       errno = EINVAL;
269       return -1;
270     }
271
272   wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
273
274   if (wnewfilename == NULL)
275     {
276       g_free (woldfilename);
277       errno = EINVAL;
278       return -1;
279     }
280
281   if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
282     retval = 0;
283   else
284     {
285       retval = -1;
286       switch (GetLastError ())
287         {
288 #define CASE(a,b) case ERROR_##a: save_errno = b; break
289           CASE (FILE_NOT_FOUND, ENOENT);
290           CASE (PATH_NOT_FOUND, ENOENT);
291           CASE (ACCESS_DENIED, EACCES);
292           CASE (NOT_SAME_DEVICE, EXDEV);
293           CASE (LOCK_VIOLATION, EACCES);
294           CASE (SHARING_VIOLATION, EACCES);
295           CASE (FILE_EXISTS, EEXIST);
296           CASE (ALREADY_EXISTS, EEXIST);
297 #undef CASE
298         default: save_errno = EIO;
299         }
300     }
301
302   g_free (woldfilename);
303   g_free (wnewfilename);
304     
305   errno = save_errno;
306   return retval;
307 #else
308   return rename (oldfilename, newfilename);
309 #endif
310 }
311
312 /**
313  * g_mkdir: 
314  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
315  * @mode: permissions to use for the newly created directory
316  *
317  * A wrapper for the POSIX mkdir() function. The mkdir() function 
318  * attempts to create a directory with the given name and permissions.
319  * The mode argument is ignored on Windows.
320  * 
321  * See the C library manual for more details about mkdir().
322  *
323  * Returns: 0 if the directory was successfully created, -1 if an error 
324  *    occurred
325  * 
326  * Since: 2.6
327  */
328 int
329 g_mkdir (const gchar *filename,
330          int          mode)
331 {
332 #ifdef G_OS_WIN32
333   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
334   int retval;
335   int save_errno;
336
337   if (wfilename == NULL)
338     {
339       errno = EINVAL;
340       return -1;
341     }
342
343   retval = _wmkdir (wfilename);
344   save_errno = errno;
345
346   g_free (wfilename);
347     
348   errno = save_errno;
349   return retval;
350 #else
351   return mkdir (filename, mode);
352 #endif
353 }
354
355 /**
356  * g_chdir: 
357  * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
358  *
359  * A wrapper for the POSIX chdir() function. The function changes the
360  * current directory of the process to @path.
361  * 
362  * See your C library manual for more details about chdir().
363  *
364  * Returns: 0 on success, -1 if an error occurred.
365  * 
366  * Since: 2.8
367  */
368 int
369 g_chdir (const gchar *path)
370 {
371 #ifdef G_OS_WIN32
372   wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
373   int retval;
374   int save_errno;
375
376   if (wpath == NULL)
377     {
378       errno = EINVAL;
379       return -1;
380     }
381
382   retval = _wchdir (wpath);
383   save_errno = errno;
384
385   g_free (wpath);
386     
387   errno = save_errno;
388   return retval;
389 #else
390   return chdir (path);
391 #endif
392 }
393
394 /**
395  * g_stat: 
396  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
397  * @buf: a pointer to a <structname>stat</structname> struct, which
398  *    will be filled with the file information
399  *
400  * A wrapper for the POSIX stat() function. The stat() function
401  * returns information about a file. On Windows the stat() function in
402  * the C library checks only the READONLY attribute and does not look
403  * at the ACL at all. Thus the protection bits in the st_mode field
404  * are a fabrication of little use.
405  * 
406  * See the C library manual for more details about stat().
407  *
408  * Returns: 0 if the information was successfully retrieved, -1 if an error 
409  *    occurred
410  * 
411  * Since: 2.6
412  */
413 int
414 g_stat (const gchar *filename,
415         struct stat *buf)
416 {
417 #ifdef G_OS_WIN32
418   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
419   int retval;
420   int save_errno;
421   int len;
422
423   if (wfilename == NULL)
424     {
425       errno = EINVAL;
426       return -1;
427     }
428
429   len = wcslen (wfilename);
430   while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
431     len--;
432   if (len > 0 &&
433       (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
434     wfilename[len] = '\0';
435
436   retval = _wstat (wfilename, (struct _stat *) buf);
437   save_errno = errno;
438
439   g_free (wfilename);
440
441   errno = save_errno;
442   return retval;
443 #else
444   return stat (filename, buf);
445 #endif
446 }
447
448 /**
449  * g_lstat: 
450  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
451  * @buf: a pointer to a <structname>stat</structname> struct, which
452  *    will be filled with the file information
453  *
454  * A wrapper for the POSIX lstat() function. The lstat() function is
455  * like stat() except that in the case of symbolic links, it returns
456  * information about the symbolic link itself and not the file that it
457  * refers to. If the system does not support symbolic links g_lstat()
458  * is identical to g_stat().
459  * 
460  * See the C library manual for more details about lstat().
461  *
462  * Returns: 0 if the information was successfully retrieved, -1 if an error 
463  *    occurred
464  * 
465  * Since: 2.6
466  */
467 int
468 g_lstat (const gchar *filename,
469          struct stat *buf)
470 {
471 #ifdef HAVE_LSTAT
472   /* This can't be Win32, so don't do the widechar dance. */
473   return lstat (filename, buf);
474 #else
475   return g_stat (filename, buf);
476 #endif
477 }
478
479 /**
480  * g_unlink:
481  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
482  *
483  * A wrapper for the POSIX unlink() function. The unlink() function 
484  * deletes a name from the filesystem. If this was the last link to the 
485  * file and no processes have it opened, the diskspace occupied by the
486  * file is freed.
487  * 
488  * See your C library manual for more details about unlink(). Note
489  * that on Windows, it is in general not possible to delete files that
490  * are open to some process, or mapped into memory.
491  *
492  * Returns: 0 if the name was successfully deleted, -1 if an error 
493  *    occurred
494  * 
495  * Since: 2.6
496  */
497 int
498 g_unlink (const gchar *filename)
499 {
500 #ifdef G_OS_WIN32
501   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
502   int retval;
503   int save_errno;
504
505   if (wfilename == NULL)
506     {
507       errno = EINVAL;
508       return -1;
509     }
510
511   retval = _wunlink (wfilename);
512   save_errno = errno;
513
514   g_free (wfilename);
515
516   errno = save_errno;
517   return retval;
518 #else
519   return unlink (filename);
520 #endif
521 }
522
523 /**
524  * g_remove:
525  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
526  *
527  * A wrapper for the POSIX remove() function. The remove() function
528  * deletes a name from the filesystem.
529  * 
530  * See your C library manual for more details about how remove() works
531  * on your system. On Unix, remove() removes also directories, as it
532  * calls unlink() for files and rmdir() for directories. On Windows,
533  * although remove() in the C library only works for files, this
534  * function tries first remove() and then if that fails rmdir(), and
535  * thus works for both files and directories. Note however, that on
536  * Windows, it is in general not possible to remove a file that is
537  * open to some process, or mapped into memory.
538  *
539  * If this function fails on Windows you can't infer too much from the
540  * errno value. rmdir() is tried regardless of what caused remove() to
541  * fail. Any errno value set by remove() will be overwritten by that
542  * set by rmdir().
543  *
544  * Returns: 0 if the file was successfully removed, -1 if an error 
545  *    occurred
546  * 
547  * Since: 2.6
548  */
549 int
550 g_remove (const gchar *filename)
551 {
552 #ifdef G_OS_WIN32
553   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
554   int retval;
555   int save_errno;
556
557   if (wfilename == NULL)
558     {
559       errno = EINVAL;
560       return -1;
561     }
562
563   retval = _wremove (wfilename);
564   if (retval == -1)
565     retval = _wrmdir (wfilename);
566   save_errno = errno;
567
568   g_free (wfilename);
569
570   errno = save_errno;
571   return retval;
572 #else
573   return remove (filename);
574 #endif
575 }
576
577 /**
578  * g_rmdir:
579  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
580  *
581  * A wrapper for the POSIX rmdir() function. The rmdir() function
582  * deletes a directory from the filesystem.
583  * 
584  * See your C library manual for more details about how rmdir() works
585  * on your system.
586  *
587  * Returns: 0 if the directory was successfully removed, -1 if an error 
588  *    occurred
589  * 
590  * Since: 2.6
591  */
592 int
593 g_rmdir (const gchar *filename)
594 {
595 #ifdef G_OS_WIN32
596   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
597   int retval;
598   int save_errno;
599
600   if (wfilename == NULL)
601     {
602       errno = EINVAL;
603       return -1;
604     }
605   
606   retval = _wrmdir (wfilename);
607   save_errno = errno;
608
609   g_free (wfilename);
610
611   errno = save_errno;
612   return retval;
613 #else
614   return rmdir (filename);
615 #endif
616 }
617
618 /**
619  * g_fopen:
620  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
621  * @mode: a string describing the mode in which the file should be 
622  *   opened
623  *
624  * A wrapper for the POSIX fopen() function. The fopen() function opens
625  * a file and associates a new stream with it. 
626  * 
627  * See the C library manual for more details about fopen().
628  *
629  * Returns: A <type>FILE</type> pointer if the file was successfully
630  *    opened, or %NULL if an error occurred
631  * 
632  * Since: 2.6
633  */
634 FILE *
635 g_fopen (const gchar *filename,
636          const gchar *mode)
637 {
638 #ifdef G_OS_WIN32
639   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
640   wchar_t *wmode;
641   FILE *retval;
642   int save_errno;
643
644   if (wfilename == NULL)
645     {
646       errno = EINVAL;
647       return NULL;
648     }
649
650   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
651
652   if (wmode == NULL)
653     {
654       g_free (wfilename);
655       errno = EINVAL;
656       return NULL;
657     }
658
659   retval = _wfopen (wfilename, wmode);
660   save_errno = errno;
661
662   g_free (wfilename);
663   g_free (wmode);
664
665   errno = save_errno;
666   return retval;
667 #else
668   return fopen (filename, mode);
669 #endif
670 }
671
672 /**
673  * g_freopen:
674  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
675  * @mode: a string describing the mode in which the file should be 
676  *   opened
677  * @stream: an existing stream which will be reused, or %NULL
678  *
679  * A wrapper for the POSIX freopen() function. The freopen() function
680  * opens a file and associates it with an existing stream.
681  * 
682  * See the C library manual for more details about freopen().
683  *
684  * Returns: A <type>FILE</type> pointer if the file was successfully
685  *    opened, or %NULL if an error occurred.
686  * 
687  * Since: 2.6
688  */
689 FILE *
690 g_freopen (const gchar *filename,
691            const gchar *mode,
692            FILE        *stream)
693 {
694 #ifdef G_OS_WIN32
695   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
696   wchar_t *wmode;
697   FILE *retval;
698   int save_errno;
699
700   if (wfilename == NULL)
701     {
702       errno = EINVAL;
703       return NULL;
704     }
705   
706   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
707
708   if (wmode == NULL)
709     {
710       g_free (wfilename);
711       errno = EINVAL;
712       return NULL;
713     }
714   
715   retval = _wfreopen (wfilename, wmode, stream);
716   save_errno = errno;
717
718   g_free (wfilename);
719   g_free (wmode);
720
721   errno = save_errno;
722   return retval;
723 #else
724   return freopen (filename, mode, stream);
725 #endif
726 }
727
728 #define __G_STDIO_C__
729 #include "galiasdef.c"