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)
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,
24 #include <sys/types.h>
29 #include "fd-reopen.h"
34 #include "safe-read.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
;
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
)
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
))
151 error (0, open_errno
, _("creating %s"), quote (file
));
154 if (no_create
&& errno
== ENOENT
)
156 error (0, errno
, _("failed to get attributes of %s"),
167 /* Pass NULL to futimens so it will not fail if we have
168 write access to the file, but don't own it. */
173 if (change_times
& CH_ATIME
)
174 timespec
[0] = newtime
[0];
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];
185 timespec
[1].tv_sec
= sbuf
.st_mtime
;
186 timespec
[1].tv_nsec
= TIMESPEC_NS (sbuf
.st_mtim
);
192 ok
= (futimens (fd
, file
, t
) == 0);
194 ok
&= (close (fd
) == 0);
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
));
208 if (no_create
&& errno
== ENOENT
)
210 error (0, errno
, _("setting times of %s"), quote (file
));
221 if (status
!= EXIT_SUCCESS
)
222 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
226 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
228 Update the access and modification times of each FILE to the current time.\n\
232 Mandatory arguments to long options are mandatory for short options too.\n\
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\
239 -m change only the modification time\n\
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\
248 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
249 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
252 Note that the -d and -t options accept different time-date formats.\n\
254 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
260 main (int argc
, char **argv
)
263 bool date_set
= false;
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
);
276 no_create
= use_ref
= false;
278 while ((c
= getopt_long (argc
, argv
, "acd:fmr:t:", longopts
, NULL
)) != -1)
283 change_times
|= CH_ATIME
;
298 change_times
|= CH_MTIME
;
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"),
311 newtime
[0].tv_nsec
= 0;
312 newtime
[1] = newtime
[0];
316 case TIME_OPTION
: /* --time */
317 change_times
|= XARGMATCH ("--time", optarg
,
318 time_args
, time_masks
);
321 case_GETOPT_HELP_CHAR
;
323 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
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
);
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
);
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]);
362 get_reldate (&newtime
[0], flex_date
, NULL
);
363 newtime
[1] = newtime
[0];
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];
377 if (! getenv ("POSIXLY_CORRECT"))
379 struct tm
const *tm
= localtime (&newtime
[0].tv_sec
);
381 _("warning: `touch %s' is obsolete; use "
382 "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
384 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
385 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
393 if (change_times
== (CH_ATIME
| CH_MTIME
))
397 gettime (&newtime
[0]);
398 newtime
[1] = newtime
[0];
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
);