* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / touch.c
bloba79c26d97ec71c2e5cee35139ac7dad39fe9ae4c
1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-2005 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)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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>
26 #include "system.h"
27 #include "argmatch.h"
28 #include "error.h"
29 #include "fd-reopen.h"
30 #include "getdate.h"
31 #include "posixtm.h"
32 #include "posixver.h"
33 #include "quote.h"
34 #include "safe-read.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 "Paul Rubin", "Arnold Robbins, Jim Kingdon, David MacKenzie", "Randy Smith"
44 /* Bitmasks for `change_times'. */
45 #define CH_ATIME 1
46 #define CH_MTIME 2
48 /* The name by which this program was run. */
49 char *program_name;
51 /* Which timestamps to change. */
52 static int change_times;
54 /* (-c) If true, don't create if not already there. */
55 static bool no_create;
57 /* (-r) If true, use times from a reference file. */
58 static bool use_ref;
60 /* If true, the only thing we have to do is change both the
61 modification and access time to the current time, so we don't
62 have to own the file, just be able to read and write it.
63 On some systems, we can do this if we own the file, even though
64 we have neither read nor write access to it. */
65 static bool amtime_now;
67 /* New access and modification times to use when setting time. */
68 static struct timespec newtime[2];
70 /* File to use for -r. */
71 static char *ref_file;
73 /* For long options that have no equivalent short option, use a
74 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
75 enum
77 TIME_OPTION = CHAR_MAX + 1
80 static struct option const longopts[] =
82 {"time", required_argument, NULL, TIME_OPTION},
83 {"no-create", no_argument, NULL, 'c'},
84 {"date", required_argument, NULL, 'd'},
85 {"file", required_argument, NULL, 'r'}, /* FIXME: remove --file in 2006 */
86 {"reference", required_argument, NULL, 'r'},
87 {GETOPT_HELP_OPTION_DECL},
88 {GETOPT_VERSION_OPTION_DECL},
89 {NULL, 0, NULL, 0}
92 /* Valid arguments to the `--time' option. */
93 static char const* const time_args[] =
95 "atime", "access", "use", "mtime", "modify", NULL
98 /* The bits in `change_times' that those arguments set. */
99 static int const time_masks[] =
101 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
104 /* Store into *RESULT the result of interpreting FLEX_DATE as a date,
105 relative to NOW. If NOW is null, use the current time. */
107 static void
108 get_reldate (struct timespec *result,
109 char const *flex_date, struct timespec const *now)
111 if (! get_date (result, flex_date, now))
112 error (EXIT_FAILURE, 0, _("invalid date format %s"), quote (flex_date));
115 /* Update the time of file FILE according to the options given.
116 Return true if successful. */
118 static bool
119 touch (const char *file)
121 bool ok;
122 struct stat sbuf;
123 int fd = -1;
124 int open_errno = 0;
125 struct timespec timespec[2];
126 struct timespec const *t;
128 if (STREQ (file, "-"))
129 fd = STDOUT_FILENO;
130 else if (! no_create)
132 /* Try to open FILE, creating it if necessary. */
133 fd = fd_reopen (STDIN_FILENO, file,
134 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
135 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
137 /* Don't save a copy of errno if it's EISDIR, since that would lead
138 touch to give a bogus diagnostic for e.g., `touch /' (assuming
139 we don't own / or have write access to it). On Solaris 5.6,
140 and probably other systems, it is EINVAL. On SunOS4, it's EPERM. */
141 if (fd == -1 && errno != EISDIR && errno != EINVAL && errno != EPERM)
142 open_errno = errno;
145 if (change_times != (CH_ATIME | CH_MTIME))
147 /* We're setting only one of the time values. stat the target to get
148 the other one. If we have the file descriptor already, use fstat.
149 Otherwise, either we're in no-create mode (and hence didn't call open)
150 or FILE is inaccessible or a directory, so we have to use stat. */
151 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
153 if (open_errno)
154 error (0, open_errno, _("creating %s"), quote (file));
155 else
157 if (no_create && (errno == ENOENT || errno == EBADF))
158 return true;
159 error (0, errno, _("failed to get attributes of %s"),
160 quote (file));
162 if (fd == STDIN_FILENO)
163 close (fd);
164 return false;
168 if (amtime_now)
170 /* Pass NULL to futimens so it will not fail if we have
171 write access to the file, but don't own it. */
172 t = NULL;
174 else
176 timespec[0] = (change_times & CH_ATIME
177 ? newtime[0]
178 : get_stat_atime (&sbuf));
179 timespec[1] = (change_times & CH_MTIME
180 ? newtime[1]
181 : get_stat_mtime (&sbuf));
182 t = timespec;
185 ok = (futimens (fd, (fd == STDOUT_FILENO ? NULL : file), t) == 0);
187 if (fd == STDIN_FILENO)
189 if (close (STDIN_FILENO) != 0)
191 error (0, errno, _("closing %s"), quote (file));
192 return false;
195 else if (fd == STDOUT_FILENO)
197 /* Do not diagnose "touch -c - >&-". */
198 if (!ok && errno == EBADF && no_create
199 && change_times == (CH_ATIME | CH_MTIME))
200 return true;
203 if (!ok)
205 if (open_errno)
207 /* The wording of this diagnostic should cover at least two cases:
208 - the file does not exist, but the parent directory is unwritable
209 - the file exists, but it isn't writable
210 I think it's not worth trying to distinguish them. */
211 error (0, open_errno, _("cannot touch %s"), quote (file));
213 else
215 if (no_create && errno == ENOENT)
216 return true;
217 error (0, errno, _("setting times of %s"), quote (file));
219 return false;
222 return true;
225 void
226 usage (int status)
228 if (status != EXIT_SUCCESS)
229 fprintf (stderr, _("Try `%s --help' for more information.\n"),
230 program_name);
231 else
233 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
234 fputs (_("\
235 Update the access and modification times of each FILE to the current time.\n\
237 "), stdout);
238 fputs (_("\
239 Mandatory arguments to long options are mandatory for short options too.\n\
240 "), stdout);
241 fputs (_("\
242 -a change only the access time\n\
243 -c, --no-create do not create any files\n\
244 -d, --date=STRING parse STRING and use it instead of current time\n\
245 -f (ignored)\n\
246 -m change only the modification time\n\
247 "), stdout);
248 fputs (_("\
249 -r, --reference=FILE use this file's times instead of current time\n\
250 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
251 --time=WORD change the specified time:\n\
252 WORD is access, atime, or use: equivalent to -a\n\
253 WORD is modify or mtime: equivalent to -m\n\
254 "), stdout);
255 fputs (HELP_OPTION_DESCRIPTION, stdout);
256 fputs (VERSION_OPTION_DESCRIPTION, stdout);
257 fputs (_("\
259 Note that the -d and -t options accept different time-date formats.\n\
261 If a FILE is -, touch standard output.\n\
262 "), stdout);
263 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
265 exit (status);
269 main (int argc, char **argv)
271 int c;
272 bool date_set = false;
273 bool ok = true;
274 char const *flex_date = NULL;
276 initialize_main (&argc, &argv);
277 program_name = argv[0];
278 setlocale (LC_ALL, "");
279 bindtextdomain (PACKAGE, LOCALEDIR);
280 textdomain (PACKAGE);
282 atexit (close_stdout);
284 change_times = 0;
285 no_create = use_ref = false;
287 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
289 switch (c)
291 case 'a':
292 change_times |= CH_ATIME;
293 break;
295 case 'c':
296 no_create = true;
297 break;
299 case 'd':
300 flex_date = optarg;
301 break;
303 case 'f':
304 break;
306 case 'm':
307 change_times |= CH_MTIME;
308 break;
310 case 'r':
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 if (stat (ref_file, &ref_stats))
352 error (EXIT_FAILURE, errno,
353 _("failed to get attributes of %s"), quote (ref_file));
354 newtime[0] = get_stat_atime (&ref_stats);
355 newtime[1] = get_stat_mtime (&ref_stats);
356 date_set = true;
357 if (flex_date)
359 if (change_times & CH_ATIME)
360 get_reldate (&newtime[0], flex_date, &newtime[0]);
361 if (change_times & CH_MTIME)
362 get_reldate (&newtime[1], flex_date, &newtime[1]);
365 else
367 if (flex_date)
369 get_reldate (&newtime[0], flex_date, NULL);
370 newtime[1] = newtime[0];
371 date_set = true;
375 /* The obsolete `MMDDhhmm[YY]' form is valid IFF there are
376 two or more non-option arguments. */
377 if (!date_set && 2 <= argc - optind && posix2_version () < 200112
378 && posixtime (&newtime[0].tv_sec, argv[optind],
379 PDS_TRAILING_YEAR | PDS_PRE_2000))
381 newtime[0].tv_nsec = 0;
382 newtime[1] = newtime[0];
383 date_set = true;
385 if (! getenv ("POSIXLY_CORRECT"))
387 struct tm const *tm = localtime (&newtime[0].tv_sec);
388 error (0, 0,
389 _("warning: `touch %s' is obsolete; use "
390 "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
391 argv[optind],
392 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
393 tm->tm_hour, tm->tm_min, tm->tm_sec);
396 optind++;
399 if (!date_set)
401 if (change_times == (CH_ATIME | CH_MTIME))
402 amtime_now = true;
403 else
405 gettime (&newtime[0]);
406 newtime[1] = newtime[0];
410 if (optind == argc)
412 error (0, 0, _("missing file operand"));
413 usage (EXIT_FAILURE);
416 for (; optind < argc; ++optind)
417 ok &= touch (argv[optind]);
419 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);