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)
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,
24 #include <sys/types.h>
31 #include "safe-read.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "touch"
37 "Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie, and Randy Smith"
45 /* Bitmasks for `change_times'. */
49 /* The name by which this program was run. */
52 /* Which timestamps to change. */
53 static int change_times;
55 /* (-c) If nonzero, don't create if not already there. */
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. */
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'},
87 {"reference", required_argument, 0, 'r'},
88 {GETOPT_HELP_OPTION_DECL},
89 {GETOPT_VERSION_OPTION_DECL},
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. */
110 open_maybe_create (const char *file, int *file_created)
117 /* First, see if we can create a new FILE. */
118 fd = open (file, O_WRONLY | O_CREAT | O_EXCL, 0666);
122 /* If the open succeeded or if it failed for any reason other
123 than the existence of FILE, then we're done. */
124 if (fd != -1 || errno != EEXIST)
127 /* The first open failed because FILE already exists.
128 Now try to open it for writing. */
129 fd = open (file, O_WRONLY);
131 /* If the open succeeded or if it failed for any reason other
132 than the absence of FILE, then we're done. */
133 /* The 2nd open can fail if FILE was unlinked between the two
134 open calls. When that happens, just iterate. */
135 if (fd != -1 || errno != ENOENT)
142 /* Update the time of file FILE according to the options given.
143 Return 0 if successful, 1 if an error occurs. */
146 touch (const char *file)
155 /* Try to open an existing FILE. */
156 fd = open (file, O_WRONLY);
157 if (fd == -1 && errno == ENOENT)
159 /* FILE doesn't exist. So we're done. */
166 /* Try to open FILE, creating it if necessary. */
167 fd = open_maybe_create (file, &file_created);
172 error (0, errno, "%s", file);
176 if (file_created && amtime_now)
180 error (0, errno, "%s", file);
183 return 0; /* We've done all we have to. */
186 if (fstat (fd, &sbuf))
188 error (0, errno, "%s", file);
195 error (0, errno, "%s", file);
201 /* Pass NULL to utime so it will not fail if we just have
202 write access to the file, but don't own it. */
203 status = utime (file, NULL);
209 /* There's currently no interface to set file timestamps with
210 better than 1-second resolution, so discard any fractional
211 part of the source timestamp. */
215 utb.actime = ref_stats.st_atime;
216 utb.modtime = ref_stats.st_mtime;
219 utb.actime = utb.modtime = newtime;
221 if (!(change_times & CH_ATIME))
222 utb.actime = sbuf.st_atime;
224 if (!(change_times & CH_MTIME))
225 utb.modtime = sbuf.st_mtime;
227 status = utime (file, &utb);
232 error (0, errno, "%s", file);
243 fprintf (stderr, _("Try `%s --help' for more information.\n"),
247 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
248 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
251 Update the access and modification times of each FILE to the current time.\n\
253 -a change only the access time\n\
254 -c do not create any files\n\
255 -d, --date=STRING parse STRING and use it instead of current time\n\
257 -m change only the modification time\n\
258 -r, --reference=FILE use this file's times instead of current time\n\
259 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
260 --time=WORD access -a, atime -a, mtime -m, modify -m, use -a\n\
261 --help display this help and exit\n\
262 --version output version information and exit\n\
264 STAMP may be used without -t if none of -drt, nor --, are used.\n\
265 Note that the three time-date formats recognized for the -d and -t options\n\
266 and for the obsolescent argument are all different.\n\
268 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
275 main (int argc, char **argv)
281 program_name = argv[0];
282 setlocale (LC_ALL, "");
283 bindtextdomain (PACKAGE, LOCALEDIR);
284 textdomain (PACKAGE);
286 change_times = no_create = use_ref = posix_date = flexible_date = 0;
287 newtime = (time_t) -1;
289 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
297 change_times |= CH_ATIME;
306 newtime = get_date (optarg, NULL);
307 if (newtime == (time_t) -1)
308 error (1, 0, _("invalid date format `%s'"), optarg);
316 change_times |= CH_MTIME;
326 newtime = posixtime (optarg,
327 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
328 if (newtime == (time_t) -1)
329 error (1, 0, _("invalid date format `%s'"), optarg);
333 case CHAR_MAX + 1: /* --time */
334 change_times |= XARGMATCH ("--time", optarg,
335 time_args, time_masks);
338 case_GETOPT_HELP_CHAR;
340 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
347 if (change_times == 0)
348 change_times = CH_ATIME | CH_MTIME;
350 if ((use_ref && (posix_date || flexible_date))
351 || (posix_date && flexible_date))
353 error (0, 0, _("cannot specify times from more than one source"));
359 if (stat (ref_file, &ref_stats))
360 error (1, errno, "%s", ref_file);
364 if (!date_set && optind < argc && !STREQ (argv[optind - 1], "--"))
366 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
367 if (newtime != (time_t) -1)
375 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
383 error (0, 0, _("file arguments missing"));
387 for (; optind < argc; ++optind)
388 err += touch (argv[optind]);