1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 89, 90, 91, 95, 1996 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. */
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, --reference=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>
54 #ifndef HAVE_UTIME_NULL
55 static int utime_now ();
58 /* Bitmasks for `change_times'. */
62 /* The name by which this program was run. */
65 /* Which timestamps to change. */
66 static int change_times;
68 /* (-c) If nonzero, don't create if not already there. */
71 /* (-d) If nonzero, date supplied on command line in get_date formats. */
72 static int flexible_date;
74 /* (-r) If nonzero, use times from a reference file. */
77 /* (-t) If nonzero, date supplied on command line in POSIX format. */
78 static int posix_date;
80 /* If nonzero, the only thing we have to do is change both the
81 modification and access time to the current time, so we don't
82 have to own the file, just be able to read and write it. */
83 static int amtime_now;
85 /* New time to use when setting time. */
86 static time_t newtime;
88 /* File to use for -r. */
89 static char *ref_file;
91 /* Info about the reference file. */
92 static struct stat ref_stats;
94 /* If nonzero, display usage information and exit. */
97 /* If nonzero, print the version on standard output and exit. */
98 static int show_version;
100 static struct option const longopts[] =
102 {"time", required_argument, 0, 130},
103 {"no-create", no_argument, 0, 'c'},
104 {"date", required_argument, 0, 'd'},
105 {"file", required_argument, 0, 'r'},
106 {"reference", required_argument, 0, 'r'},
107 {"help", no_argument, &show_help, 1},
108 {"version", no_argument, &show_version, 1},
112 /* Valid arguments to the `--time' option. */
113 static char const* const time_args[] =
115 "atime", "access", "use", "mtime", "modify", 0
118 /* The bits in `change_times' that those arguments set. */
119 static int const time_masks[] =
121 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
124 /* Update the time of file FILE according to the options given.
125 Return 0 if successful, 1 if an error occurs. */
134 if (stat (file, &sbuf))
138 error (0, errno, "%s", file);
143 fd = creat (file, 0666);
146 error (0, errno, "%s", file);
153 error (0, errno, "%s", file);
156 return 0; /* We've done all we have to. */
158 if (fstat (fd, &sbuf))
160 error (0, errno, "%s", file);
166 error (0, errno, "%s", file);
173 #ifndef HAVE_UTIME_NULL
174 status = utime_now (file, sbuf.st_size);
176 /* Pass NULL to utime so it will not fail if we just have
177 write access to the file, but don't own it. */
178 status = utime (file, NULL);
187 utb.actime = ref_stats.st_atime;
188 utb.modtime = ref_stats.st_mtime;
191 utb.actime = utb.modtime = newtime;
193 if (!(change_times & CH_ATIME))
194 utb.actime = sbuf.st_atime;
196 if (!(change_times & CH_MTIME))
197 utb.modtime = sbuf.st_mtime;
199 status = utime (file, &utb);
204 error (0, errno, "%s", file);
211 #ifndef HAVE_UTIME_NULL
212 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
213 interpret it to set the access and modification times of FILE to
214 the current time. FILESIZE is the correct size of FILE, used to
215 make sure empty files are not lengthened to 1 byte.
216 Return 0 if successful, -1 if not. */
219 utime_now (const char *file, off_t filesize)
225 fd = open (file, O_RDWR, 0666);
227 || safe_read (fd, &c, sizeof (char)) < 0
228 || lseek (fd, (off_t) 0, SEEK_SET) < 0
229 || full_write (fd, &c, sizeof (char)) < 0
230 || ftruncate (fd, filesize) < 0
241 fprintf (stderr, _("Try `%s --help' for more information.\n"),
245 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
247 Update the access and modification times of each FILE to the current time.\n\
249 -a change only the access time\n\
250 -c do not create any files\n\
251 -d, --date=STRING parse STRING and use it instead of current time\n\
253 -m change only the modification time\n\
254 -r, --reference=FILE use this file's times instead of current time\n\
255 -t STAMP use MMDDhhmm[[CC]YY][.ss] instead of current time\n\
256 --time=WORD access -a, atime -a, mtime -m, modify -m, use -a\n\
257 --help display this help and exit\n\
258 --version output version information and exit\n\
260 STAMP may be used without -t if none of -drt, nor --, are used.\n\
262 puts (_("\nReport bugs to <fileutils-bugs@gnu.ai.mit.edu>."));
268 main (int argc, char **argv)
274 program_name = argv[0];
275 setlocale (LC_ALL, "");
276 bindtextdomain (PACKAGE, LOCALEDIR);
277 textdomain (PACKAGE);
279 change_times = no_create = use_ref = posix_date = flexible_date = 0;
280 newtime = (time_t) -1;
282 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
290 change_times |= CH_ATIME;
299 newtime = get_date (optarg, NULL);
300 if (newtime == (time_t) -1)
301 error (1, 0, _("invalid date format `%s'"), optarg);
309 change_times |= CH_MTIME;
319 newtime = posixtime (optarg);
320 if (newtime == (time_t) -1)
321 error (1, 0, _("invalid date format `%s'"), optarg);
326 i = argmatch (optarg, time_args);
329 invalid_arg (_("time selector"), optarg, i);
332 change_times |= time_masks[i];
342 printf ("touch (%s) %s\n", GNU_PACKAGE, VERSION);
349 if (change_times == 0)
350 change_times = CH_ATIME | CH_MTIME;
352 if ((use_ref && (posix_date || flexible_date))
353 || (posix_date && flexible_date))
355 error (0, 0, _("cannot specify times from more than one source"));
361 if (stat (ref_file, &ref_stats))
362 error (1, errno, "%s", ref_file);
366 if (!date_set && optind < argc && strcmp (argv[optind - 1], "--"))
368 newtime = posixtime (argv[optind]);
369 if (newtime != (time_t) -1)
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]);