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