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