*** empty log message ***
[coreutils.git] / src / touch.c
blobc7d5c009c0d63f2160ba51544fccb85f5fd297f7
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 "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 (! no_create)
129 /* Try to open FILE, creating it if necessary. */
130 fd = fd_reopen (STDIN_FILENO, file,
131 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
132 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
134 /* Don't save a copy of errno if it's EISDIR, since that would lead
135 touch to give a bogus diagnostic for e.g., `touch /' (assuming
136 we don't own / or have write access to it). On Solaris 5.6,
137 and probably other systems, it is EINVAL. On SunOS4, it's EPERM. */
138 if (fd == -1 && errno != EISDIR && errno != EINVAL && errno != EPERM)
139 open_errno = errno;
142 if (change_times != (CH_ATIME | CH_MTIME))
144 /* We're setting only one of the time values. stat the target to get
145 the other one. If we have the file descriptor already, use fstat.
146 Otherwise, either we're in no-create mode (and hence didn't call open)
147 or FILE is inaccessible or a directory, so we have to use stat. */
148 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
150 if (open_errno)
151 error (0, open_errno, _("creating %s"), quote (file));
152 else
154 if (no_create && errno == ENOENT)
155 return true;
156 error (0, errno, _("failed to get attributes of %s"),
157 quote (file));
159 if (fd != -1)
160 close (fd);
161 return false;
165 if (amtime_now)
167 /* Pass NULL to futimens so it will not fail if we have
168 write access to the file, but don't own it. */
169 t = NULL;
171 else
173 if (change_times & CH_ATIME)
174 timespec[0] = newtime[0];
175 else
177 timespec[0].tv_sec = sbuf.st_atime;
178 timespec[0].tv_nsec = TIMESPEC_NS (sbuf.st_atim);
181 if (change_times & CH_MTIME)
182 timespec[1] = newtime[1];
183 else
185 timespec[1].tv_sec = sbuf.st_mtime;
186 timespec[1].tv_nsec = TIMESPEC_NS (sbuf.st_mtim);
189 t = timespec;
192 ok = (futimens (fd, file, t) == 0);
193 if (fd != -1)
194 ok &= (close (fd) == 0);
196 if (!ok)
198 if (open_errno)
200 /* The wording of this diagnostic should cover at least two cases:
201 - the file does not exist, but the parent directory is unwritable
202 - the file exists, but it isn't writable
203 I think it's not worth trying to distinguish them. */
204 error (0, open_errno, _("cannot touch %s"), quote (file));
206 else
208 if (no_create && errno == ENOENT)
209 return true;
210 error (0, errno, _("setting times of %s"), quote (file));
212 return false;
215 return true;
218 void
219 usage (int status)
221 if (status != EXIT_SUCCESS)
222 fprintf (stderr, _("Try `%s --help' for more information.\n"),
223 program_name);
224 else
226 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
227 fputs (_("\
228 Update the access and modification times of each FILE to the current time.\n\
230 "), stdout);
231 fputs (_("\
232 Mandatory arguments to long options are mandatory for short options too.\n\
233 "), stdout);
234 fputs (_("\
235 -a change only the access time\n\
236 -c, --no-create do not create any files\n\
237 -d, --date=STRING parse STRING and use it instead of current time\n\
238 -f (ignored)\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 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
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;
267 initialize_main (&argc, &argv);
268 program_name = argv[0];
269 setlocale (LC_ALL, "");
270 bindtextdomain (PACKAGE, LOCALEDIR);
271 textdomain (PACKAGE);
273 atexit (close_stdout);
275 change_times = 0;
276 no_create = use_ref = false;
278 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
280 switch (c)
282 case 'a':
283 change_times |= CH_ATIME;
284 break;
286 case 'c':
287 no_create = true;
288 break;
290 case 'd':
291 flex_date = optarg;
292 break;
294 case 'f':
295 break;
297 case 'm':
298 change_times |= CH_MTIME;
299 break;
301 case 'r':
302 use_ref = true;
303 ref_file = optarg;
304 break;
306 case 't':
307 if (! posixtime (&newtime[0].tv_sec, optarg,
308 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS))
309 error (EXIT_FAILURE, 0, _("invalid date format %s"),
310 quote (optarg));
311 newtime[0].tv_nsec = 0;
312 newtime[1] = newtime[0];
313 date_set = true;
314 break;
316 case TIME_OPTION: /* --time */
317 change_times |= XARGMATCH ("--time", optarg,
318 time_args, time_masks);
319 break;
321 case_GETOPT_HELP_CHAR;
323 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
325 default:
326 usage (EXIT_FAILURE);
330 if (change_times == 0)
331 change_times = CH_ATIME | CH_MTIME;
333 if (date_set && (use_ref || flex_date))
335 error (0, 0, _("cannot specify times from more than one source"));
336 usage (EXIT_FAILURE);
339 if (use_ref)
341 struct stat ref_stats;
342 if (stat (ref_file, &ref_stats))
343 error (EXIT_FAILURE, errno,
344 _("failed to get attributes of %s"), quote (ref_file));
345 newtime[0].tv_sec = ref_stats.st_atime;
346 newtime[0].tv_nsec = TIMESPEC_NS (ref_stats.st_atim);
347 newtime[1].tv_sec = ref_stats.st_mtime;
348 newtime[1].tv_nsec = TIMESPEC_NS (ref_stats.st_mtim);
349 date_set = true;
350 if (flex_date)
352 if (change_times & CH_ATIME)
353 get_reldate (&newtime[0], flex_date, &newtime[0]);
354 if (change_times & CH_MTIME)
355 get_reldate (&newtime[1], flex_date, &newtime[1]);
358 else
360 if (flex_date)
362 get_reldate (&newtime[0], flex_date, NULL);
363 newtime[1] = newtime[0];
364 date_set = true;
368 /* The obsolete `MMDDhhmm[YY]' form is valid IFF there are
369 two or more non-option arguments. */
370 if (!date_set && 2 <= argc - optind && posix2_version () < 200112
371 && posixtime (&newtime[0].tv_sec, argv[optind], PDS_TRAILING_YEAR))
373 newtime[0].tv_nsec = 0;
374 newtime[1] = newtime[0];
375 date_set = true;
377 if (! getenv ("POSIXLY_CORRECT"))
379 struct tm const *tm = localtime (&newtime[0].tv_sec);
380 error (0, 0,
381 _("warning: `touch %s' is obsolete; use "
382 "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
383 argv[optind],
384 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
385 tm->tm_hour, tm->tm_min, tm->tm_sec);
388 optind++;
391 if (!date_set)
393 if (change_times == (CH_ATIME | CH_MTIME))
394 amtime_now = true;
395 else
397 gettime (&newtime[0]);
398 newtime[1] = newtime[0];
402 if (optind == argc)
404 error (0, 0, _("missing file operand"));
405 usage (EXIT_FAILURE);
408 for (; optind < argc; ++optind)
409 ok &= touch (argv[optind]);
411 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);