1 /* touch -- change modification and access times of files
2 Copyright (C) 1987-2023 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
23 #include <sys/types.h>
30 #include "fd-reopen.h"
31 #include "parse-datetime.h"
35 #include "stat-time.h"
38 /* The official name of this program (e.g., no 'g' prefix). */
39 #define PROGRAM_NAME "touch"
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'. */
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. */
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. */
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 {"reference", required_argument
, NULL
, 'r'},
90 {"no-dereference", no_argument
, NULL
, 'h'},
91 {GETOPT_HELP_OPTION_DECL
},
92 {GETOPT_VERSION_OPTION_DECL
},
96 /* Valid arguments to the '--time' option. */
97 static char const *const time_args
[] =
99 "atime", "access", "use", "mtime", "modify", NULL
102 /* The bits in 'change_times' that those arguments set. */
103 static int const time_masks
[] =
105 CH_ATIME
, CH_ATIME
, CH_ATIME
, CH_MTIME
, CH_MTIME
108 /* The interpretation of FLEX_DATE as a date, relative to NOW. */
110 static struct timespec
111 date_relative (char const *flex_date
, struct timespec now
)
113 struct timespec result
;
114 if (! parse_datetime (&result
, flex_date
, &now
))
115 die (EXIT_FAILURE
, 0, _("invalid date format %s"), quote (flex_date
));
119 /* Update the time of file FILE according to the options given.
120 Return true if successful. */
123 touch (char const *file
)
127 struct timespec
const *t
= newtime
;
129 if (STREQ (file
, "-"))
131 else if (! (no_create
|| no_dereference
))
133 /* Try to open FILE, creating it if necessary. */
134 fd
= fd_reopen (STDIN_FILENO
, file
,
135 O_WRONLY
| O_CREAT
| O_NONBLOCK
| O_NOCTTY
, MODE_RW_UGO
);
140 if (change_times
!= (CH_ATIME
| CH_MTIME
))
142 /* We're setting only one of the time values. */
143 if (change_times
== CH_MTIME
)
144 newtime
[0].tv_nsec
= UTIME_OMIT
;
147 assert (change_times
== CH_ATIME
);
148 newtime
[1].tv_nsec
= UTIME_OMIT
;
154 /* Pass NULL to futimens so it will not fail if we have
155 write access to the file, but don't own it. */
159 char const *file_opt
= fd
== STDOUT_FILENO
? NULL
: file
;
160 int atflag
= no_dereference
? AT_SYMLINK_NOFOLLOW
: 0;
161 int utime_errno
= (fdutimensat (fd
, AT_FDCWD
, file_opt
, t
, atflag
) == 0
164 if (fd
== STDIN_FILENO
)
166 if (close (STDIN_FILENO
) != 0)
168 error (0, errno
, _("failed to close %s"), quoteaf (file
));
172 else if (fd
== STDOUT_FILENO
)
174 /* Do not diagnose "touch -c - >&-". */
175 if (utime_errno
== EBADF
&& no_create
)
179 if (utime_errno
!= 0)
181 /* Don't diagnose with open_errno if FILE is a directory, as that
182 would give a bogus diagnostic for e.g., 'touch /' (assuming we
183 don't own / or have write access). On Solaris 10 and probably
184 other systems, opening a directory like "." fails with EINVAL.
185 (On SunOS 4 it was EPERM but that's obsolete.) */
188 && ! (open_errno
== EISDIR
189 || (open_errno
== EINVAL
190 && stat (file
, &st
) == 0 && S_ISDIR (st
.st_mode
))))
192 /* The wording of this diagnostic should cover at least two cases:
193 - the file does not exist, but the parent directory is unwritable
194 - the file exists, but it isn't writable
195 I think it's not worth trying to distinguish them. */
196 error (0, open_errno
, _("cannot touch %s"), quoteaf (file
));
200 if (no_create
&& utime_errno
== ENOENT
)
202 error (0, utime_errno
, _("setting times of %s"), quoteaf (file
));
213 if (status
!= EXIT_SUCCESS
)
217 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
219 Update the access and modification times of each FILE to the current time.\n\
221 A FILE argument that does not exist is created empty, unless -c or -h\n\
224 A FILE argument string of - is handled specially and causes touch to\n\
225 change the times of the file associated with standard output.\n\
228 emit_mandatory_arg_note ();
231 -a change only the access time\n\
232 -c, --no-create do not create any files\n\
233 -d, --date=STRING parse STRING and use it instead of current time\n\
237 -h, --no-dereference affect each symbolic link instead of any referenced\n\
238 file (useful only on systems that can change the\n\
239 timestamps of a symlink)\n\
240 -m change only the modification time\n\
243 -r, --reference=FILE use this file's times instead of current time\n\
244 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
245 --time=WORD change the specified time:\n\
246 WORD is access, atime, or use: equivalent to -a\n\
247 WORD is modify or mtime: equivalent to -m\n\
249 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
250 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
253 Note that the -d and -t options accept different time-date formats.\n\
255 emit_ancillary_info (PROGRAM_NAME
);
261 main (int argc
, char **argv
)
264 bool date_set
= false;
266 char const *flex_date
= NULL
;
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
);
277 no_create
= use_ref
= false;
279 while ((c
= getopt_long (argc
, argv
, "acd:fhmr:t:", longopts
, NULL
)) != -1)
284 change_times
|= CH_ATIME
;
299 no_dereference
= true;
303 change_times
|= CH_MTIME
;
312 if (! posixtime (&newtime
[0].tv_sec
, optarg
,
313 PDS_LEADING_YEAR
| PDS_CENTURY
| PDS_SECONDS
))
314 die (EXIT_FAILURE
, 0, _("invalid date format %s"),
316 newtime
[0].tv_nsec
= 0;
317 newtime
[1] = newtime
[0];
321 case TIME_OPTION
: /* --time */
322 change_times
|= XARGMATCH ("--time", optarg
,
323 time_args
, time_masks
);
326 case_GETOPT_HELP_CHAR
;
328 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
331 usage (EXIT_FAILURE
);
335 if (change_times
== 0)
336 change_times
= CH_ATIME
| CH_MTIME
;
338 if (date_set
&& (use_ref
|| flex_date
))
340 error (0, 0, _("cannot specify times from more than one source"));
341 usage (EXIT_FAILURE
);
346 struct stat ref_stats
;
347 /* Don't use (no_dereference?lstat:stat) (args), since stat
348 might be an object-like macro. */
349 if (no_dereference
? lstat (ref_file
, &ref_stats
)
350 : stat (ref_file
, &ref_stats
))
351 die (EXIT_FAILURE
, errno
,
352 _("failed to get attributes of %s"), quoteaf (ref_file
));
353 newtime
[0] = get_stat_atime (&ref_stats
);
354 newtime
[1] = get_stat_mtime (&ref_stats
);
358 if (change_times
& CH_ATIME
)
359 newtime
[0] = date_relative (flex_date
, newtime
[0]);
360 if (change_times
& CH_MTIME
)
361 newtime
[1] = date_relative (flex_date
, newtime
[1]);
368 struct timespec now
= current_timespec ();
369 newtime
[1] = newtime
[0] = date_relative (flex_date
, now
);
372 /* If neither -a nor -m is specified, treat "-d now" as if
373 it were absent; this lets "touch" succeed more often in
374 the presence of restrictive permissions. */
375 if (change_times
== (CH_ATIME
| CH_MTIME
)
376 && newtime
[0].tv_sec
== now
.tv_sec
377 && newtime
[0].tv_nsec
== now
.tv_nsec
)
379 /* Check that it really was "-d now", and not a timestamp
380 that just happens to be the current time. */
381 struct timespec notnow
, notnow1
;
382 notnow
.tv_sec
= now
.tv_sec
^ 1;
383 notnow
.tv_nsec
= now
.tv_nsec
;
384 notnow1
= date_relative (flex_date
, notnow
);
385 if (notnow1
.tv_sec
== notnow
.tv_sec
386 && notnow1
.tv_nsec
== notnow
.tv_nsec
)
392 /* The obsolete 'MMDDhhmm[YY]' form is valid IFF there are
393 two or more non-option arguments. */
394 if (!date_set
&& 2 <= argc
- optind
&& posix2_version () < 200112
395 && posixtime (&newtime
[0].tv_sec
, argv
[optind
],
396 PDS_TRAILING_YEAR
| PDS_PRE_2000
))
398 newtime
[0].tv_nsec
= 0;
399 newtime
[1] = newtime
[0];
402 if (! getenv ("POSIXLY_CORRECT"))
404 struct tm
const *tm
= localtime (&newtime
[0].tv_sec
);
406 /* Technically, it appears that even a deliberate attempt to cause
407 the above localtime to return NULL will always fail because our
408 posixtime implementation rejects all dates for which localtime
409 would fail. However, skip the warning if it ever fails. */
412 _("warning: 'touch %s' is obsolete; use "
413 "'touch -t %04ld%02d%02d%02d%02d.%02d'"),
415 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
416 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
424 if (change_times
== (CH_ATIME
| CH_MTIME
))
427 newtime
[1].tv_nsec
= newtime
[0].tv_nsec
= UTIME_NOW
;
432 error (0, 0, _("missing file operand"));
433 usage (EXIT_FAILURE
);
436 for (; optind
< argc
; ++optind
)
437 ok
&= touch (argv
[optind
]);
439 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;