1 /* Copyright (C) 1998, 2001, 2002, 2003, 2004, 2006 Free Software
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 3 of the License, or any
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* derived from a function in touch.c */
22 #include <sys/types.h>
29 # include <sys/stat.h>
36 #include "full-write.h"
37 #include "safe-read.h"
39 /* Some systems (even some that do have <utime.h>) don't declare this
40 structure anywhere. */
41 #ifndef HAVE_STRUCT_UTIMBUF
49 /* The results of open() in this file are not used with fchdir,
50 therefore save some unnecessary work in fchdir.c. */
54 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
55 interpret it to set the access and modification times of FILE to
56 the current time. Return 0 if successful, -1 if not. */
59 utime_null (const char *file)
62 return utimes (file, 0);
70 fd = open (file, O_RDWR);
72 || fstat (fd, &st) < 0
73 || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR
74 || lseek (fd, (off_t) 0, SEEK_SET) < 0
75 || full_write (fd, &c, sizeof c) != sizeof c
76 /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination
77 of patches, but that system doesn't use this code: it has utimes.
80 || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0))
91 /* If there was a prior failure, use the saved errno value.
92 But if the only failure was in the close, don't change errno. */
102 rpl_utime (const char *file, const struct utimbuf *times)
105 return utime (file, times);
107 return utime_null (file);