*** empty log message ***
[coreutils.git] / src / touch.c
blobc3f76917a6aaeaee1fce26bb25253b98e7800d99
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 int full_write ();
45 /* Bitmasks for `change_times'. */
46 #define CH_ATIME 1
47 #define CH_MTIME 2
49 /* The name by which this program was run. */
50 char *program_name;
52 /* Which timestamps to change. */
53 static int change_times;
55 /* (-c) If nonzero, don't create if not already there. */
56 static int no_create;
58 /* (-d) If nonzero, date supplied on command line in get_date formats. */
59 static int flexible_date;
61 /* (-r) If nonzero, use times from a reference file. */
62 static int use_ref;
64 /* (-t) If nonzero, date supplied on command line in POSIX format. */
65 static int posix_date;
67 /* If nonzero, the only thing we have to do is change both the
68 modification and access time to the current time, so we don't
69 have to own the file, just be able to read and write 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 /* Open FILE, possibly creating it. Set *FILE_CREATED to nonzero if the
106 open creates it, or to zero if the open call opened an existing file.
107 Return the result of the open call. Be careful to avoid race conditions. */
109 static int
110 open_maybe_create (const char *file, int *file_created)
112 int fd;
114 *file_created = 0;
115 while (1)
117 /* First, see if we can create a new FILE. */
118 fd = open (file, O_WRONLY | O_CREAT | O_EXCL,
119 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
120 if (fd != -1)
121 *file_created = 1;
123 /* If the open succeeded or if it failed for any reason other
124 than the existence of FILE, then we're done. Some systems
125 (solaris) set errno to EINVAL when FILE is a directory. */
126 if (fd != -1 || (errno != EEXIST && errno != EINVAL))
127 break;
129 /* The first open failed because FILE already exists.
130 Now try to open it for writing. */
131 fd = open (file, O_WRONLY);
133 /* If the open succeeded or if it failed for any reason other
134 than the absence of FILE, then we're done. */
135 /* The 2nd open can fail if FILE was unlinked between the two
136 open calls. When that happens, just iterate. */
137 if (fd != -1 || errno != ENOENT)
138 break;
141 return fd;
144 /* Update the time of file FILE according to the options given.
145 Return 0 if successful, 1 if an error occurs. */
147 static int
148 touch (const char *file)
150 int status;
151 struct stat sbuf;
152 int fd;
153 int file_created;
155 if (no_create)
157 /* Try to open an existing FILE. */
158 fd = open (file, O_WRONLY);
159 if (fd == -1 && errno == ENOENT)
161 /* FILE doesn't exist. So we're done. */
162 return 0;
164 file_created = 0;
166 else
168 /* Try to open FILE, creating it if necessary. */
169 fd = open_maybe_create (file, &file_created);
172 /* Don't fail if the open failed because FILE is a directory. */
173 if (fd == -1
174 /* As usual, using E* macros like this is risky...
175 So heads up. */
176 && errno != EISDIR)
178 error (0, errno, "%s", file);
179 return 1;
182 if (amtime_now)
184 if (file_created)
186 if (close (fd) < 0)
188 error (0, errno, "%s", file);
189 return 1;
192 /* We've just created the file with the current times. */
193 return 0;
196 else
198 /* We're setting only one of the time values. stat the target to get
199 the other one. If we have the file descriptor already, use fstat,
200 otherwise, FILE is a directory, so we have to use stat. */
201 if (fd == -1 ? stat (file, &sbuf) : fstat (fd, &sbuf))
203 error (0, errno, "%s", file);
204 close (fd);
205 return 1;
209 if (fd != -1 && close (fd) < 0)
211 error (0, errno, "%s", file);
212 return 1;
215 if (amtime_now)
217 /* Pass NULL to utime so it will not fail if we just have
218 write access to the file, but don't own it. */
219 status = utime (file, NULL);
221 else
223 struct utimbuf utb;
225 /* There's currently no interface to set file timestamps with
226 better than 1-second resolution, so discard any fractional
227 part of the source timestamp. */
229 if (use_ref)
231 utb.actime = ref_stats.st_atime;
232 utb.modtime = ref_stats.st_mtime;
234 else
235 utb.actime = utb.modtime = newtime;
237 if (!(change_times & CH_ATIME))
238 utb.actime = sbuf.st_atime;
240 if (!(change_times & CH_MTIME))
241 utb.modtime = sbuf.st_mtime;
243 status = utime (file, &utb);
246 if (status)
248 error (0, errno, "%s", file);
249 return 1;
252 return 0;
255 void
256 usage (int status)
258 if (status != 0)
259 fprintf (stderr, _("Try `%s --help' for more information.\n"),
260 program_name);
261 else
263 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
264 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
265 program_name);
266 printf (_("\
267 Update the access and modification times of each FILE to the current time.\n\
269 -a change only the access time\n\
270 -c, --no-create do not create any files\n\
271 -d, --date=STRING parse STRING and use it instead of current time\n\
272 -f (ignored)\n\
273 -m change only the modification time\n\
274 -r, --reference=FILE use this file's times instead of current time\n\
275 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
276 --time=WORD access -a, atime -a, mtime -m, modify -m, use -a\n\
277 --help display this help and exit\n\
278 --version output version information and exit\n\
280 STAMP may be used without -t if none of -drt, nor --, are used.\n\
281 Note that the three time-date formats recognized for the -d and -t options\n\
282 and for the obsolescent argument are all different.\n\
283 "));
284 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
285 close_stdout ();
287 exit (status);
291 main (int argc, char **argv)
293 int c;
294 int date_set = 0;
295 int err = 0;
297 program_name = argv[0];
298 setlocale (LC_ALL, "");
299 bindtextdomain (PACKAGE, LOCALEDIR);
300 textdomain (PACKAGE);
302 change_times = no_create = use_ref = posix_date = flexible_date = 0;
303 newtime = (time_t) -1;
305 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
307 switch (c)
309 case 0:
310 break;
312 case 'a':
313 change_times |= CH_ATIME;
314 break;
316 case 'c':
317 no_create++;
318 break;
320 case 'd':
321 flexible_date++;
322 newtime = get_date (optarg, NULL);
323 if (newtime == (time_t) -1)
324 error (1, 0, _("invalid date format `%s'"), optarg);
325 date_set++;
326 break;
328 case 'f':
329 break;
331 case 'm':
332 change_times |= CH_MTIME;
333 break;
335 case 'r':
336 use_ref++;
337 ref_file = optarg;
338 break;
340 case 't':
341 posix_date++;
342 newtime = posixtime (optarg,
343 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
344 if (newtime == (time_t) -1)
345 error (1, 0, _("invalid date format `%s'"), optarg);
346 date_set++;
347 break;
349 case CHAR_MAX + 1: /* --time */
350 change_times |= XARGMATCH ("--time", optarg,
351 time_args, time_masks);
352 break;
354 case_GETOPT_HELP_CHAR;
356 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
358 default:
359 usage (1);
363 if (change_times == 0)
364 change_times = CH_ATIME | CH_MTIME;
366 if ((use_ref && (posix_date || flexible_date))
367 || (posix_date && flexible_date))
369 error (0, 0, _("cannot specify times from more than one source"));
370 usage (1);
373 if (use_ref)
375 if (stat (ref_file, &ref_stats))
376 error (1, errno, "%s", ref_file);
377 date_set++;
380 if (!date_set && optind < argc && !STREQ (argv[optind - 1], "--"))
382 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
383 if (newtime != (time_t) -1)
385 optind++;
386 date_set++;
389 if (!date_set)
391 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
392 amtime_now = 1;
393 else
394 time (&newtime);
397 if (optind == argc)
399 error (0, 0, _("file arguments missing"));
400 usage (1);
403 for (; optind < argc; ++optind)
404 err += touch (argv[optind]);
406 exit (err != 0);