1 /* Copyright (C) 1998, 2001-2004, 2006, 2009 Free Software Foundation, Inc.
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
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.
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/>. */
16 /* derived from a function in touch.c */
21 #include <sys/types.h>
28 # include <sys/stat.h>
35 #include "full-write.h"
36 #include "safe-read.h"
38 /* Some systems (even some that do have <utime.h>) don't declare this
39 structure anywhere. */
40 #ifndef HAVE_STRUCT_UTIMBUF
48 /* The results of open() in this file are not used with fchdir,
49 therefore save some unnecessary work in fchdir.c. */
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. */
58 utime_null (const char *file)
61 return utimes (file, 0);
69 fd = open (file, O_RDWR);
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.
79 || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0))
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. */
101 rpl_utime (const char *file, const struct utimbuf *times)
104 return utime (file, times);
106 return utime_null (file);