tests: unpack xz-compressed tarballs when possible, not always *.gz
[coreutils.git] / src / touch.c
blob11d73ce6c42f3b8fec87b250ef855cc2fb9b44eb
1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-2005, 2007-2009
3 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
19 and Randy Smith. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <assert.h>
27 #include "system.h"
28 #include "argmatch.h"
29 #include "error.h"
30 #include "fd-reopen.h"
31 #include "getdate.h"
32 #include "posixtm.h"
33 #include "posixver.h"
34 #include "quote.h"
35 #include "stat-time.h"
36 #include "utimens.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "touch"
41 #define AUTHORS \
42 proper_name ("Paul Rubin"), \
43 proper_name ("Arnold Robbins"), \
44 proper_name ("Jim Kingdon"), \
45 proper_name ("David MacKenzie"), \
46 proper_name ("Randy Smith")
48 /* Bitmasks for `change_times'. */
49 #define CH_ATIME 1
50 #define CH_MTIME 2
52 /* Which timestamps to change. */
53 static int change_times;
55 /* (-c) If true, don't create if not already there. */
56 static bool no_create;
58 /* (-r) If true, use times from a reference file. */
59 static bool use_ref;
61 /* (-h) If true, change the times of an existing symlink, if possible. */
62 static bool no_dereference;
64 /* If true, the only thing we have to do is change both the
65 modification and access time to the current time, so we don't
66 have to own the file, just be able to read and write it.
67 On some systems, we can do this if we own the file, even though
68 we have neither read nor write access to it. */
69 static bool amtime_now;
71 /* New access and modification times to use when setting time. */
72 static struct timespec newtime[2];
74 /* File to use for -r. */
75 static char *ref_file;
77 /* For long options that have no equivalent short option, use a
78 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
79 enum
81 TIME_OPTION = CHAR_MAX + 1
84 static struct option const longopts[] =
86 {"time", required_argument, NULL, TIME_OPTION},
87 {"no-create", no_argument, NULL, 'c'},
88 {"date", required_argument, NULL, 'd'},
89 {"file", required_argument, NULL, 'r'}, /* FIXME: remove --file in 2010 */
90 {"reference", required_argument, NULL, 'r'},
91 {"no-dereference", no_argument, NULL, 'h'},
92 {GETOPT_HELP_OPTION_DECL},
93 {GETOPT_VERSION_OPTION_DECL},
94 {NULL, 0, NULL, 0}
97 /* Valid arguments to the `--time' option. */
98 static char const* const time_args[] =
100 "atime", "access", "use", "mtime", "modify", NULL
103 /* The bits in `change_times' that those arguments set. */
104 static int const time_masks[] =
106 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
109 /* Store into *RESULT the result of interpreting FLEX_DATE as a date,
110 relative to NOW. If NOW is null, use the current time. */
112 static void
113 get_reldate (struct timespec *result,
114 char const *flex_date, struct timespec const *now)
116 if (! get_date (result, flex_date, now))
117 error (EXIT_FAILURE, 0, _("invalid date format %s"), quote (flex_date));
120 /* Update the time of file FILE according to the options given.
121 Return true if successful. */
123 static bool
124 touch (const char *file)
126 bool ok;
127 int fd = -1;
128 int open_errno = 0;
129 struct timespec const *t = newtime;
131 if (STREQ (file, "-"))
132 fd = STDOUT_FILENO;
133 else if (! (no_create || no_dereference))
135 /* Try to open FILE, creating it if necessary. */
136 fd = fd_reopen (STDIN_FILENO, file,
137 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
138 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
140 /* Don't save a copy of errno if it's EISDIR, since that would lead
141 touch to give a bogus diagnostic for e.g., `touch /' (assuming
142 we don't own / or have write access to it). On Solaris 5.6,
143 and probably other systems, it is EINVAL. On SunOS4, it's EPERM. */
144 if (fd == -1 && errno != EISDIR && errno != EINVAL && errno != EPERM)
145 open_errno = errno;
148 if (change_times != (CH_ATIME | CH_MTIME))
150 /* We're setting only one of the time values. */
151 if (change_times == CH_MTIME)
152 newtime[0].tv_nsec = UTIME_OMIT;
153 else
155 assert (change_times == CH_ATIME);
156 newtime[1].tv_nsec = UTIME_OMIT;
160 if (amtime_now)
162 /* Pass NULL to futimens so it will not fail if we have
163 write access to the file, but don't own it. */
164 t = NULL;
167 ok = ((no_dereference && fd == -1) ? lutimens (file, t)
168 : gl_futimens (fd, (fd == STDOUT_FILENO ? NULL : file), t)) == 0;
170 if (fd == STDIN_FILENO)
172 if (close (STDIN_FILENO) != 0)
174 error (0, errno, _("closing %s"), quote (file));
175 return false;
178 else if (fd == STDOUT_FILENO)
180 /* Do not diagnose "touch -c - >&-". */
181 if (!ok && errno == EBADF && no_create)
182 return true;
185 if (!ok)
187 if (open_errno)
189 /* The wording of this diagnostic should cover at least two cases:
190 - the file does not exist, but the parent directory is unwritable
191 - the file exists, but it isn't writable
192 I think it's not worth trying to distinguish them. */
193 error (0, open_errno, _("cannot touch %s"), quote (file));
195 else
197 if (no_create && errno == ENOENT)
198 return true;
199 error (0, errno, _("setting times of %s"), quote (file));
201 return false;
204 return true;
207 void
208 usage (int status)
210 if (status != EXIT_SUCCESS)
211 fprintf (stderr, _("Try `%s --help' for more information.\n"),
212 program_name);
213 else
215 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
216 fputs (_("\
217 Update the access and modification times of each FILE to the current time.\n\
219 A FILE argument that does not exist is created empty, unless -c or -h\n\
220 is supplied.\n\
222 A FILE argument string of - is handled specially and causes touch to\n\
223 change the times of the file associated with standard output.\n\
225 "), stdout);
226 fputs (_("\
227 Mandatory arguments to long options are mandatory for short options too.\n\
228 "), stdout);
229 fputs (_("\
230 -a change only the access time\n\
231 -c, --no-create do not create any files\n\
232 -d, --date=STRING parse STRING and use it instead of current time\n\
233 -f (ignored)\n\
234 "), stdout);
235 fputs (_("\
236 -h, --no-dereference affect each symbolic link instead of any referenced\n\
237 file (useful only on systems that can change the\n\
238 timestamps of a symlink)\n\
239 -m change only the modification time\n\
240 "), stdout);
241 fputs (_("\
242 -r, --reference=FILE use this file's times instead of current time\n\
243 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
244 --time=WORD change the specified time:\n\
245 WORD is access, atime, or use: equivalent to -a\n\
246 WORD is modify or mtime: equivalent to -m\n\
247 "), stdout);
248 fputs (HELP_OPTION_DESCRIPTION, stdout);
249 fputs (VERSION_OPTION_DESCRIPTION, stdout);
250 fputs (_("\
252 Note that the -d and -t options accept different time-date formats.\n\
253 "), stdout);
254 emit_ancillary_info ();
256 exit (status);
260 main (int argc, char **argv)
262 int c;
263 bool date_set = false;
264 bool ok = true;
265 char const *flex_date = NULL;
266 int long_idx; /* FIXME: remove in 2010, when --file is removed */
268 initialize_main (&argc, &argv);
269 set_program_name (argv[0]);
270 setlocale (LC_ALL, "");
271 bindtextdomain (PACKAGE, LOCALEDIR);
272 textdomain (PACKAGE);
274 atexit (close_stdout);
276 change_times = 0;
277 no_create = use_ref = false;
279 while ((c = getopt_long (argc, argv, "acd:fhmr:t:", longopts, &long_idx)) != -1)
281 switch (c)
283 case 'a':
284 change_times |= CH_ATIME;
285 break;
287 case 'c':
288 no_create = true;
289 break;
291 case 'd':
292 flex_date = optarg;
293 break;
295 case 'f':
296 break;
298 case 'h':
299 no_dereference = true;
300 break;
302 case 'm':
303 change_times |= CH_MTIME;
304 break;
306 case 'r':
307 if (long_idx == 3)
308 error (0, 0,
309 _("warning: the --%s option is obsolete; use --reference"),
310 longopts[long_idx].name);
311 use_ref = true;
312 ref_file = optarg;
313 break;
315 case 't':
316 if (! posixtime (&newtime[0].tv_sec, optarg,
317 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS))
318 error (EXIT_FAILURE, 0, _("invalid date format %s"),
319 quote (optarg));
320 newtime[0].tv_nsec = 0;
321 newtime[1] = newtime[0];
322 date_set = true;
323 break;
325 case TIME_OPTION: /* --time */
326 change_times |= XARGMATCH ("--time", optarg,
327 time_args, time_masks);
328 break;
330 case_GETOPT_HELP_CHAR;
332 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
334 default:
335 usage (EXIT_FAILURE);
339 if (change_times == 0)
340 change_times = CH_ATIME | CH_MTIME;
342 if (date_set && (use_ref || flex_date))
344 error (0, 0, _("cannot specify times from more than one source"));
345 usage (EXIT_FAILURE);
348 if (use_ref)
350 struct stat ref_stats;
351 /* Don't use (no_dereference?lstat:stat) (args), since stat
352 might be an object-like macro. */
353 if (no_dereference ? lstat (ref_file, &ref_stats)
354 : stat (ref_file, &ref_stats))
355 error (EXIT_FAILURE, errno,
356 _("failed to get attributes of %s"), quote (ref_file));
357 newtime[0] = get_stat_atime (&ref_stats);
358 newtime[1] = get_stat_mtime (&ref_stats);
359 date_set = true;
360 if (flex_date)
362 if (change_times & CH_ATIME)
363 get_reldate (&newtime[0], flex_date, &newtime[0]);
364 if (change_times & CH_MTIME)
365 get_reldate (&newtime[1], flex_date, &newtime[1]);
368 else
370 if (flex_date)
372 struct timespec now;
373 gettime (&now);
374 get_reldate (&newtime[0], flex_date, &now);
375 newtime[1] = newtime[0];
376 date_set = true;
378 /* If neither -a nor -m is specified, treat "-d now" as if
379 it were absent; this lets "touch" succeed more often in
380 the presence of restrictive permissions. */
381 if (change_times == (CH_ATIME | CH_MTIME)
382 && newtime[0].tv_sec == now.tv_sec
383 && newtime[0].tv_nsec == now.tv_nsec)
385 /* Check that it really was "-d now", and not a time
386 stamp that just happens to be the current time. */
387 struct timespec notnow, notnow1;
388 notnow.tv_sec = now.tv_sec ^ 1;
389 notnow.tv_nsec = now.tv_nsec;
390 get_reldate (&notnow1, flex_date, &notnow);
391 if (notnow1.tv_sec == notnow.tv_sec
392 && notnow1.tv_nsec == notnow.tv_nsec)
393 date_set = false;
398 /* The obsolete `MMDDhhmm[YY]' form is valid IFF there are
399 two or more non-option arguments. */
400 if (!date_set && 2 <= argc - optind && posix2_version () < 200112
401 && posixtime (&newtime[0].tv_sec, argv[optind],
402 PDS_TRAILING_YEAR | PDS_PRE_2000))
404 newtime[0].tv_nsec = 0;
405 newtime[1] = newtime[0];
406 date_set = true;
408 if (! getenv ("POSIXLY_CORRECT"))
410 struct tm const *tm = localtime (&newtime[0].tv_sec);
411 error (0, 0,
412 _("warning: `touch %s' is obsolete; use "
413 "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
414 argv[optind],
415 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
416 tm->tm_hour, tm->tm_min, tm->tm_sec);
419 optind++;
422 if (!date_set)
424 if (change_times == (CH_ATIME | CH_MTIME))
425 amtime_now = true;
426 else
427 newtime[1].tv_nsec = newtime[0].tv_nsec = UTIME_NOW;
430 if (optind == argc)
432 error (0, 0, _("missing file operand"));
433 usage (EXIT_FAILURE);
436 for (; optind < argc; ++optind)
437 ok &= touch (argv[optind]);
439 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);