[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[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  * see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21 #include "glibconfig.h"
22
23 #define G_STDIO_NO_WRAP_ON_UNIX
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #ifdef G_OS_UNIX
30 #include <unistd.h>
31 #endif
32
33 #ifdef G_OS_WIN32
34 #include <windows.h>
35 #include <errno.h>
36 #include <wchar.h>
37 #include <direct.h>
38 #include <io.h>
39 #include <sys/utime.h>
40 #else
41 #include <utime.h>
42 #include <errno.h>
43 #endif
44
45 #include "gstdio.h"
46
47
48 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
49 #error Please port this to your operating system
50 #endif
51
52 #if defined (_MSC_VER) && !defined(_WIN64)
53 #undef _wstat
54 #define _wstat _wstat32
55 #endif
56
57 /**
58  * g_access:
59  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
60  * @mode: as in access()
61  *
62  * A wrapper for the POSIX access() function. This function is used to
63  * test a pathname for one or several of read, write or execute
64  * permissions, or just existence.
65  *
66  * On Windows, the file protection mechanism is not at all POSIX-like,
67  * and the underlying function in the C library only checks the
68  * FAT-style READONLY attribute, and does not look at the ACL of a
69  * file at all. This function is this in practise almost useless on
70  * Windows. Software that needs to handle file permissions on Windows
71  * more exactly should use the Win32 API.
72  *
73  * See your C library manual for more details about access().
74  *
75  * Returns: zero if the pathname refers to an existing file system
76  *     object that has all the tested permissions, or -1 otherwise
77  *     or on error.
78  * 
79  * Since: 2.8
80  */
81 int
82 g_access (const gchar *filename,
83           int          mode)
84 {
85 #ifdef G_OS_WIN32
86   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
87   int retval;
88   int save_errno;
89     
90   if (wfilename == NULL)
91     {
92       errno = EINVAL;
93       return -1;
94     }
95
96 #ifndef X_OK
97 #define X_OK 1
98 #endif
99
100   retval = _waccess (wfilename, mode & ~X_OK);
101   save_errno = errno;
102
103   g_free (wfilename);
104
105   errno = save_errno;
106   return retval;
107 #else
108   return access (filename, mode);
109 #endif
110 }
111
112 /**
113  * g_chmod:
114  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
115  * @mode: as in chmod()
116  *
117  * A wrapper for the POSIX chmod() function. The chmod() function is
118  * used to set the permissions of a file system object.
119  * 
120  * On Windows the file protection mechanism is not at all POSIX-like,
121  * and the underlying chmod() function in the C library just sets or
122  * clears the FAT-style READONLY attribute. It does not touch any
123  * ACL. Software that needs to manage file permissions on Windows
124  * exactly should use the Win32 API.
125  *
126  * See your C library manual for more details about chmod().
127  *
128  * Returns: 0 if the operation succeeded, -1 on error
129  * 
130  * Since: 2.8
131  */
132 int
133 g_chmod (const gchar *filename,
134          int          mode)
135 {
136 #ifdef G_OS_WIN32
137   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
138   int retval;
139   int save_errno;
140     
141   if (wfilename == NULL)
142     {
143       errno = EINVAL;
144       return -1;
145     }
146
147   retval = _wchmod (wfilename, mode);
148   save_errno = errno;
149
150   g_free (wfilename);
151
152   errno = save_errno;
153   return retval;
154 #else
155   return chmod (filename, mode);
156 #endif
157 }
158 /**
159  * g_open:
160  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
161  * @flags: as in open()
162  * @mode: as in open()
163  *
164  * A wrapper for the POSIX open() function. The open() function is
165  * used to convert a pathname into a file descriptor.
166  *
167  * On POSIX systems file descriptors are implemented by the operating
168  * system. On Windows, it's the C library that implements open() and
169  * file descriptors. The actual Win32 API for opening files is quite
170  * different, see MSDN documentation for CreateFile(). The Win32 API
171  * uses file handles, which are more randomish integers, not small
172  * integers like file descriptors.
173  *
174  * Because file descriptors are specific to the C library on Windows,
175  * the file descriptor returned by this function makes sense only to
176  * functions in the same C library. Thus if the GLib-using code uses a
177  * different C library than GLib does, the file descriptor returned by
178  * this function cannot be passed to C library functions like write()
179  * or read().
180  *
181  * See your C library manual for more details about open().
182  *
183  * Returns: a new file descriptor, or -1 if an error occurred.
184  *     The return value can be used exactly like the return value
185  *     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.
247  *     The return value can be used exactly like the return value
248  *     from creat().
249  * 
250  * Since: 2.8
251  */
252 int
253 g_creat (const gchar *filename,
254          int          mode)
255 {
256 #ifdef G_OS_WIN32
257   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
258   int retval;
259   int save_errno;
260     
261   if (wfilename == NULL)
262     {
263       errno = EINVAL;
264       return -1;
265     }
266
267   retval = _wcreat (wfilename, mode);
268   save_errno = errno;
269
270   g_free (wfilename);
271
272   errno = save_errno;
273   return retval;
274 #else
275   return creat (filename, mode);
276 #endif
277 }
278
279 /**
280  * g_rename:
281  * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
282  * @newfilename: a pathname in the GLib file name encoding
283  *
284  * A wrapper for the POSIX rename() function. The rename() function 
285  * renames a file, moving it between directories if required.
286  * 
287  * See your C library manual for more details about how rename() works
288  * on your system. It is not possible in general on Windows to rename
289  * a file that is open to some process.
290  *
291  * Returns: 0 if the renaming succeeded, -1 if an error occurred
292  * 
293  * Since: 2.6
294  */
295 int
296 g_rename (const gchar *oldfilename,
297           const gchar *newfilename)
298 {
299 #ifdef G_OS_WIN32
300   wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
301   wchar_t *wnewfilename;
302   int retval;
303   int save_errno = 0;
304
305   if (woldfilename == NULL)
306     {
307       errno = EINVAL;
308       return -1;
309     }
310
311   wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
312
313   if (wnewfilename == NULL)
314     {
315       g_free (woldfilename);
316       errno = EINVAL;
317       return -1;
318     }
319
320   if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
321     retval = 0;
322   else
323     {
324       retval = -1;
325       switch (GetLastError ())
326         {
327 #define CASE(a,b) case ERROR_##a: save_errno = b; break
328           CASE (FILE_NOT_FOUND, ENOENT);
329           CASE (PATH_NOT_FOUND, ENOENT);
330           CASE (ACCESS_DENIED, EACCES);
331           CASE (NOT_SAME_DEVICE, EXDEV);
332           CASE (LOCK_VIOLATION, EACCES);
333           CASE (SHARING_VIOLATION, EACCES);
334           CASE (FILE_EXISTS, EEXIST);
335           CASE (ALREADY_EXISTS, EEXIST);
336 #undef CASE
337         default: save_errno = EIO;
338         }
339     }
340
341   g_free (woldfilename);
342   g_free (wnewfilename);
343     
344   errno = save_errno;
345   return retval;
346 #else
347   return rename (oldfilename, newfilename);
348 #endif
349 }
350
351 /**
352  * g_mkdir: 
353  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
354  * @mode: permissions to use for the newly created directory
355  *
356  * A wrapper for the POSIX mkdir() function. The mkdir() function 
357  * attempts to create a directory with the given name and permissions.
358  * The mode argument is ignored on Windows.
359  * 
360  * See your C library manual for more details about mkdir().
361  *
362  * Returns: 0 if the directory was successfully created, -1 if an error 
363  *    occurred
364  * 
365  * Since: 2.6
366  */
367 int
368 g_mkdir (const gchar *filename,
369          int          mode)
370 {
371 #ifdef G_OS_WIN32
372   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
373   int retval;
374   int save_errno;
375
376   if (wfilename == NULL)
377     {
378       errno = EINVAL;
379       return -1;
380     }
381
382   retval = _wmkdir (wfilename);
383   save_errno = errno;
384
385   g_free (wfilename);
386     
387   errno = save_errno;
388   return retval;
389 #else
390   return mkdir (filename, mode);
391 #endif
392 }
393
394 /**
395  * g_chdir: 
396  * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
397  *
398  * A wrapper for the POSIX chdir() function. The function changes the
399  * current directory of the process to @path.
400  * 
401  * See your C library manual for more details about chdir().
402  *
403  * Returns: 0 on success, -1 if an error occurred.
404  * 
405  * Since: 2.8
406  */
407 int
408 g_chdir (const gchar *path)
409 {
410 #ifdef G_OS_WIN32
411   wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
412   int retval;
413   int save_errno;
414
415   if (wpath == NULL)
416     {
417       errno = EINVAL;
418       return -1;
419     }
420
421   retval = _wchdir (wpath);
422   save_errno = errno;
423
424   g_free (wpath);
425     
426   errno = save_errno;
427   return retval;
428 #else
429   return chdir (path);
430 #endif
431 }
432
433 /**
434  * GStatBuf:
435  *
436  * A type corresponding to the appropriate struct type for the stat()
437  * system call, depending on the platform and/or compiler being used.
438  *
439  * See g_stat() for more information.
440  */
441 /**
442  * g_stat: 
443  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
444  * @buf: a pointer to a stat struct, which will be filled with the file
445  *     information
446  *
447  * A wrapper for the POSIX stat() function. The stat() function
448  * returns information about a file. On Windows the stat() function in
449  * the C library checks only the FAT-style READONLY attribute and does
450  * not look at the ACL at all. Thus on Windows the protection bits in
451  * the @st_mode field are a fabrication of little use.
452  * 
453  * On Windows the Microsoft C libraries have several variants of the
454  * stat struct and stat() function with names like _stat(), _stat32(),
455  * _stat32i64() and _stat64i32(). The one used here is for 32-bit code
456  * the one with 32-bit size and time 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, stat()
464  * 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,
469  *     -1 if an error 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 stat struct, which will be filled with the file
512  *     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,
523  *     -1 if an error 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 opened
682  *
683  * A wrapper for the stdio fopen() function. The fopen() function
684  * opens a file and associates a new stream with it.
685  * 
686  * Because file descriptors are specific to the C library on Windows,
687  * and a file descriptor is part of the FILE struct, the FILE* returned
688  * by this function makes sense only to functions in the same C library.
689  * Thus if the GLib-using code uses a different C library than GLib does,
690  * the FILE* returned by this function cannot be passed to C library
691  * functions like fprintf() or fread().
692  *
693  * See your C library manual for more details about fopen().
694  *
695  * Returns: A FILE* if the file was successfully opened, or %NULL if
696  *     an error occurred
697  * 
698  * Since: 2.6
699  */
700 FILE *
701 g_fopen (const gchar *filename,
702          const gchar *mode)
703 {
704 #ifdef G_OS_WIN32
705   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
706   wchar_t *wmode;
707   FILE *retval;
708   int save_errno;
709
710   if (wfilename == NULL)
711     {
712       errno = EINVAL;
713       return NULL;
714     }
715
716   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
717
718   if (wmode == NULL)
719     {
720       g_free (wfilename);
721       errno = EINVAL;
722       return NULL;
723     }
724
725   retval = _wfopen (wfilename, wmode);
726   save_errno = errno;
727
728   g_free (wfilename);
729   g_free (wmode);
730
731   errno = save_errno;
732   return retval;
733 #else
734   return fopen (filename, mode);
735 #endif
736 }
737
738 /**
739  * g_freopen:
740  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
741  * @mode: a string describing the mode in which the file should be  opened
742  * @stream: (allow-none): an existing stream which will be reused, or %NULL
743  *
744  * A wrapper for the POSIX freopen() function. The freopen() function
745  * opens a file and associates it with an existing stream.
746  * 
747  * See your C library manual for more details about freopen().
748  *
749  * Returns: A FILE* if the file was successfully opened, or %NULL if
750  *     an error occurred.
751  * 
752  * Since: 2.6
753  */
754 FILE *
755 g_freopen (const gchar *filename,
756            const gchar *mode,
757            FILE        *stream)
758 {
759 #ifdef G_OS_WIN32
760   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
761   wchar_t *wmode;
762   FILE *retval;
763   int save_errno;
764
765   if (wfilename == NULL)
766     {
767       errno = EINVAL;
768       return NULL;
769     }
770   
771   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
772
773   if (wmode == NULL)
774     {
775       g_free (wfilename);
776       errno = EINVAL;
777       return NULL;
778     }
779   
780   retval = _wfreopen (wfilename, wmode, stream);
781   save_errno = errno;
782
783   g_free (wfilename);
784   g_free (wmode);
785
786   errno = save_errno;
787   return retval;
788 #else
789   return freopen (filename, mode, stream);
790 #endif
791 }
792
793 /**
794  * g_utime:
795  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
796  * @utb: a pointer to a struct utimbuf.
797  *
798  * A wrapper for the POSIX utime() function. The utime() function
799  * sets the access and modification timestamps of a file.
800  * 
801  * See your C library manual for more details about how utime() works
802  * on your system.
803  *
804  * Returns: 0 if the operation was successful, -1 if an error occurred
805  * 
806  * Since: 2.18
807  */
808 int
809 g_utime (const gchar    *filename,
810          struct utimbuf *utb)
811 {
812 #ifdef G_OS_WIN32
813   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
814   int retval;
815   int save_errno;
816
817   if (wfilename == NULL)
818     {
819       errno = EINVAL;
820       return -1;
821     }
822   
823   retval = _wutime (wfilename, (struct _utimbuf*) utb);
824   save_errno = errno;
825
826   g_free (wfilename);
827
828   errno = save_errno;
829   return retval;
830 #else
831   return utime (filename, utb);
832 #endif
833 }
834
835 /**
836  * g_close:
837  * @fd: A file descriptor
838  * @error: a #GError
839  *
840  * This wraps the close() call; in case of error, %errno will be
841  * preserved, but the error will also be stored as a #GError in @error.
842  *
843  * Besides using #GError, there is another major reason to prefer this
844  * function over the call provided by the system; on Unix, it will
845  * attempt to correctly handle %EINTR, which has platform-specific
846  * semantics.
847  *
848  * Since: 2.36
849  */
850 gboolean
851 g_close (gint       fd,
852          GError   **error)
853 {
854   int res;
855   res = close (fd);
856   /* Just ignore EINTR for now; a retry loop is the wrong thing to do
857    * on Linux at least.  Anyone who wants to add a conditional check
858    * for e.g. HP-UX is welcome to do so later...
859    *
860    * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
861    * https://bugzilla.gnome.org/show_bug.cgi?id=682819
862    * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
863    * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
864    */
865   if (G_UNLIKELY (res == -1 && errno == EINTR))
866     return TRUE;
867   else if (res == -1)
868     {
869       int errsv = errno;
870       g_set_error_literal (error, G_FILE_ERROR,
871                            g_file_error_from_errno (errsv),
872                            g_strerror (errsv));
873       errno = errsv;
874       return FALSE;
875     }
876   return TRUE;
877 }
878