Fix the GObject Visual Studio Projects
[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 "glibconfig.h"
23
24 #define G_STDIO_NO_WRAP_ON_UNIX
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 <windows.h>
36 #include <errno.h>
37 #include <wchar.h>
38 #include <direct.h>
39 #include <io.h>
40 #include <sys/utime.h>
41 #else
42 #include <utime.h>
43 #include <errno.h>
44 #endif
45
46 #include "gstdio.h"
47
48
49 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
50 #error Please port this to your operating system
51 #endif
52
53 #if defined (_MSC_VER) && !defined(_WIN64)
54 #undef _wstat
55 #define _wstat _wstat32
56 #endif
57
58 /**
59  * g_access:
60  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
61  * @mode: as in access()
62  *
63  * A wrapper for the POSIX access() function. This function is used to
64  * test a pathname for one or several of read, write or execute
65  * permissions, or just existence.
66  *
67  * On Windows, the file protection mechanism is not at all POSIX-like,
68  * and the underlying function in the C library only checks the
69  * FAT-style READONLY attribute, and does not look at the ACL of a
70  * file at all. This function is this in practise almost useless on
71  * Windows. Software that needs to handle file permissions on Windows
72  * more exactly should use the Win32 API.
73  *
74  * See your C library manual for more details about access().
75  *
76  * Returns: zero if the pathname refers to an existing file system
77  * object that has all the tested permissions, or -1 otherwise or on
78  * error.
79  * 
80  * Since: 2.8
81  */
82 int
83 g_access (const gchar *filename,
84           int          mode)
85 {
86 #ifdef G_OS_WIN32
87   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
88   int retval;
89   int save_errno;
90     
91   if (wfilename == NULL)
92     {
93       errno = EINVAL;
94       return -1;
95     }
96
97 #ifndef X_OK
98 #define X_OK 1
99 #endif
100
101   retval = _waccess (wfilename, mode & ~X_OK);
102   save_errno = errno;
103
104   g_free (wfilename);
105
106   errno = save_errno;
107   return retval;
108 #else
109   return access (filename, mode);
110 #endif
111 }
112
113 /**
114  * g_chmod:
115  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
116  * @mode: as in chmod()
117  *
118  * A wrapper for the POSIX chmod() function. The chmod() function is
119  * used to set the permissions of a file system object.
120  * 
121  * On Windows the file protection mechanism is not at all POSIX-like,
122  * and the underlying chmod() function in the C library just sets or
123  * clears the FAT-style READONLY attribute. It does not touch any
124  * ACL. Software that needs to manage file permissions on Windows
125  * exactly should use the Win32 API.
126  *
127  * See your C library manual for more details about chmod().
128  *
129  * Returns: zero if the operation succeeded, -1 on error.
130  * 
131  * Since: 2.8
132  */
133 int
134 g_chmod (const gchar *filename,
135          int          mode)
136 {
137 #ifdef G_OS_WIN32
138   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
139   int retval;
140   int save_errno;
141     
142   if (wfilename == NULL)
143     {
144       errno = EINVAL;
145       return -1;
146     }
147
148   retval = _wchmod (wfilename, mode);
149   save_errno = errno;
150
151   g_free (wfilename);
152
153   errno = save_errno;
154   return retval;
155 #else
156   return chmod (filename, mode);
157 #endif
158 }
159 /**
160  * g_open:
161  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
162  * @flags: as in open()
163  * @mode: as in open()
164  *
165  * A wrapper for the POSIX open() function. The open() function is
166  * used to convert a pathname into a file descriptor.
167  *
168  * On POSIX systems file descriptors are implemented by the operating
169  * system. On Windows, it's the C library that implements open() and
170  * file descriptors. The actual Win32 API for opening files is quite
171  * different, see MSDN documentation for CreateFile(). The Win32 API
172  * uses file handles, which are more randomish integers, not small
173  * integers like file descriptors.
174  *
175  * Because file descriptors are specific to the C library on Windows,
176  * the file descriptor returned by this function makes sense only to
177  * functions in the same C library. Thus if the GLib-using code uses a
178  * different C library than GLib does, the file descriptor returned by
179  * this function cannot be passed to C library functions like write()
180  * or read().
181  *
182  * See your C library manual for more details about open().
183  *
184  * Returns: a new file descriptor, or -1 if an error occurred. The
185  * return value can be used exactly like the return value from open().
186  * 
187  * Since: 2.6
188  */
189 int
190 g_open (const gchar *filename,
191         int          flags,
192         int          mode)
193 {
194 #ifdef G_OS_WIN32
195   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
196   int retval;
197   int save_errno;
198     
199   if (wfilename == NULL)
200     {
201       errno = EINVAL;
202       return -1;
203     }
204
205   retval = _wopen (wfilename, flags, mode);
206   save_errno = errno;
207
208   g_free (wfilename);
209
210   errno = save_errno;
211   return retval;
212 #else
213   int fd;
214   do
215     fd = open (filename, flags, mode);
216   while (G_UNLIKELY (fd == -1 && errno == EINTR));
217   return fd;
218 #endif
219 }
220
221 /**
222  * g_creat:
223  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
224  * @mode: as in creat()
225  *
226  * A wrapper for the POSIX creat() function. The creat() function is
227  * used to convert a pathname into a file descriptor, creating a file
228  * if necessary.
229
230  * On POSIX systems file descriptors are implemented by the operating
231  * system. On Windows, it's the C library that implements creat() and
232  * file descriptors. The actual Windows API for opening files is
233  * different, see MSDN documentation for CreateFile(). The Win32 API
234  * uses file handles, which are more randomish integers, not small
235  * integers like file descriptors.
236  *
237  * Because file descriptors are specific to the C library on Windows,
238  * the file descriptor returned by this function makes sense only to
239  * functions in the same C library. Thus if the GLib-using code uses a
240  * different C library than GLib does, the file descriptor returned by
241  * this function cannot be passed to C library functions like write()
242  * or read().
243  *
244  * See your C library manual for more details about creat().
245  *
246  * Returns: a new file descriptor, or -1 if an error occurred. The
247  * return value can be used exactly like the return value from creat().
248  * 
249  * Since: 2.8
250  */
251 int
252 g_creat (const gchar *filename,
253          int          mode)
254 {
255 #ifdef G_OS_WIN32
256   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
257   int retval;
258   int save_errno;
259     
260   if (wfilename == NULL)
261     {
262       errno = EINVAL;
263       return -1;
264     }
265
266   retval = _wcreat (wfilename, mode);
267   save_errno = errno;
268
269   g_free (wfilename);
270
271   errno = save_errno;
272   return retval;
273 #else
274   return creat (filename, mode);
275 #endif
276 }
277
278 /**
279  * g_rename:
280  * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
281  * @newfilename: a pathname in the GLib file name encoding
282  *
283  * A wrapper for the POSIX rename() function. The rename() function 
284  * renames a file, moving it between directories if required.
285  * 
286  * See your C library manual for more details about how rename() works
287  * on your system. It is not possible in general on Windows to rename
288  * a file that is open to some process.
289  *
290  * Returns: 0 if the renaming succeeded, -1 if an error occurred
291  * 
292  * Since: 2.6
293  */
294 int
295 g_rename (const gchar *oldfilename,
296           const gchar *newfilename)
297 {
298 #ifdef G_OS_WIN32
299   wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
300   wchar_t *wnewfilename;
301   int retval;
302   int save_errno = 0;
303
304   if (woldfilename == NULL)
305     {
306       errno = EINVAL;
307       return -1;
308     }
309
310   wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
311
312   if (wnewfilename == NULL)
313     {
314       g_free (woldfilename);
315       errno = EINVAL;
316       return -1;
317     }
318
319   if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
320     retval = 0;
321   else
322     {
323       retval = -1;
324       switch (GetLastError ())
325         {
326 #define CASE(a,b) case ERROR_##a: save_errno = b; break
327           CASE (FILE_NOT_FOUND, ENOENT);
328           CASE (PATH_NOT_FOUND, ENOENT);
329           CASE (ACCESS_DENIED, EACCES);
330           CASE (NOT_SAME_DEVICE, EXDEV);
331           CASE (LOCK_VIOLATION, EACCES);
332           CASE (SHARING_VIOLATION, EACCES);
333           CASE (FILE_EXISTS, EEXIST);
334           CASE (ALREADY_EXISTS, EEXIST);
335 #undef CASE
336         default: save_errno = EIO;
337         }
338     }
339
340   g_free (woldfilename);
341   g_free (wnewfilename);
342     
343   errno = save_errno;
344   return retval;
345 #else
346   return rename (oldfilename, newfilename);
347 #endif
348 }
349
350 /**
351  * g_mkdir: 
352  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
353  * @mode: permissions to use for the newly created directory
354  *
355  * A wrapper for the POSIX mkdir() function. The mkdir() function 
356  * attempts to create a directory with the given name and permissions.
357  * The mode argument is ignored on Windows.
358  * 
359  * See your C library manual for more details about mkdir().
360  *
361  * Returns: 0 if the directory was successfully created, -1 if an error 
362  *    occurred
363  * 
364  * Since: 2.6
365  */
366 int
367 g_mkdir (const gchar *filename,
368          int          mode)
369 {
370 #ifdef G_OS_WIN32
371   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
372   int retval;
373   int save_errno;
374
375   if (wfilename == NULL)
376     {
377       errno = EINVAL;
378       return -1;
379     }
380
381   retval = _wmkdir (wfilename);
382   save_errno = errno;
383
384   g_free (wfilename);
385     
386   errno = save_errno;
387   return retval;
388 #else
389   return mkdir (filename, mode);
390 #endif
391 }
392
393 /**
394  * g_chdir: 
395  * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
396  *
397  * A wrapper for the POSIX chdir() function. The function changes the
398  * current directory of the process to @path.
399  * 
400  * See your C library manual for more details about chdir().
401  *
402  * Returns: 0 on success, -1 if an error occurred.
403  * 
404  * Since: 2.8
405  */
406 int
407 g_chdir (const gchar *path)
408 {
409 #ifdef G_OS_WIN32
410   wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
411   int retval;
412   int save_errno;
413
414   if (wpath == NULL)
415     {
416       errno = EINVAL;
417       return -1;
418     }
419
420   retval = _wchdir (wpath);
421   save_errno = errno;
422
423   g_free (wpath);
424     
425   errno = save_errno;
426   return retval;
427 #else
428   return chdir (path);
429 #endif
430 }
431
432 /**
433  * GStatBuf:
434  *
435  * A type corresponding to the appropriate struct type for the stat
436  * system call, depending on the platform and/or compiler being used.
437  *
438  * See g_stat() for more information.
439  **/
440 /**
441  * g_stat: 
442  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
443  * @buf: a pointer to a <structname>stat</structname> struct, which
444  *    will be filled with the file information
445  *
446  * A wrapper for the POSIX stat() function. The stat() function
447  * returns information about a file. On Windows the stat() function in
448  * the C library checks only the FAT-style READONLY attribute and does
449  * not look at the ACL at all. Thus on Windows the protection bits in
450  * the st_mode field are a fabrication of little use.
451  * 
452  * On Windows the Microsoft C libraries have several variants of the
453  * <structname>stat</structname> struct and stat() function with names
454  * like "_stat", "_stat32", "_stat32i64" and "_stat64i32". The one
455  * used here is for 32-bit code the one with 32-bit size and time
456  * fields, specifically called "_stat32".
457  *
458  * In Microsoft's compiler, by default "struct stat" means one with
459  * 64-bit time fields while in MinGW "struct stat" is the legacy one
460  * with 32-bit fields. To hopefully clear up this messs, the gstdio.h
461  * header defines a type GStatBuf which is the appropriate struct type
462  * depending on the platform and/or compiler being used. On POSIX it
463  * is just "struct stat", but note that even on POSIX platforms,
464  * "stat" might be a macro.
465  *
466  * See your C library manual for more details about stat().
467  *
468  * Returns: 0 if the information was successfully retrieved, -1 if an error 
469  *    occurred
470  * 
471  * Since: 2.6
472  */
473 int
474 g_stat (const gchar *filename,
475         GStatBuf    *buf)
476 {
477 #ifdef G_OS_WIN32
478   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
479   int retval;
480   int save_errno;
481   int len;
482
483   if (wfilename == NULL)
484     {
485       errno = EINVAL;
486       return -1;
487     }
488
489   len = wcslen (wfilename);
490   while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
491     len--;
492   if (len > 0 &&
493       (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
494     wfilename[len] = '\0';
495
496   retval = _wstat (wfilename, buf);
497   save_errno = errno;
498
499   g_free (wfilename);
500
501   errno = save_errno;
502   return retval;
503 #else
504   return stat (filename, buf);
505 #endif
506 }
507
508 /**
509  * g_lstat: 
510  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
511  * @buf: a pointer to a <structname>stat</structname> struct, which
512  *    will be filled with the file information
513  *
514  * A wrapper for the POSIX lstat() function. The lstat() function is
515  * like stat() except that in the case of symbolic links, it returns
516  * information about the symbolic link itself and not the file that it
517  * refers to. If the system does not support symbolic links g_lstat()
518  * is identical to g_stat().
519  * 
520  * See your C library manual for more details about lstat().
521  *
522  * Returns: 0 if the information was successfully retrieved, -1 if an error 
523  *    occurred
524  * 
525  * Since: 2.6
526  */
527 int
528 g_lstat (const gchar *filename,
529          GStatBuf    *buf)
530 {
531 #ifdef HAVE_LSTAT
532   /* This can't be Win32, so don't do the widechar dance. */
533   return lstat (filename, buf);
534 #else
535   return g_stat (filename, buf);
536 #endif
537 }
538
539 /**
540  * g_unlink:
541  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
542  *
543  * A wrapper for the POSIX unlink() function. The unlink() function 
544  * deletes a name from the filesystem. If this was the last link to the 
545  * file and no processes have it opened, the diskspace occupied by the
546  * file is freed.
547  * 
548  * See your C library manual for more details about unlink(). Note
549  * that on Windows, it is in general not possible to delete files that
550  * are open to some process, or mapped into memory.
551  *
552  * Returns: 0 if the name was successfully deleted, -1 if an error 
553  *    occurred
554  * 
555  * Since: 2.6
556  */
557 int
558 g_unlink (const gchar *filename)
559 {
560 #ifdef G_OS_WIN32
561   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
562   int retval;
563   int save_errno;
564
565   if (wfilename == NULL)
566     {
567       errno = EINVAL;
568       return -1;
569     }
570
571   retval = _wunlink (wfilename);
572   save_errno = errno;
573
574   g_free (wfilename);
575
576   errno = save_errno;
577   return retval;
578 #else
579   return unlink (filename);
580 #endif
581 }
582
583 /**
584  * g_remove:
585  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
586  *
587  * A wrapper for the POSIX remove() function. The remove() function
588  * deletes a name from the filesystem.
589  * 
590  * See your C library manual for more details about how remove() works
591  * on your system. On Unix, remove() removes also directories, as it
592  * calls unlink() for files and rmdir() for directories. On Windows,
593  * although remove() in the C library only works for files, this
594  * function tries first remove() and then if that fails rmdir(), and
595  * thus works for both files and directories. Note however, that on
596  * Windows, it is in general not possible to remove a file that is
597  * open to some process, or mapped into memory.
598  *
599  * If this function fails on Windows you can't infer too much from the
600  * errno value. rmdir() is tried regardless of what caused remove() to
601  * fail. Any errno value set by remove() will be overwritten by that
602  * set by rmdir().
603  *
604  * Returns: 0 if the file was successfully removed, -1 if an error 
605  *    occurred
606  * 
607  * Since: 2.6
608  */
609 int
610 g_remove (const gchar *filename)
611 {
612 #ifdef G_OS_WIN32
613   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
614   int retval;
615   int save_errno;
616
617   if (wfilename == NULL)
618     {
619       errno = EINVAL;
620       return -1;
621     }
622
623   retval = _wremove (wfilename);
624   if (retval == -1)
625     retval = _wrmdir (wfilename);
626   save_errno = errno;
627
628   g_free (wfilename);
629
630   errno = save_errno;
631   return retval;
632 #else
633   return remove (filename);
634 #endif
635 }
636
637 /**
638  * g_rmdir:
639  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
640  *
641  * A wrapper for the POSIX rmdir() function. The rmdir() function
642  * deletes a directory from the filesystem.
643  * 
644  * See your C library manual for more details about how rmdir() works
645  * on your system.
646  *
647  * Returns: 0 if the directory was successfully removed, -1 if an error 
648  *    occurred
649  * 
650  * Since: 2.6
651  */
652 int
653 g_rmdir (const gchar *filename)
654 {
655 #ifdef G_OS_WIN32
656   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
657   int retval;
658   int save_errno;
659
660   if (wfilename == NULL)
661     {
662       errno = EINVAL;
663       return -1;
664     }
665   
666   retval = _wrmdir (wfilename);
667   save_errno = errno;
668
669   g_free (wfilename);
670
671   errno = save_errno;
672   return retval;
673 #else
674   return rmdir (filename);
675 #endif
676 }
677
678 /**
679  * g_fopen:
680  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
681  * @mode: a string describing the mode in which the file should be 
682  *   opened
683  *
684  * A wrapper for the stdio fopen() function. The fopen() function
685  * opens a file and associates a new stream with it.
686  * 
687  * Because file descriptors are specific to the C library on Windows,
688  * and a file descriptor is partof the <type>FILE</type> struct, the
689  * <type>FILE</type> pointer returned by this function makes sense
690  * only to functions in the same C library. Thus if the GLib-using
691  * code uses a different C library than GLib does, the
692  * <type>FILE</type> pointer returned by this function cannot be
693  * passed to C library functions like fprintf() or fread().
694  *
695  * See your C library manual for more details about fopen().
696  *
697  * Returns: A <type>FILE</type> pointer if the file was successfully
698  *    opened, or %NULL if an error occurred
699  * 
700  * Since: 2.6
701  */
702 FILE *
703 g_fopen (const gchar *filename,
704          const gchar *mode)
705 {
706 #ifdef G_OS_WIN32
707   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
708   wchar_t *wmode;
709   FILE *retval;
710   int save_errno;
711
712   if (wfilename == NULL)
713     {
714       errno = EINVAL;
715       return NULL;
716     }
717
718   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
719
720   if (wmode == NULL)
721     {
722       g_free (wfilename);
723       errno = EINVAL;
724       return NULL;
725     }
726
727   retval = _wfopen (wfilename, wmode);
728   save_errno = errno;
729
730   g_free (wfilename);
731   g_free (wmode);
732
733   errno = save_errno;
734   return retval;
735 #else
736   return fopen (filename, mode);
737 #endif
738 }
739
740 /**
741  * g_freopen:
742  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
743  * @mode: a string describing the mode in which the file should be 
744  *   opened
745  * @stream: (allow-none): an existing stream which will be reused, or %NULL
746  *
747  * A wrapper for the POSIX freopen() function. The freopen() function
748  * opens a file and associates it with an existing stream.
749  * 
750  * See your C library manual for more details about freopen().
751  *
752  * Returns: A <literal>FILE</literal> pointer if the file was successfully
753  *    opened, or %NULL if an error occurred.
754  * 
755  * Since: 2.6
756  */
757 FILE *
758 g_freopen (const gchar *filename,
759            const gchar *mode,
760            FILE        *stream)
761 {
762 #ifdef G_OS_WIN32
763   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
764   wchar_t *wmode;
765   FILE *retval;
766   int save_errno;
767
768   if (wfilename == NULL)
769     {
770       errno = EINVAL;
771       return NULL;
772     }
773   
774   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
775
776   if (wmode == NULL)
777     {
778       g_free (wfilename);
779       errno = EINVAL;
780       return NULL;
781     }
782   
783   retval = _wfreopen (wfilename, wmode, stream);
784   save_errno = errno;
785
786   g_free (wfilename);
787   g_free (wmode);
788
789   errno = save_errno;
790   return retval;
791 #else
792   return freopen (filename, mode, stream);
793 #endif
794 }
795
796 /**
797  * g_utime:
798  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
799  * @utb: a pointer to a struct utimbuf.
800  *
801  * A wrapper for the POSIX utime() function. The utime() function
802  * sets the access and modification timestamps of a file.
803  * 
804  * See your C library manual for more details about how utime() works
805  * on your system.
806  *
807  * Returns: 0 if the operation was successful, -1 if an error 
808  *    occurred
809  * 
810  * Since: 2.18
811  */
812 int
813 g_utime (const gchar    *filename,
814          struct utimbuf *utb)
815 {
816 #ifdef G_OS_WIN32
817   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
818   int retval;
819   int save_errno;
820
821   if (wfilename == NULL)
822     {
823       errno = EINVAL;
824       return -1;
825     }
826   
827   retval = _wutime (wfilename, (struct _utimbuf*) utb);
828   save_errno = errno;
829
830   g_free (wfilename);
831
832   errno = save_errno;
833   return retval;
834 #else
835   return utime (filename, utb);
836 #endif
837 }
838
839 /**
840  * g_close:
841  * @fd: A file descriptor
842  * @error: a #GError
843  *
844  * This wraps the close() call; in case of error, %errno will be
845  * preserved, but the error will also be stored as a #GError in @error.
846  *
847  * Besides using #GError, there is another major reason to prefer this
848  * function over the call provided by the system; on Unix, it will
849  * attempt to correctly handle %EINTR, which has platform-specific
850  * semantics.
851  */
852 gboolean
853 g_close (gint       fd,
854          GError   **error)
855 {
856   int res;
857   res = close (fd);
858   /* Just ignore EINTR for now; a retry loop is the wrong thing to do
859    * on Linux at least.  Anyone who wants to add a conditional check
860    * for e.g. HP-UX is welcome to do so later...
861    *
862    * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
863    * https://bugzilla.gnome.org/show_bug.cgi?id=682819
864    * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
865    * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
866    */
867   if (G_UNLIKELY (res == -1 && errno == EINTR))
868     return TRUE;
869   else if (res == -1)
870     {
871       int errsv = errno;
872       g_set_error_literal (error, G_FILE_ERROR,
873                            g_file_error_from_errno (errsv),
874                            g_strerror (errsv));
875       errno = errsv;
876       return FALSE;
877     }
878   return TRUE;
879 }
880