(recheck): Handle a race condition (including <dev,inode>
[coreutils.git] / src / touch.c
blob03366564d69f5062e0a20db4fd3d6baa7e78b84d
1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
19 and Randy Smith. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
26 #include "system.h"
27 #include "argmatch.h"
28 #include "error.h"
29 #include "getdate.h"
30 #include "posixtm.h"
31 #include "safe-read.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "touch"
36 #define AUTHORS \
37 "Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie, and Randy Smith"
39 #ifndef STDC_HEADERS
40 time_t time ();
41 #endif
43 /* Bitmasks for `change_times'. */
44 #define CH_ATIME 1
45 #define CH_MTIME 2
47 /* The name by which this program was run. */
48 char *program_name;
50 /* Which timestamps to change. */
51 static int change_times;
53 /* (-c) If nonzero, don't create if not already there. */
54 static int no_create;
56 /* (-d) If nonzero, date supplied on command line in get_date formats. */
57 static int flexible_date;
59 /* (-r) If nonzero, use times from a reference file. */
60 static int use_ref;
62 /* (-t) If nonzero, date supplied on command line in POSIX format. */
63 static int posix_date;
65 /* If nonzero, the only thing we have to do is change both the
66 modification and access time to the current time, so we don't
67 have to own the file, just be able to read and write it.
68 On some systems, we can do this if we own the file, even though
69 we have neither read nor write access to it. */
70 static int amtime_now;
72 /* New time to use when setting time. */
73 static time_t newtime;
75 /* File to use for -r. */
76 static char *ref_file;
78 /* Info about the reference file. */
79 static struct stat ref_stats;
81 static struct option const longopts[] =
83 {"time", required_argument, 0, CHAR_MAX + 1},
84 {"no-create", no_argument, 0, 'c'},
85 {"date", required_argument, 0, 'd'},
86 {"file", required_argument, 0, 'r'}, /* FIXME: phase out --file */
87 {"reference", required_argument, 0, 'r'},
88 {GETOPT_HELP_OPTION_DECL},
89 {GETOPT_VERSION_OPTION_DECL},
90 {0, 0, 0, 0}
93 /* Valid arguments to the `--time' option. */
94 static char const* const time_args[] =
96 "atime", "access", "use", "mtime", "modify", 0
99 /* The bits in `change_times' that those arguments set. */
100 static int const time_masks[] =
102 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
105 /* Update the time of file FILE according to the options given.
106 Return 0 if successful, 1 if an error occurs. */
108 static int
109 touch (const char *file)
111 int status;
112 struct stat sbuf;
113 int fd = -1;
115 if (! no_create)
117 /* Try to open FILE, creating it if necessary. */
118 fd = open (file, O_WRONLY | O_CREAT,
119 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
122 if (! amtime_now)
124 /* We're setting only one of the time values. stat the target to get
125 the other one. If we have the file descriptor already, use fstat.
126 Otherwise, either we're in no-create mode (and hence didn't call open)
127 or FILE is inaccessible or a directory, so we have to use stat. */
128 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
130 error (0, errno, "%s", file);
131 close (fd);
132 return 1;
136 if (fd != -1 && close (fd) < 0)
138 error (0, errno, "%s", file);
139 return 1;
142 if (amtime_now)
144 /* Pass NULL to utime so it will not fail if we just have
145 write access to the file, but don't own it. */
146 status = utime (file, NULL);
148 else
150 struct utimbuf utb;
152 /* There's currently no interface to set file timestamps with
153 better than 1-second resolution, so discard any fractional
154 part of the source timestamp. */
156 if (use_ref)
158 utb.actime = ref_stats.st_atime;
159 utb.modtime = ref_stats.st_mtime;
161 else
162 utb.actime = utb.modtime = newtime;
164 if (!(change_times & CH_ATIME))
165 utb.actime = sbuf.st_atime;
167 if (!(change_times & CH_MTIME))
168 utb.modtime = sbuf.st_mtime;
170 status = utime (file, &utb);
173 if (status)
175 error (0, errno, "%s", file);
176 return 1;
179 return 0;
182 void
183 usage (int status)
185 if (status != 0)
186 fprintf (stderr, _("Try `%s --help' for more information.\n"),
187 program_name);
188 else
190 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
191 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
192 program_name);
193 printf (_("\
194 Update the access and modification times of each FILE to the current time.\n\
196 -a change only the access time\n\
197 -c, --no-create do not create any files\n\
198 -d, --date=STRING parse STRING and use it instead of current time\n\
199 -f (ignored)\n\
200 -m change only the modification time\n\
201 -r, --reference=FILE use this file's times instead of current time\n\
202 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
203 --time=WORD set time given by WORD: access atime use (same as -a)\n\
204 modify mtime (same as -m)\n\
205 --help display this help and exit\n\
206 --version output version information and exit\n\
208 Note that the three time-date formats recognized for the -d and -t options\n\
209 and for the obsolescent argument are all different.\n\
210 "));
211 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
212 close_stdout ();
214 exit (status);
218 main (int argc, char **argv)
220 int c;
221 int date_set = 0;
222 int err = 0;
224 program_name = argv[0];
225 setlocale (LC_ALL, "");
226 bindtextdomain (PACKAGE, LOCALEDIR);
227 textdomain (PACKAGE);
229 change_times = no_create = use_ref = posix_date = flexible_date = 0;
230 newtime = (time_t) -1;
232 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
234 switch (c)
236 case 0:
237 break;
239 case 'a':
240 change_times |= CH_ATIME;
241 break;
243 case 'c':
244 no_create++;
245 break;
247 case 'd':
248 flexible_date++;
249 newtime = get_date (optarg, NULL);
250 if (newtime == (time_t) -1)
251 error (1, 0, _("invalid date format `%s'"), optarg);
252 date_set++;
253 break;
255 case 'f':
256 break;
258 case 'm':
259 change_times |= CH_MTIME;
260 break;
262 case 'r':
263 use_ref++;
264 ref_file = optarg;
265 break;
267 case 't':
268 posix_date++;
269 newtime = posixtime (optarg,
270 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
271 if (newtime == (time_t) -1)
272 error (1, 0, _("invalid date format `%s'"), optarg);
273 date_set++;
274 break;
276 case CHAR_MAX + 1: /* --time */
277 change_times |= XARGMATCH ("--time", optarg,
278 time_args, time_masks);
279 break;
281 case_GETOPT_HELP_CHAR;
283 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
285 default:
286 usage (1);
290 if (change_times == 0)
291 change_times = CH_ATIME | CH_MTIME;
293 if ((use_ref && (posix_date || flexible_date))
294 || (posix_date && flexible_date))
296 error (0, 0, _("cannot specify times from more than one source"));
297 usage (1);
300 if (use_ref)
302 if (stat (ref_file, &ref_stats))
303 error (1, errno, "%s", ref_file);
304 date_set++;
307 if (!date_set && optind < argc && !STREQ (argv[optind - 1], "--"))
309 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
310 if (newtime != (time_t) -1)
312 optind++;
313 date_set++;
316 if (!date_set)
318 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
319 amtime_now = 1;
320 else
321 time (&newtime);
324 if (optind == argc)
326 error (0, 0, _("file arguments missing"));
327 usage (1);
330 for (; optind < argc; ++optind)
331 err += touch (argv[optind]);
333 exit (err != 0);