1 /* touch -- change modification and access times of files
2 Copyright (C) 1987, 1989, 1990, 1991 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 -a, --time={atime,access,use} Change access time only.
20 -c, --no-create Do not create files that do not exist.
21 -d, --date=TIME Specify time and date in various formats.
23 -m, --time={mtime,modify} Change modification time only.
24 -r, --file=FILE Use the time and date of reference file FILE.
25 -t TIME Specify time and date in the form
26 `MMDDhhmm[[CC]YY][.ss]'.
28 If no options are given, -am is the default, using the current time.
29 The -r, -t, and -d options are mutually exclusive. If a file does not
30 exist, create it unless -c is given.
32 Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
38 #include <sys/types.h>
41 #include "safe-stat.h"
58 #ifndef HAVE_UTIME_NULL
59 static int utime_now ();
62 /* Bitmasks for `change_times'. */
66 /* The name by which this program was run. */
69 /* Which timestamps to change. */
70 static int change_times;
72 /* (-c) If nonzero, don't create if not already there. */
75 /* (-d) If nonzero, date supplied on command line in get_date formats. */
76 static int flexible_date;
78 /* (-r) If nonzero, use times from a reference file. */
81 /* (-t) If nonzero, date supplied on command line in POSIX format. */
82 static int posix_date;
84 /* If nonzero, the only thing we have to do is change both the
85 modification and access time to the current time, so we don't
86 have to own the file, just be able to read and write 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 /* If non-zero, display usage information and exit. */
101 /* If non-zero, print the version on standard output and exit. */
102 static int show_version;
104 static struct option const longopts[] =
106 {"time", required_argument, 0, 130},
107 {"no-create", no_argument, 0, 'c'},
108 {"date", required_argument, 0, 'd'},
109 {"file", required_argument, 0, 'r'},
110 {"help", no_argument, &show_help, 1},
111 {"version", no_argument, &show_version, 1},
115 /* Valid arguments to the `--time' option. */
116 static char const* const time_args[] =
118 "atime", "access", "use", "mtime", "modify", 0
121 /* The bits in `change_times' that those arguments set. */
122 static int const time_masks[] =
124 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
136 program_name = argv[0];
137 change_times = no_create = use_ref = posix_date = flexible_date = 0;
138 newtime = (time_t) -1;
140 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, (int *) 0))
149 change_times |= CH_ATIME;
158 newtime = get_date (optarg, NULL);
159 if (newtime == (time_t) -1)
160 error (1, 0, "invalid date format `%s'", optarg);
168 change_times |= CH_MTIME;
178 newtime = posixtime (optarg);
179 if (newtime == (time_t) -1)
180 error (1, 0, "invalid date format `%s'", optarg);
185 i = argmatch (optarg, time_args);
188 invalid_arg ("time selector", optarg, i);
191 change_times |= time_masks[i];
201 printf ("%s\n", version_string);
208 if (change_times == 0)
209 change_times = CH_ATIME | CH_MTIME;
211 if ((use_ref && (posix_date || flexible_date))
212 || (posix_date && flexible_date))
214 error (0, 0, "cannot specify times from more than one source");
220 if (SAFE_STAT (ref_file, &ref_stats))
221 error (1, errno, "%s", ref_file);
225 if (!date_set && optind < argc && strcmp (argv[optind - 1], "--"))
227 newtime = posixtime (argv[optind]);
228 if (newtime != (time_t) -1)
236 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
244 error (0, 0, "file arguments missing");
248 for (; optind < argc; ++optind)
249 err += touch (argv[optind]);
254 /* Update the time of file FILE according to the options given.
255 Return 0 if successful, 1 if an error occurs. */
265 if (SAFE_STAT (file, &sbuf))
269 error (0, errno, "%s", file);
274 fd = creat (file, 0666);
277 error (0, errno, "%s", file);
284 error (0, errno, "%s", file);
287 return 0; /* We've done all we have to. */
289 if (fstat (fd, &sbuf))
291 error (0, errno, "%s", file);
297 error (0, errno, "%s", file);
304 #ifndef HAVE_UTIME_NULL
305 status = utime_now (file, sbuf.st_size);
307 /* Pass NULL to utime so it will not fail if we just have
308 write access to the file, but don't own it. */
309 status = utime (file, NULL);
318 utb.actime = ref_stats.st_atime;
319 utb.modtime = ref_stats.st_mtime;
322 utb.actime = utb.modtime = newtime;
324 if (!(change_times & CH_ATIME))
325 utb.actime = sbuf.st_atime;
327 if (!(change_times & CH_MTIME))
328 utb.modtime = sbuf.st_mtime;
330 status = utime (file, &utb);
335 error (0, errno, "%s", file);
342 #ifndef HAVE_UTIME_NULL
343 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
344 interpret it to set the access and modification times of FILE to
345 the current time. FILESIZE is the correct size of FILE, used to
346 make sure empty files are not lengthened to 1 byte.
347 Return 0 if successful, -1 if not. */
350 utime_now (file, filesize)
358 fd = open (file, O_RDWR, 0666);
360 || safe_read (fd, &c, sizeof (char)) < 0
361 || lseek (fd, (off_t) 0, SEEK_SET) < 0
362 || full_write (fd, &c, sizeof (char)) < 0
363 || ftruncate (fd, filesize) < 0
375 fprintf (stderr, "Try `%s --help' for more information.\n",
379 printf ("Usage: %s [OPTION]... FILE...\n", program_name);
382 -a change only the access time\n\
383 -c do not create any files\n\
384 -d, --date=STRING parse STRING and use it instead of current time\n\
386 -m change only the modification time\n\
387 -r, --file=REFERENCE use this file's times instead of current time\n\
388 -t STAMP use MMDDhhmm[[CC]YY][.ss] instead of current time\n\
389 --help display this help and exit\n\
390 --time=WORD access -a, atime -a, mtime -m, modify -m, use -a\n\
391 --version output version information and exit\n\
393 STAMP may be used without -t if none of -drt, nor --, are used.\n");