1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-2001 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>
32 #include "safe-read.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "touch"
38 "Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie, and Randy Smith"
44 /* Bitmasks for `change_times'. */
52 #if !defined O_NONBLOCK
53 # define O_NONBLOCK O_NDELAY
64 /* The name by which this program was run. */
67 /* Which timestamps to change. */
68 static int change_times;
70 /* (-c) If nonzero, don't create if not already there. */
73 /* (-d) If nonzero, date supplied on command line in get_date formats. */
74 static int flexible_date;
76 /* (-r) If nonzero, use times from a reference file. */
79 /* (-t) If nonzero, date supplied on command line in POSIX format. */
80 static int posix_date;
82 /* If nonzero, the only thing we have to do is change both the
83 modification and access time to the current time, so we don't
84 have to own the file, just be able to read and write it.
85 On some systems, we can do this if we own the file, even though
86 we have neither read nor write access to it. */
87 static int amtime_now;
89 /* New time to use when setting time. */
90 static time_t newtime;
92 /* File to use for -r. */
93 static char *ref_file;
95 /* Info about the reference file. */
96 static struct stat ref_stats;
98 /* For long options that have no equivalent short option, use a
99 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
102 TIME_OPTION = CHAR_MAX + 1
105 static struct option const longopts[] =
107 {"time", required_argument, 0, TIME_OPTION},
108 {"no-create", no_argument, 0, 'c'},
109 {"date", required_argument, 0, 'd'},
110 {"file", required_argument, 0, 'r'}, /* FIXME: phase out --file */
111 {"reference", required_argument, 0, 'r'},
112 {GETOPT_HELP_OPTION_DECL},
113 {GETOPT_VERSION_OPTION_DECL},
117 /* Valid arguments to the `--time' option. */
118 static char const* const time_args[] =
120 "atime", "access", "use", "mtime", "modify", 0
123 /* The bits in `change_times' that those arguments set. */
124 static int const time_masks[] =
126 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
129 /* Update the time of file FILE according to the options given.
130 Return 0 if successful, 1 if an error occurs. */
133 touch (const char *file)
142 /* Try to open FILE, creating it if necessary. */
143 fd = open (file, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
144 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
146 /* Don't save a copy of errno if it's EISDIR, since that would lead
147 touch to give a bogus diagnostic for e.g., `touch /' (assuming
148 we don't own / or have write access to it). On Solaris 5.6,
149 and probably other systems, it is EINVAL. */
150 if (fd == -1 && errno != EISDIR && errno != EINVAL)
156 /* We're setting only one of the time values. stat the target to get
157 the other one. If we have the file descriptor already, use fstat.
158 Otherwise, either we're in no-create mode (and hence didn't call open)
159 or FILE is inaccessible or a directory, so we have to use stat. */
160 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
163 error (0, open_errno, _("creating %s"), quote (file));
165 error (0, errno, _("getting attributes of %s"), quote (file));
171 if (fd != -1 && close (fd) < 0)
173 error (0, errno, _("creating %s"), quote (file));
179 /* Pass NULL to utime so it will not fail if we just have
180 write access to the file, but don't own it. */
181 status = utime (file, NULL);
187 /* There's currently no interface to set file timestamps with
188 better than 1-second resolution, so discard any fractional
189 part of the source timestamp. */
193 utb.actime = ref_stats.st_atime;
194 utb.modtime = ref_stats.st_mtime;
197 utb.actime = utb.modtime = newtime;
199 if (!(change_times & CH_ATIME))
200 utb.actime = sbuf.st_atime;
202 if (!(change_times & CH_MTIME))
203 utb.modtime = sbuf.st_mtime;
205 status = utime (file, &utb);
211 error (0, open_errno, _("creating %s"), quote (file));
213 error (0, errno, _("setting times of %s"), quote (file));
224 fprintf (stderr, _("Try `%s --help' for more information.\n"),
228 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
229 printf (_(" or: %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
232 Update the access and modification times of each FILE to the current time.\n\
234 -a change only the access time\n\
235 -c, --no-create do not create any files\n\
236 -d, --date=STRING parse STRING and use it instead of current time\n\
238 -m change only the modification time\n\
239 -r, --reference=FILE use this file's times instead of current time\n\
240 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
241 --time=WORD set time given by WORD: access atime use (same as -a)\n\
242 modify mtime (same as -m)\n\
243 --help display this help and exit\n\
244 --version output version information and exit\n\
246 Note that the three time-date formats recognized for the -d and -t options\n\
247 and for the obsolescent argument are all different.\n\
249 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
255 main (int argc, char **argv)
261 program_name = argv[0];
262 setlocale (LC_ALL, "");
263 bindtextdomain (PACKAGE, LOCALEDIR);
264 textdomain (PACKAGE);
266 atexit (close_stdout);
268 change_times = no_create = use_ref = posix_date = flexible_date = 0;
269 newtime = (time_t) -1;
271 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
279 change_times |= CH_ATIME;
288 newtime = get_date (optarg, NULL);
289 if (newtime == (time_t) -1)
290 error (1, 0, _("invalid date format %s"), quote (optarg));
298 change_times |= CH_MTIME;
308 newtime = posixtime (optarg,
309 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
310 if (newtime == (time_t) -1)
311 error (1, 0, _("invalid date format %s"), quote (optarg));
315 case TIME_OPTION: /* --time */
316 change_times |= XARGMATCH ("--time", optarg,
317 time_args, time_masks);
320 case_GETOPT_HELP_CHAR;
322 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
329 if (change_times == 0)
330 change_times = CH_ATIME | CH_MTIME;
332 if ((use_ref && (posix_date || flexible_date))
333 || (posix_date && flexible_date))
335 error (0, 0, _("cannot specify times from more than one source"));
341 if (stat (ref_file, &ref_stats))
342 error (1, errno, _("getting attributes of %s"), quote (ref_file));
346 /* The obsolescent `MMDDhhmm[YY]' form is valid IFF there are
347 two or more non-option arguments. */
348 if (!date_set && 2 <= argc - optind && !STREQ (argv[optind - 1], "--"))
350 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
351 if (newtime != (time_t) -1)
359 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
367 error (0, 0, _("file arguments missing"));
371 for (; optind < argc; ++optind)
372 err += touch (argv[optind]);