1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-1999 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
24 #include <sys/types.h>
31 #include "safe-read.h"
40 /* Bitmasks for `change_times'. */
44 /* The name by which this program was run. */
47 /* Which timestamps to change. */
48 static int change_times
;
50 /* (-c) If nonzero, don't create if not already there. */
53 /* (-d) If nonzero, date supplied on command line in get_date formats. */
54 static int flexible_date
;
56 /* (-r) If nonzero, use times from a reference file. */
59 /* (-t) If nonzero, date supplied on command line in POSIX format. */
60 static int posix_date
;
62 /* If nonzero, the only thing we have to do is change both the
63 modification and access time to the current time, so we don't
64 have to own the file, just be able to read and write it. */
65 static int amtime_now
;
67 /* New time to use when setting time. */
68 static time_t newtime
;
70 /* File to use for -r. */
71 static char *ref_file
;
73 /* Info about the reference file. */
74 static struct stat ref_stats
;
76 /* If nonzero, display usage information and exit. */
79 /* If nonzero, print the version on standard output and exit. */
80 static int show_version
;
82 static struct option
const longopts
[] =
84 {"time", required_argument
, 0, CHAR_MAX
+ 1},
85 {"no-create", no_argument
, 0, 'c'},
86 {"date", required_argument
, 0, 'd'},
87 {"file", required_argument
, 0, 'r'},
88 {"reference", required_argument
, 0, 'r'},
89 {"help", no_argument
, &show_help
, 1},
90 {"version", no_argument
, &show_version
, 1},
94 /* Valid arguments to the `--time' option. */
95 static char const* const time_args
[] =
97 "atime", "access", "use", "mtime", "modify", 0
100 /* The bits in `change_times' that those arguments set. */
101 static int const time_masks
[] =
103 CH_ATIME
, CH_ATIME
, CH_ATIME
, CH_MTIME
, CH_MTIME
106 /* Update the time of file FILE according to the options given.
107 Return 0 if successful, 1 if an error occurs. */
116 if (stat (file
, &sbuf
))
120 error (0, errno
, "%s", file
);
125 fd
= creat (file
, 0666);
128 error (0, errno
, "%s", file
);
135 error (0, errno
, "%s", file
);
138 return 0; /* We've done all we have to. */
140 if (fstat (fd
, &sbuf
))
142 error (0, errno
, "%s", file
);
148 error (0, errno
, "%s", file
);
155 /* Pass NULL to utime so it will not fail if we just have
156 write access to the file, but don't own it. */
157 status
= utime (file
, NULL
);
163 /* There's currently no interface to set file timestamps with
164 better than 1-second resolution, so discard any fractional
165 part of the source timestamp. */
169 utb
.actime
= ref_stats
.st_atime
;
170 utb
.modtime
= ref_stats
.st_mtime
;
173 utb
.actime
= utb
.modtime
= newtime
;
175 if (!(change_times
& CH_ATIME
))
176 utb
.actime
= sbuf
.st_atime
;
178 if (!(change_times
& CH_MTIME
))
179 utb
.modtime
= sbuf
.st_mtime
;
181 status
= utime (file
, &utb
);
186 error (0, errno
, "%s", file
);
197 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
201 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
202 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
205 Update the access and modification times of each FILE to the current time.\n\
207 -a change only the access time\n\
208 -c do not create any files\n\
209 -d, --date=STRING parse STRING and use it instead of current time\n\
211 -m change only the modification time\n\
212 -r, --reference=FILE use this file's times instead of current time\n\
213 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
214 --time=WORD access -a, atime -a, mtime -m, modify -m, use -a\n\
215 --help display this help and exit\n\
216 --version output version information and exit\n\
218 STAMP may be used without -t if none of -drt, nor --, are used.\n\
219 Note that the three time-date formats recognized for the -d and -t options\n\
220 and for the obsolescent argument are all different.\n\
222 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
229 main (int argc
, char **argv
)
235 program_name
= argv
[0];
236 setlocale (LC_ALL
, "");
237 bindtextdomain (PACKAGE
, LOCALEDIR
);
238 textdomain (PACKAGE
);
240 change_times
= no_create
= use_ref
= posix_date
= flexible_date
= 0;
241 newtime
= (time_t) -1;
243 while ((c
= getopt_long (argc
, argv
, "acd:fmr:t:", longopts
, NULL
)) != -1)
251 change_times
|= CH_ATIME
;
260 newtime
= get_date (optarg
, NULL
);
261 if (newtime
== (time_t) -1)
262 error (1, 0, _("invalid date format `%s'"), optarg
);
270 change_times
|= CH_MTIME
;
280 newtime
= posixtime (optarg
,
281 PDS_LEADING_YEAR
| PDS_CENTURY
| PDS_SECONDS
);
282 if (newtime
== (time_t) -1)
283 error (1, 0, _("invalid date format `%s'"), optarg
);
287 case CHAR_MAX
+ 1: /* --time */
288 change_times
|= XARGMATCH ("--time", optarg
,
289 time_args
, time_masks
);
299 printf ("touch (%s) %s\n", GNU_PACKAGE
, VERSION
);
307 if (change_times
== 0)
308 change_times
= CH_ATIME
| CH_MTIME
;
310 if ((use_ref
&& (posix_date
|| flexible_date
))
311 || (posix_date
&& flexible_date
))
313 error (0, 0, _("cannot specify times from more than one source"));
319 if (stat (ref_file
, &ref_stats
))
320 error (1, errno
, "%s", ref_file
);
324 if (!date_set
&& optind
< argc
&& !STREQ (argv
[optind
- 1], "--"))
326 newtime
= posixtime (argv
[optind
], PDS_TRAILING_YEAR
);
327 if (newtime
!= (time_t) -1)
335 if ((change_times
& (CH_ATIME
| CH_MTIME
)) == (CH_ATIME
| CH_MTIME
))
343 error (0, 0, _("file arguments missing"));
347 for (; optind
< argc
; ++optind
)
348 err
+= touch (argv
[optind
]);