1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-2002 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>
33 #include "safe-read.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "touch"
39 "Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie, and Randy Smith"
45 /* Bitmasks for `change_times'. */
53 #if !defined O_NONBLOCK
54 # define O_NONBLOCK O_NDELAY
65 /* The name by which this program was run. */
68 /* Which timestamps to change. */
69 static int change_times;
71 /* (-c) If nonzero, don't create if not already there. */
74 /* (-d) If nonzero, date supplied on command line in get_date formats. */
75 static int flexible_date;
77 /* (-r) If nonzero, use times from a reference file. */
80 /* (-t) If nonzero, date supplied on command line in POSIX format. */
81 static int posix_date;
83 /* If nonzero, the only thing we have to do is change both the
84 modification and access time to the current time, so we don't
85 have to own the file, just be able to read and write it.
86 On some systems, we can do this if we own the file, even though
87 we have neither read nor write access to it. */
88 static int amtime_now;
90 /* New time to use when setting time. */
91 static time_t newtime;
93 /* File to use for -r. */
94 static char *ref_file;
96 /* Info about the reference file. */
97 static struct stat ref_stats;
99 /* For long options that have no equivalent short option, use a
100 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
103 TIME_OPTION = CHAR_MAX + 1
106 static struct option const longopts[] =
108 {"time", required_argument, 0, TIME_OPTION},
109 {"no-create", no_argument, 0, 'c'},
110 {"date", required_argument, 0, 'd'},
111 {"file", required_argument, 0, 'r'}, /* FIXME: phase out --file */
112 {"reference", required_argument, 0, 'r'},
113 {GETOPT_HELP_OPTION_DECL},
114 {GETOPT_VERSION_OPTION_DECL},
118 /* Valid arguments to the `--time' option. */
119 static char const* const time_args[] =
121 "atime", "access", "use", "mtime", "modify", 0
124 /* The bits in `change_times' that those arguments set. */
125 static int const time_masks[] =
127 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
130 /* Update the time of file FILE according to the options given.
131 Return 0 if successful, 1 if an error occurs. */
134 touch (const char *file)
143 /* Try to open FILE, creating it if necessary. */
144 fd = open (file, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
145 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
147 /* Don't save a copy of errno if it's EISDIR, since that would lead
148 touch to give a bogus diagnostic for e.g., `touch /' (assuming
149 we don't own / or have write access to it). On Solaris 5.6,
150 and probably other systems, it is EINVAL. On SunOS4, it's EPERM. */
151 if (fd == -1 && errno != EISDIR && errno != EINVAL && errno != EPERM)
157 /* We're setting only one of the time values. stat the target to get
158 the other one. If we have the file descriptor already, use fstat.
159 Otherwise, either we're in no-create mode (and hence didn't call open)
160 or FILE is inaccessible or a directory, so we have to use stat. */
161 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
164 error (0, open_errno, _("creating %s"), quote (file));
166 error (0, errno, _("failed to get attributes of %s"), quote (file));
172 if (fd != -1 && close (fd) < 0)
174 error (0, errno, _("creating %s"), quote (file));
180 /* Pass NULL to utime so it will not fail if we just have
181 write access to the file, but don't own it. */
182 status = utime (file, NULL);
188 /* There's currently no interface to set file timestamps with
189 better than 1-second resolution, so discard any fractional
190 part of the source timestamp. */
194 utb.actime = ref_stats.st_atime;
195 utb.modtime = ref_stats.st_mtime;
198 utb.actime = utb.modtime = newtime;
200 if (!(change_times & CH_ATIME))
201 utb.actime = sbuf.st_atime;
203 if (!(change_times & CH_MTIME))
204 utb.modtime = sbuf.st_mtime;
206 status = utime (file, &utb);
212 error (0, open_errno, _("creating %s"), quote (file));
214 error (0, errno, _("setting times of %s"), quote (file));
225 fprintf (stderr, _("Try `%s --help' for more information.\n"),
229 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
231 Update the access and modification times of each FILE to the current time.\n\
235 Mandatory arguments to long options are mandatory for short options too.\n\
238 -a change only the access time\n\
239 -c, --no-create do not create any files\n\
240 -d, --date=STRING parse STRING and use it instead of current time\n\
242 -m change only the modification time\n\
245 -r, --reference=FILE use this file's times instead of current time\n\
246 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
247 --time=WORD set time given by WORD: access atime use (same as -a)\n\
248 modify mtime (same as -m)\n\
250 fputs (HELP_OPTION_DESCRIPTION, stdout);
251 fputs (VERSION_OPTION_DESCRIPTION, stdout);
254 Note that the -d and -t options accept different time-date formats.\n\
256 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
262 main (int argc, char **argv)
268 program_name = argv[0];
269 setlocale (LC_ALL, "");
270 bindtextdomain (PACKAGE, LOCALEDIR);
271 textdomain (PACKAGE);
273 atexit (close_stdout);
275 change_times = no_create = use_ref = posix_date = flexible_date = 0;
276 newtime = (time_t) -1;
278 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
286 change_times |= CH_ATIME;
295 newtime = get_date (optarg, NULL);
296 if (newtime == (time_t) -1)
297 error (1, 0, _("invalid date format %s"), quote (optarg));
305 change_times |= CH_MTIME;
315 newtime = posixtime (optarg,
316 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
317 if (newtime == (time_t) -1)
318 error (1, 0, _("invalid date format %s"), quote (optarg));
322 case TIME_OPTION: /* --time */
323 change_times |= XARGMATCH ("--time", optarg,
324 time_args, time_masks);
327 case_GETOPT_HELP_CHAR;
329 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
336 if (change_times == 0)
337 change_times = CH_ATIME | CH_MTIME;
339 if ((use_ref && (posix_date || flexible_date))
340 || (posix_date && flexible_date))
342 error (0, 0, _("cannot specify times from more than one source"));
348 if (stat (ref_file, &ref_stats))
349 error (1, errno, _("failed to get attributes of %s"), quote (ref_file));
353 /* The obsolete `MMDDhhmm[YY]' form is valid IFF there are
354 two or more non-option arguments. */
355 if (!date_set && 2 <= argc - optind && !STREQ (argv[optind - 1], "--")
356 && posix2_version () < 200112)
358 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
359 if (newtime != (time_t) -1)
361 if (! getenv ("POSIXLY_CORRECT"))
363 struct tm const *tm = posixtm (argv[optind], PDS_TRAILING_YEAR);
365 _("warning: `touch %s' is obsolete; use `touch -t %04d%02d%02d%02d%02d.%02d'"),
367 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
368 tm->tm_hour, tm->tm_min, tm->tm_sec);
377 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
385 error (0, 0, _("file arguments missing"));
389 for (; optind < argc; ++optind)
390 err += touch (argv[optind]);