3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
59 static void stime_arg1(const char *, struct timespec *);
60 static void stime_arg2(const char *, int, struct timespec *);
61 static void stime_darg(const char *, struct timespec *);
62 static void stime_file(const char *, struct timespec *);
63 static int timeoffset(const char *);
64 static void usage(const char *);
67 main(int argc, char *argv[])
70 struct timespec ts[2];
72 int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
76 myname = basename(argv[0]);
77 Aflag = aflag = cflag = mflag = timeset = 0;
79 if (clock_gettime(CLOCK_REALTIME, &ts[0]) == -1)
80 err(1, "clock_gettime(CLOCK_REALTIME)");
82 while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
85 Aflag = timeoffset(optarg);
95 stime_darg(optarg, ts);
98 /* No-op for compatibility. */
102 atflag = AT_SYMLINK_NOFOLLOW;
109 stime_file(optarg, ts);
113 stime_arg1(optarg, ts);
121 if (aflag == 0 && mflag == 0)
127 * We're setting the time to an offset from a specified
128 * time. God knows why, but it means that we can set
129 * that time once and for all here.
132 ts[0].tv_sec += Aflag;
134 ts[1].tv_sec += Aflag;
135 Aflag = 0; /* done our job */
139 * If no -r or -t flag, at least two operands, the first of
140 * which is an 8 or 10 digit number, use the obsolete time
141 * specification, otherwise use the current time.
144 strtol(argv[0], &p, 10);
146 if (*p == '\0' && (len == 8 || len == 10)) {
148 stime_arg2(*argv++, len == 10, ts);
151 /* Both times default to the same. */
161 for (rval = 0; *argv; ++argv) {
162 /* See if the file exists. */
163 if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
164 if (errno != ENOENT) {
170 /* Create the file. */
172 O_WRONLY | O_CREAT, DEFFILEMODE);
173 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
179 /* If using the current time, we're done. */
192 * We're adjusting the times based on the file times, not a
193 * specified time (that gets handled above).
198 ts[0].tv_sec += Aflag;
202 ts[1].tv_sec += Aflag;
206 /* Try utimensat(2). */
207 if (!utimensat(AT_FDCWD, *argv, ts, atflag))
210 /* If the user specified a time, nothing else we can do. */
211 if (timeset || Aflag) {
218 * System V and POSIX 1003.1 require that a NULL argument
219 * set the access/modification times to the current time.
220 * The permission checks are different, too, in that the
221 * ability to write the file is sufficient. Take a shot.
223 if (!utimensat(AT_FDCWD, *argv, NULL, atflag))
232 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
235 stime_arg1(const char *arg, struct timespec *tvp)
241 /* Start with the current time. */
243 if ((t = localtime(&now)) == NULL)
245 /* [[CC]YY]MMDDhhmm[.SS] */
246 if ((p = strchr(arg, '.')) == NULL)
247 t->tm_sec = 0; /* Seconds defaults to 0. */
249 if (strlen(p + 1) != 2)
252 t->tm_sec = ATOI2(p);
256 switch(strlen(arg)) {
257 case 12: /* CCYYMMDDhhmm */
258 t->tm_year = ATOI2(arg);
262 case 10: /* YYMMDDhhmm */
264 yearset = ATOI2(arg);
265 t->tm_year += yearset;
267 yearset = ATOI2(arg);
269 t->tm_year = yearset + 2000;
271 t->tm_year = yearset + 1900;
273 t->tm_year -= 1900; /* Convert to UNIX time. */
275 case 8: /* MMDDhhmm */
276 t->tm_mon = ATOI2(arg);
277 --t->tm_mon; /* Convert from 01-12 to 00-11 */
278 t->tm_mday = ATOI2(arg);
279 t->tm_hour = ATOI2(arg);
280 t->tm_min = ATOI2(arg);
286 t->tm_isdst = -1; /* Figure out DST. */
287 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
288 if (tvp[0].tv_sec == -1)
291 tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
295 errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
299 stime_arg2(const char *arg, int year, struct timespec *tvp)
303 /* Start with the current time. */
305 if ((t = localtime(&now)) == NULL)
308 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
309 --t->tm_mon; /* Convert from 01-12 to 00-11 */
310 t->tm_mday = ATOI2(arg);
311 t->tm_hour = ATOI2(arg);
312 t->tm_min = ATOI2(arg);
314 t->tm_year = ATOI2(arg);
315 if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */
319 t->tm_isdst = -1; /* Figure out DST. */
320 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
321 if (tvp[0].tv_sec == -1)
323 "out of range or illegal time specification: MMDDhhmm[yy]");
325 tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
329 stime_darg(const char *arg, struct timespec *tvp)
331 struct tm t = { .tm_sec = 0 };
332 const char *fmt, *colon;
338 colon = strchr(arg, ':');
339 if (colon == NULL || strchr(colon + 1, ':') == NULL)
341 fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
343 p = strptime(arg, fmt, &t);
346 /* POSIX: must have at least one digit after dot */
347 if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
350 while (isdigit((unsigned char)*p)) {
351 tvp[0].tv_nsec += val * (*p - '0');
363 tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
369 errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
372 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
374 timeoffset(const char *arg)
383 switch (strlen(arg)) {
384 default: /* invalid */
385 errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
391 offset = offset * 60 + ATOI2(arg);
394 offset = offset * 60 + ATOI2(arg);
403 stime_file(const char *fname, struct timespec *tsp)
407 if (stat(fname, &sb))
414 usage(const char *myname)
416 fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
417 "[-t [[CC]YY]MMDDhhmm[.SS]]\n"
418 " [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
419 "file ...\n", myname);