1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-2000 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"
43 /* Bitmasks for `change_times'. */
51 #if !defined O_NONBLOCK
52 # define O_NONBLOCK O_NDELAY
55 /* The name by which this program was run. */
58 /* Which timestamps to change. */
59 static int change_times;
61 /* (-c) If nonzero, don't create if not already there. */
64 /* (-d) If nonzero, date supplied on command line in get_date formats. */
65 static int flexible_date;
67 /* (-r) If nonzero, use times from a reference file. */
70 /* (-t) If nonzero, date supplied on command line in POSIX format. */
71 static int posix_date;
73 /* If nonzero, the only thing we have to do is change both the
74 modification and access time to the current time, so we don't
75 have to own the file, just be able to read and write it.
76 On some systems, we can do this if we own the file, even though
77 we have neither read nor write access to it. */
78 static int amtime_now;
80 /* New time to use when setting time. */
81 static time_t newtime;
83 /* File to use for -r. */
84 static char *ref_file;
86 /* Info about the reference file. */
87 static struct stat ref_stats;
89 /* For long options that have no equivalent short option, use a
90 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
93 TIME_OPTION = CHAR_MAX + 1
96 static struct option const longopts[] =
98 {"time", required_argument, 0, TIME_OPTION},
99 {"no-create", no_argument, 0, 'c'},
100 {"date", required_argument, 0, 'd'},
101 {"file", required_argument, 0, 'r'}, /* FIXME: phase out --file */
102 {"reference", required_argument, 0, 'r'},
103 {GETOPT_HELP_OPTION_DECL},
104 {GETOPT_VERSION_OPTION_DECL},
108 /* Valid arguments to the `--time' option. */
109 static char const* const time_args[] =
111 "atime", "access", "use", "mtime", "modify", 0
114 /* The bits in `change_times' that those arguments set. */
115 static int const time_masks[] =
117 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
120 /* Update the time of file FILE according to the options given.
121 Return 0 if successful, 1 if an error occurs. */
124 touch (const char *file)
133 /* Try to open FILE, creating it if necessary. */
134 fd = open (file, O_WRONLY | O_CREAT | O_NONBLOCK,
135 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
142 /* We're setting only one of the time values. stat the target to get
143 the other one. If we have the file descriptor already, use fstat.
144 Otherwise, either we're in no-create mode (and hence didn't call open)
145 or FILE is inaccessible or a directory, so we have to use stat. */
146 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
148 error (0, open_errno ? open_errno : errno, "%s", file);
154 if (fd != -1 && close (fd) < 0)
156 error (0, errno, "%s", file);
162 /* Pass NULL to utime so it will not fail if we just have
163 write access to the file, but don't own it. */
164 status = utime (file, NULL);
170 /* There's currently no interface to set file timestamps with
171 better than 1-second resolution, so discard any fractional
172 part of the source timestamp. */
176 utb.actime = ref_stats.st_atime;
177 utb.modtime = ref_stats.st_mtime;
180 utb.actime = utb.modtime = newtime;
182 if (!(change_times & CH_ATIME))
183 utb.actime = sbuf.st_atime;
185 if (!(change_times & CH_MTIME))
186 utb.modtime = sbuf.st_mtime;
188 status = utime (file, &utb);
193 error (0, open_errno ? open_errno : errno, "%s", file);
204 fprintf (stderr, _("Try `%s --help' for more information.\n"),
208 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
209 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
212 Update the access and modification times of each FILE to the current time.\n\
214 -a change only the access time\n\
215 -c, --no-create do not create any files\n\
216 -d, --date=STRING parse STRING and use it instead of current time\n\
218 -m change only the modification time\n\
219 -r, --reference=FILE use this file's times instead of current time\n\
220 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
221 --time=WORD set time given by WORD: access atime use (same as -a)\n\
222 modify mtime (same as -m)\n\
223 --help display this help and exit\n\
224 --version output version information and exit\n\
226 Note that the three time-date formats recognized for the -d and -t options\n\
227 and for the obsolescent argument are all different.\n\
229 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
236 main (int argc, char **argv)
242 program_name = argv[0];
243 setlocale (LC_ALL, "");
244 bindtextdomain (PACKAGE, LOCALEDIR);
245 textdomain (PACKAGE);
247 change_times = no_create = use_ref = posix_date = flexible_date = 0;
248 newtime = (time_t) -1;
250 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
258 change_times |= CH_ATIME;
267 newtime = get_date (optarg, NULL);
268 if (newtime == (time_t) -1)
269 error (1, 0, _("invalid date format `%s'"), optarg);
277 change_times |= CH_MTIME;
287 newtime = posixtime (optarg,
288 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
289 if (newtime == (time_t) -1)
290 error (1, 0, _("invalid date format `%s'"), optarg);
294 case TIME_OPTION: /* --time */
295 change_times |= XARGMATCH ("--time", optarg,
296 time_args, time_masks);
299 case_GETOPT_HELP_CHAR;
301 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
308 if (change_times == 0)
309 change_times = CH_ATIME | CH_MTIME;
311 if ((use_ref && (posix_date || flexible_date))
312 || (posix_date && flexible_date))
314 error (0, 0, _("cannot specify times from more than one source"));
320 if (stat (ref_file, &ref_stats))
321 error (1, errno, "%s", ref_file);
325 if (!date_set && optind < argc && !STREQ (argv[optind - 1], "--"))
327 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
328 if (newtime != (time_t) -1)
336 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
344 error (0, 0, _("file arguments missing"));
348 for (; optind < argc; ++optind)
349 err += touch (argv[optind]);