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,
23 #include <sys/types.h>
28 #include "fd-reopen.h"
33 #include "safe-read.h"
34 #include "stat-time.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "touch"
41 "Paul Rubin", "Arnold Robbins, Jim Kingdon, David MacKenzie", "Randy Smith"
43 /* Bitmasks for `change_times'. */
47 /* The name by which this program was run. */
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. */
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. */
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
},
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. */
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. */
118 touch (const char *file
)
124 struct timespec timespec
[2];
125 struct timespec
const *t
;
127 if (STREQ (file
, "-"))
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
)
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
))
153 error (0, open_errno
, _("creating %s"), quote (file
));
156 if (no_create
&& (errno
== ENOENT
|| errno
== EBADF
))
158 error (0, errno
, _("failed to get attributes of %s"),
161 if (fd
== STDIN_FILENO
)
169 /* Pass NULL to futimens so it will not fail if we have
170 write access to the file, but don't own it. */
175 timespec
[0] = (change_times
& CH_ATIME
177 : get_stat_atime (&sbuf
));
178 timespec
[1] = (change_times
& CH_MTIME
180 : get_stat_mtime (&sbuf
));
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
));
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
))
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
));
214 if (no_create
&& errno
== ENOENT
)
216 error (0, errno
, _("setting times of %s"), quote (file
));
227 if (status
!= EXIT_SUCCESS
)
228 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
232 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
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\
243 Mandatory arguments to long options are mandatory for short options too.\n\
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\
250 -m change only the modification time\n\
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\
259 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
260 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
263 Note that the -d and -t options accept different time-date formats.\n\
265 emit_bug_reporting_address ();
271 main (int argc
, char **argv
)
274 bool date_set
= false;
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
);
287 no_create
= use_ref
= false;
289 while ((c
= getopt_long (argc
, argv
, "acd:fmr:t:", longopts
, NULL
)) != -1)
294 change_times
|= CH_ATIME
;
309 change_times
|= CH_MTIME
;
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"),
322 newtime
[0].tv_nsec
= 0;
323 newtime
[1] = newtime
[0];
327 case TIME_OPTION
: /* --time */
328 change_times
|= XARGMATCH ("--time", optarg
,
329 time_args
, time_masks
);
332 case_GETOPT_HELP_CHAR
;
334 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
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
);
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
);
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]);
371 get_reldate (&newtime
[0], flex_date
, NULL
);
372 newtime
[1] = newtime
[0];
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];
387 if (! getenv ("POSIXLY_CORRECT"))
389 struct tm
const *tm
= localtime (&newtime
[0].tv_sec
);
391 _("warning: `touch %s' is obsolete; use "
392 "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
394 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
395 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
403 if (change_times
== (CH_ATIME
| CH_MTIME
))
407 gettime (&newtime
[0]);
408 newtime
[1] = newtime
[0];
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
);