Another bootstrap kludge.
[coreutils/ericb.git] / src / touch.c
blob2540558e55d21324aa0dba58f138198e6abc218d
1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-2005, 2007 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
18 and Randy Smith. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "argmatch.h"
27 #include "error.h"
28 #include "fd-reopen.h"
29 #include "getdate.h"
30 #include "posixtm.h"
31 #include "posixver.h"
32 #include "quote.h"
33 #include "safe-read.h"
34 #include "stat-time.h"
35 #include "utimens.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "touch"
40 #define AUTHORS \
41 "Paul Rubin", "Arnold Robbins, Jim Kingdon, David MacKenzie", "Randy Smith"
43 /* Bitmasks for `change_times'. */
44 #define CH_ATIME 1
45 #define CH_MTIME 2
47 /* The name by which this program was run. */
48 char *program_name;
50 /* Which timestamps to change. */
51 static int change_times;
53 /* (-c) If true, don't create if not already there. */
54 static bool no_create;
56 /* (-r) If true, use times from a reference file. */
57 static bool use_ref;
59 /* If true, the only thing we have to do is change both the
60 modification and access time to the current time, so we don't
61 have to own the file, just be able to read and write it.
62 On some systems, we can do this if we own the file, even though
63 we have neither read nor write access to it. */
64 static bool amtime_now;
66 /* New access and modification times to use when setting time. */
67 static struct timespec newtime[2];
69 /* File to use for -r. */
70 static char *ref_file;
72 /* For long options that have no equivalent short option, use a
73 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
74 enum
76 TIME_OPTION = CHAR_MAX + 1
79 static struct option const longopts[] =
81 {"time", required_argument, NULL, TIME_OPTION},
82 {"no-create", no_argument, NULL, 'c'},
83 {"date", required_argument, NULL, 'd'},
84 {"file", required_argument, NULL, 'r'}, /* FIXME: remove --file in 2006 */
85 {"reference", required_argument, NULL, 'r'},
86 {GETOPT_HELP_OPTION_DECL},
87 {GETOPT_VERSION_OPTION_DECL},
88 {NULL, 0, NULL, 0}
91 /* Valid arguments to the `--time' option. */
92 static char const* const time_args[] =
94 "atime", "access", "use", "mtime", "modify", NULL
97 /* The bits in `change_times' that those arguments set. */
98 static int const time_masks[] =
100 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
103 /* Store into *RESULT the result of interpreting FLEX_DATE as a date,
104 relative to NOW. If NOW is null, use the current time. */
106 static void
107 get_reldate (struct timespec *result,
108 char const *flex_date, struct timespec const *now)
110 if (! get_date (result, flex_date, now))
111 error (EXIT_FAILURE, 0, _("invalid date format %s"), quote (flex_date));
114 /* Update the time of file FILE according to the options given.
115 Return true if successful. */
117 static bool
118 touch (const char *file)
120 bool ok;
121 struct stat sbuf;
122 int fd = -1;
123 int open_errno = 0;
124 struct timespec timespec[2];
125 struct timespec const *t;
127 if (STREQ (file, "-"))
128 fd = STDOUT_FILENO;
129 else if (! no_create)
131 /* Try to open FILE, creating it if necessary. */
132 fd = fd_reopen (STDIN_FILENO, file,
133 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
134 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
136 /* Don't save a copy of errno if it's EISDIR, since that would lead
137 touch to give a bogus diagnostic for e.g., `touch /' (assuming
138 we don't own / or have write access to it). On Solaris 5.6,
139 and probably other systems, it is EINVAL. On SunOS4, it's EPERM. */
140 if (fd == -1 && errno != EISDIR && errno != EINVAL && errno != EPERM)
141 open_errno = errno;
144 if (change_times != (CH_ATIME | CH_MTIME))
146 /* We're setting only one of the time values. stat the target to get
147 the other one. If we have the file descriptor already, use fstat.
148 Otherwise, either we're in no-create mode (and hence didn't call open)
149 or FILE is inaccessible or a directory, so we have to use stat. */
150 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
152 if (open_errno)
153 error (0, open_errno, _("creating %s"), quote (file));
154 else
156 if (no_create && (errno == ENOENT || errno == EBADF))
157 return true;
158 error (0, errno, _("failed to get attributes of %s"),
159 quote (file));
161 if (fd == STDIN_FILENO)
162 close (fd);
163 return false;
167 if (amtime_now)
169 /* Pass NULL to futimens so it will not fail if we have
170 write access to the file, but don't own it. */
171 t = NULL;
173 else
175 timespec[0] = (change_times & CH_ATIME
176 ? newtime[0]
177 : get_stat_atime (&sbuf));
178 timespec[1] = (change_times & CH_MTIME
179 ? newtime[1]
180 : get_stat_mtime (&sbuf));
181 t = timespec;
184 ok = (gl_futimens (fd, (fd == STDOUT_FILENO ? NULL : file), t) == 0);
186 if (fd == STDIN_FILENO)
188 if (close (STDIN_FILENO) != 0)
190 error (0, errno, _("closing %s"), quote (file));
191 return false;
194 else if (fd == STDOUT_FILENO)
196 /* Do not diagnose "touch -c - >&-". */
197 if (!ok && errno == EBADF && no_create
198 && change_times == (CH_ATIME | CH_MTIME))
199 return true;
202 if (!ok)
204 if (open_errno)
206 /* The wording of this diagnostic should cover at least two cases:
207 - the file does not exist, but the parent directory is unwritable
208 - the file exists, but it isn't writable
209 I think it's not worth trying to distinguish them. */
210 error (0, open_errno, _("cannot touch %s"), quote (file));
212 else
214 if (no_create && errno == ENOENT)
215 return true;
216 error (0, errno, _("setting times of %s"), quote (file));
218 return false;
221 return true;
224 void
225 usage (int status)
227 if (status != EXIT_SUCCESS)
228 fprintf (stderr, _("Try `%s --help' for more information.\n"),
229 program_name);
230 else
232 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
233 fputs (_("\
234 Update the access and modification times of each FILE to the current time.\n\
236 A FILE argument that does not exist is created empty.\n\
238 A FILE argument string of - is handled specially and causes touch to\n\
239 change the times of the file associated with standard output.\n\
241 "), stdout);
242 fputs (_("\
243 Mandatory arguments to long options are mandatory for short options too.\n\
244 "), stdout);
245 fputs (_("\
246 -a change only the access time\n\
247 -c, --no-create do not create any files\n\
248 -d, --date=STRING parse STRING and use it instead of current time\n\
249 -f (ignored)\n\
250 -m change only the modification time\n\
251 "), stdout);
252 fputs (_("\
253 -r, --reference=FILE use this file's times instead of current time\n\
254 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
255 --time=WORD change the specified time:\n\
256 WORD is access, atime, or use: equivalent to -a\n\
257 WORD is modify or mtime: equivalent to -m\n\
258 "), stdout);
259 fputs (HELP_OPTION_DESCRIPTION, stdout);
260 fputs (VERSION_OPTION_DESCRIPTION, stdout);
261 fputs (_("\
263 Note that the -d and -t options accept different time-date formats.\n\
264 "), stdout);
265 emit_bug_reporting_address ();
267 exit (status);
271 main (int argc, char **argv)
273 int c;
274 bool date_set = false;
275 bool ok = true;
276 char const *flex_date = NULL;
278 initialize_main (&argc, &argv);
279 program_name = argv[0];
280 setlocale (LC_ALL, "");
281 bindtextdomain (PACKAGE, LOCALEDIR);
282 textdomain (PACKAGE);
284 atexit (close_stdout);
286 change_times = 0;
287 no_create = use_ref = false;
289 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
291 switch (c)
293 case 'a':
294 change_times |= CH_ATIME;
295 break;
297 case 'c':
298 no_create = true;
299 break;
301 case 'd':
302 flex_date = optarg;
303 break;
305 case 'f':
306 break;
308 case 'm':
309 change_times |= CH_MTIME;
310 break;
312 case 'r':
313 use_ref = true;
314 ref_file = optarg;
315 break;
317 case 't':
318 if (! posixtime (&newtime[0].tv_sec, optarg,
319 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS))
320 error (EXIT_FAILURE, 0, _("invalid date format %s"),
321 quote (optarg));
322 newtime[0].tv_nsec = 0;
323 newtime[1] = newtime[0];
324 date_set = true;
325 break;
327 case TIME_OPTION: /* --time */
328 change_times |= XARGMATCH ("--time", optarg,
329 time_args, time_masks);
330 break;
332 case_GETOPT_HELP_CHAR;
334 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
336 default:
337 usage (EXIT_FAILURE);
341 if (change_times == 0)
342 change_times = CH_ATIME | CH_MTIME;
344 if (date_set && (use_ref || flex_date))
346 error (0, 0, _("cannot specify times from more than one source"));
347 usage (EXIT_FAILURE);
350 if (use_ref)
352 struct stat ref_stats;
353 if (stat (ref_file, &ref_stats))
354 error (EXIT_FAILURE, errno,
355 _("failed to get attributes of %s"), quote (ref_file));
356 newtime[0] = get_stat_atime (&ref_stats);
357 newtime[1] = get_stat_mtime (&ref_stats);
358 date_set = true;
359 if (flex_date)
361 if (change_times & CH_ATIME)
362 get_reldate (&newtime[0], flex_date, &newtime[0]);
363 if (change_times & CH_MTIME)
364 get_reldate (&newtime[1], flex_date, &newtime[1]);
367 else
369 if (flex_date)
371 get_reldate (&newtime[0], flex_date, NULL);
372 newtime[1] = newtime[0];
373 date_set = true;
377 /* The obsolete `MMDDhhmm[YY]' form is valid IFF there are
378 two or more non-option arguments. */
379 if (!date_set && 2 <= argc - optind && posix2_version () < 200112
380 && posixtime (&newtime[0].tv_sec, argv[optind],
381 PDS_TRAILING_YEAR | PDS_PRE_2000))
383 newtime[0].tv_nsec = 0;
384 newtime[1] = newtime[0];
385 date_set = true;
387 if (! getenv ("POSIXLY_CORRECT"))
389 struct tm const *tm = localtime (&newtime[0].tv_sec);
390 error (0, 0,
391 _("warning: `touch %s' is obsolete; use "
392 "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
393 argv[optind],
394 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
395 tm->tm_hour, tm->tm_min, tm->tm_sec);
398 optind++;
401 if (!date_set)
403 if (change_times == (CH_ATIME | CH_MTIME))
404 amtime_now = true;
405 else
407 gettime (&newtime[0]);
408 newtime[1] = newtime[0];
412 if (optind == argc)
414 error (0, 0, _("missing file operand"));
415 usage (EXIT_FAILURE);
418 for (; optind < argc; ++optind)
419 ok &= touch (argv[optind]);
421 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);