Remove suppression-related options.
[coreutils.git] / lib / utime.c
blob05aa3467ccc947aa3343570f5581ebd11b0edade
1 /* Copyright (C) 1998, 2001, 2002, 2003, 2004 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 2, or (at your option) any
6 later version.
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, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 /* derived from a function in touch.c */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #undef utime
24 #include <sys/types.h>
26 #ifdef HAVE_UTIME_H
27 # include <utime.h>
28 #endif
30 #if !HAVE_UTIMES_NULL
31 # include <sys/stat.h>
32 # include <fcntl.h>
33 #endif
35 #include <unistd.h>
36 #include <errno.h>
38 #include "full-write.h"
39 #include "safe-read.h"
41 /* Some systems (even some that do have <utime.h>) don't declare this
42 structure anywhere. */
43 #ifndef HAVE_STRUCT_UTIMBUF
44 struct utimbuf
46 long actime;
47 long modtime;
49 #endif
51 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
52 interpret it to set the access and modification times of FILE to
53 the current time. Return 0 if successful, -1 if not. */
55 static int
56 utime_null (const char *file)
58 #if HAVE_UTIMES_NULL
59 return utimes (file, 0);
60 #else
61 int fd;
62 char c;
63 int status = 0;
64 struct stat st;
65 int saved_errno = 0;
67 fd = open (file, O_RDWR);
68 if (fd < 0
69 || fstat (fd, &st) < 0
70 || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR
71 || lseek (fd, (off_t) 0, SEEK_SET) < 0
72 || full_write (fd, &c, sizeof c) != sizeof c
73 /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination
74 of patches, but that system doesn't use this code: it has utimes.
75 || fsync (fd) < 0
77 || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0))
79 saved_errno = errno;
80 status = -1;
83 if (0 <= fd)
85 if (close (fd) < 0)
86 status = -1;
88 /* If there was a prior failure, use the saved errno value.
89 But if the only failure was in the close, don't change errno. */
90 if (saved_errno)
91 errno = saved_errno;
94 return status;
95 #endif
98 int
99 rpl_utime (const char *file, const struct utimbuf *times)
101 if (times)
102 return utime (file, times);
104 return utime_null (file);