* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / utime.c
1 /* Copyright (C) 1998, 2001, 2002, 2003, 2004, 2006 Free Software
2    Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 2, or (at your option) any
7    later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* derived from a function in touch.c */
19
20 #include <config.h>
21 #undef utime
22
23 #include <sys/types.h>
24
25 #ifdef HAVE_UTIME_H
26 # include <utime.h>
27 #endif
28
29 #if !HAVE_UTIMES_NULL
30 # include <sys/stat.h>
31 # include <fcntl.h>
32 #endif
33
34 #include <unistd.h>
35 #include <errno.h>
36
37 #include "full-write.h"
38 #include "safe-read.h"
39
40 /* Some systems (even some that do have <utime.h>) don't declare this
41    structure anywhere.  */
42 #ifndef HAVE_STRUCT_UTIMBUF
43 struct utimbuf
44 {
45   long actime;
46   long modtime;
47 };
48 #endif
49
50 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
51    interpret it to set the access and modification times of FILE to
52    the current time.  Return 0 if successful, -1 if not. */
53
54 static int
55 utime_null (const char *file)
56 {
57 #if HAVE_UTIMES_NULL
58   return utimes (file, 0);
59 #else
60   int fd;
61   char c;
62   int status = 0;
63   struct stat st;
64   int saved_errno = 0;
65
66   fd = open (file, O_RDWR);
67   if (fd < 0
68       || fstat (fd, &st) < 0
69       || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR
70       || lseek (fd, (off_t) 0, SEEK_SET) < 0
71       || full_write (fd, &c, sizeof c) != sizeof c
72       /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination
73          of patches, but that system doesn't use this code: it has utimes.
74          || fsync (fd) < 0
75       */
76       || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0))
77     {
78       saved_errno = errno;
79       status = -1;
80     }
81
82   if (0 <= fd)
83     {
84       if (close (fd) < 0)
85         status = -1;
86
87       /* If there was a prior failure, use the saved errno value.
88          But if the only failure was in the close, don't change errno.  */
89       if (saved_errno)
90         errno = saved_errno;
91     }
92
93   return status;
94 #endif
95 }
96
97 int
98 rpl_utime (const char *file, const struct utimbuf *times)
99 {
100   if (times)
101     return utime (file, times);
102
103   return utime_null (file);
104 }