xref: /freebsd/usr.bin/touch/touch.c (revision cb54c500d0e1a2c52270b15c6db6a88ca3feb86a)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
321a9e1c9dSMark Murray #include <sys/cdefs.h>
331a9e1c9dSMark Murray 
341a9e1c9dSMark Murray __FBSDID("$FreeBSD$");
351a9e1c9dSMark Murray 
369b50d902SRodney W. Grimes #ifndef lint
3716dd925fSKris Kennaway static const char copyright[] =
389b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\
399b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
401a9e1c9dSMark Murray #endif
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #ifndef lint
4316dd925fSKris Kennaway static const char sccsid[] = "@(#)touch.c	8.1 (Berkeley) 6/6/93";
4416dd925fSKris Kennaway #endif
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <sys/types.h>
479b50d902SRodney W. Grimes #include <sys/stat.h>
489b50d902SRodney W. Grimes #include <sys/time.h>
499b50d902SRodney W. Grimes 
5058771450SJilles Tjoelker #include <ctype.h>
519b50d902SRodney W. Grimes #include <err.h>
529b50d902SRodney W. Grimes #include <errno.h>
539b50d902SRodney W. Grimes #include <fcntl.h>
541b54381bSGreg Lehey #include <libgen.h>
559b50d902SRodney W. Grimes #include <stdio.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
589b50d902SRodney W. Grimes #include <time.h>
599b50d902SRodney W. Grimes #include <unistd.h>
609b50d902SRodney W. Grimes 
615faf2ae1SJilles Tjoelker static void	stime_arg1(const char *, struct timespec *);
625faf2ae1SJilles Tjoelker static void	stime_arg2(const char *, int, struct timespec *);
635faf2ae1SJilles Tjoelker static void	stime_darg(const char *, struct timespec *);
645faf2ae1SJilles Tjoelker static void	stime_file(const char *, struct timespec *);
654145c5f1SEitan Adler static int	timeoffset(const char *);
66b268ae64SEitan Adler static void	usage(const char *);
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes int
69f4ac32deSDavid Malone main(int argc, char *argv[])
709b50d902SRodney W. Grimes {
719b50d902SRodney W. Grimes 	struct stat sb;
725faf2ae1SJilles Tjoelker 	struct timespec ts[2];
735faf2ae1SJilles Tjoelker 	int atflag;
74401c9fdaSJaakko Heinonen 	int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
759b50d902SRodney W. Grimes 	char *p;
763481910dSGreg Lehey 	char *myname;
779b50d902SRodney W. Grimes 
781b54381bSGreg Lehey 	myname = basename(argv[0]);
79401c9fdaSJaakko Heinonen 	Aflag = aflag = cflag = mflag = timeset = 0;
805faf2ae1SJilles Tjoelker 	atflag = 0;
8151492908SJilles Tjoelker 	ts[0].tv_sec = ts[1].tv_sec = 0;
8251492908SJilles Tjoelker 	ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
839b50d902SRodney W. Grimes 
8458771450SJilles Tjoelker 	while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
859b50d902SRodney W. Grimes 		switch(ch) {
863481910dSGreg Lehey 		case 'A':
873481910dSGreg Lehey 			Aflag = timeoffset(optarg);
883481910dSGreg Lehey 			break;
899b50d902SRodney W. Grimes 		case 'a':
909b50d902SRodney W. Grimes 			aflag = 1;
919b50d902SRodney W. Grimes 			break;
929b50d902SRodney W. Grimes 		case 'c':
939b50d902SRodney W. Grimes 			cflag = 1;
949b50d902SRodney W. Grimes 			break;
9558771450SJilles Tjoelker 		case 'd':
9658771450SJilles Tjoelker 			timeset = 1;
975faf2ae1SJilles Tjoelker 			stime_darg(optarg, ts);
9858771450SJilles Tjoelker 			break;
999b50d902SRodney W. Grimes 		case 'f':
100401c9fdaSJaakko Heinonen 			/* No-op for compatibility. */
1019b50d902SRodney W. Grimes 			break;
102842d1c6cSDavid E. O'Brien 		case 'h':
103842d1c6cSDavid E. O'Brien 			cflag = 1;
1045faf2ae1SJilles Tjoelker 			atflag = AT_SYMLINK_NOFOLLOW;
105842d1c6cSDavid E. O'Brien 			break;
1069b50d902SRodney W. Grimes 		case 'm':
1079b50d902SRodney W. Grimes 			mflag = 1;
1089b50d902SRodney W. Grimes 			break;
1099b50d902SRodney W. Grimes 		case 'r':
1109b50d902SRodney W. Grimes 			timeset = 1;
1115faf2ae1SJilles Tjoelker 			stime_file(optarg, ts);
1129b50d902SRodney W. Grimes 			break;
1139b50d902SRodney W. Grimes 		case 't':
1149b50d902SRodney W. Grimes 			timeset = 1;
1155faf2ae1SJilles Tjoelker 			stime_arg1(optarg, ts);
1169b50d902SRodney W. Grimes 			break;
1179b50d902SRodney W. Grimes 		default:
1183481910dSGreg Lehey 			usage(myname);
1199b50d902SRodney W. Grimes 		}
1209b50d902SRodney W. Grimes 	argc -= optind;
1219b50d902SRodney W. Grimes 	argv += optind;
1229b50d902SRodney W. Grimes 
1231b54381bSGreg Lehey 	if (aflag == 0 && mflag == 0)
1249b50d902SRodney W. Grimes 		aflag = mflag = 1;
1259b50d902SRodney W. Grimes 
1261b54381bSGreg Lehey 	if (timeset) {
1271b54381bSGreg Lehey 		if (Aflag) {
1289b50d902SRodney W. Grimes 			/*
1291b54381bSGreg Lehey 			 * We're setting the time to an offset from a specified
1301b54381bSGreg Lehey 			 * time.  God knows why, but it means that we can set
1311b54381bSGreg Lehey 			 * that time once and for all here.
1329b50d902SRodney W. Grimes 			 */
1331b54381bSGreg Lehey 			if (aflag)
1345faf2ae1SJilles Tjoelker 				ts[0].tv_sec += Aflag;
1351b54381bSGreg Lehey 			if (mflag)
1365faf2ae1SJilles Tjoelker 				ts[1].tv_sec += Aflag;
1371b54381bSGreg Lehey 			Aflag = 0;		/* done our job */
1381b54381bSGreg Lehey 		}
1391b54381bSGreg Lehey 	} else {
1401b54381bSGreg Lehey 		/*
1411b54381bSGreg Lehey 		 * If no -r or -t flag, at least two operands, the first of
1421b54381bSGreg Lehey 		 * which is an 8 or 10 digit number, use the obsolete time
1431b54381bSGreg Lehey 		 * specification, otherwise use the current time.
1441b54381bSGreg Lehey 		 */
1451b54381bSGreg Lehey 		if (argc > 1) {
1461b54381bSGreg Lehey 			strtol(argv[0], &p, 10);
1479b50d902SRodney W. Grimes 			len = p - argv[0];
1489b50d902SRodney W. Grimes 			if (*p == '\0' && (len == 8 || len == 10)) {
1499b50d902SRodney W. Grimes 				timeset = 1;
1505faf2ae1SJilles Tjoelker 				stime_arg2(*argv++, len == 10, ts);
1519b50d902SRodney W. Grimes 			}
1529b50d902SRodney W. Grimes 		}
1531b54381bSGreg Lehey 		/* Both times default to the same. */
1545faf2ae1SJilles Tjoelker 		ts[1] = ts[0];
1551b54381bSGreg Lehey 	}
1569b50d902SRodney W. Grimes 
15751492908SJilles Tjoelker 	if (!aflag)
15851492908SJilles Tjoelker 		ts[0].tv_nsec = UTIME_OMIT;
15951492908SJilles Tjoelker 	if (!mflag)
16051492908SJilles Tjoelker 		ts[1].tv_nsec = UTIME_OMIT;
16151492908SJilles Tjoelker 
1629b50d902SRodney W. Grimes 	if (*argv == NULL)
1633481910dSGreg Lehey 		usage(myname);
1649b50d902SRodney W. Grimes 
1651b54381bSGreg Lehey 	if (Aflag)
1661b54381bSGreg Lehey 		cflag = 1;
1671b54381bSGreg Lehey 
1689b50d902SRodney W. Grimes 	for (rval = 0; *argv; ++argv) {
1699b50d902SRodney W. Grimes 		/* See if the file exists. */
1705faf2ae1SJilles Tjoelker 		if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
171c64b097bSJaakko Heinonen 			if (errno != ENOENT) {
172c64b097bSJaakko Heinonen 				rval = 1;
173c64b097bSJaakko Heinonen 				warn("%s", *argv);
174c64b097bSJaakko Heinonen 				continue;
175c64b097bSJaakko Heinonen 			}
1769b50d902SRodney W. Grimes 			if (!cflag) {
1779b50d902SRodney W. Grimes 				/* Create the file. */
1789b50d902SRodney W. Grimes 				fd = open(*argv,
1799b50d902SRodney W. Grimes 				    O_WRONLY | O_CREAT, DEFFILEMODE);
180*cb54c500SMariusz Zaborski 				if (fd == -1) {
1819b50d902SRodney W. Grimes 					rval = 1;
1829b50d902SRodney W. Grimes 					warn("%s", *argv);
1839b50d902SRodney W. Grimes 					continue;
1849b50d902SRodney W. Grimes 				}
185*cb54c500SMariusz Zaborski 				if (fstat(fd, &sb) < 0) {
186*cb54c500SMariusz Zaborski 					warn("%s", *argv);
187*cb54c500SMariusz Zaborski 					rval = 1;
188*cb54c500SMariusz Zaborski 				}
189*cb54c500SMariusz Zaborski 				if (close(fd) < 0) {
190*cb54c500SMariusz Zaborski 					warn("%s", *argv);
191*cb54c500SMariusz Zaborski 					rval = 1;
192*cb54c500SMariusz Zaborski 				}
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes 				/* If using the current time, we're done. */
1959b50d902SRodney W. Grimes 				if (!timeset)
1969b50d902SRodney W. Grimes 					continue;
1979b50d902SRodney W. Grimes 			} else
1989b50d902SRodney W. Grimes 				continue;
1999ef5c48bSBill Fumerola 		}
2009b50d902SRodney W. Grimes 
2011b54381bSGreg Lehey 		/*
2021b54381bSGreg Lehey 		 * We're adjusting the times based on the file times, not a
2031b54381bSGreg Lehey 		 * specified time (that gets handled above).
2041b54381bSGreg Lehey 		 */
2053481910dSGreg Lehey 		if (Aflag) {
2061b54381bSGreg Lehey 			if (aflag) {
2075faf2ae1SJilles Tjoelker 				ts[0] = sb.st_atim;
2085faf2ae1SJilles Tjoelker 				ts[0].tv_sec += Aflag;
2091b54381bSGreg Lehey 			}
2101b54381bSGreg Lehey 			if (mflag) {
2115faf2ae1SJilles Tjoelker 				ts[1] = sb.st_mtim;
2125faf2ae1SJilles Tjoelker 				ts[1].tv_sec += Aflag;
2133481910dSGreg Lehey 			}
2141b54381bSGreg Lehey 		}
2159b50d902SRodney W. Grimes 
2165faf2ae1SJilles Tjoelker 		if (!utimensat(AT_FDCWD, *argv, ts, atflag))
2179b50d902SRodney W. Grimes 			continue;
2189b50d902SRodney W. Grimes 
219c64b097bSJaakko Heinonen 		rval = 1;
2204d01a8fbSEric Melville 		warn("%s", *argv);
2219b50d902SRodney W. Grimes 	}
2229b50d902SRodney W. Grimes 	exit(rval);
2239b50d902SRodney W. Grimes }
2249b50d902SRodney W. Grimes 
2259b50d902SRodney W. Grimes #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
2269b50d902SRodney W. Grimes 
2274145c5f1SEitan Adler static void
2285faf2ae1SJilles Tjoelker stime_arg1(const char *arg, struct timespec *tvp)
2299b50d902SRodney W. Grimes {
23039470616SBruce Evans 	time_t now;
2319b50d902SRodney W. Grimes 	struct tm *t;
2329b50d902SRodney W. Grimes 	int yearset;
2339b50d902SRodney W. Grimes 	char *p;
23451492908SJilles Tjoelker 
23551492908SJilles Tjoelker 	now = time(NULL);
23639470616SBruce Evans 	if ((t = localtime(&now)) == NULL)
2379b50d902SRodney W. Grimes 		err(1, "localtime");
2389b50d902SRodney W. Grimes 					/* [[CC]YY]MMDDhhmm[.SS] */
2399b50d902SRodney W. Grimes 	if ((p = strchr(arg, '.')) == NULL)
2409b50d902SRodney W. Grimes 		t->tm_sec = 0;		/* Seconds defaults to 0. */
2419b50d902SRodney W. Grimes 	else {
2429b50d902SRodney W. Grimes 		if (strlen(p + 1) != 2)
2439b50d902SRodney W. Grimes 			goto terr;
2449b50d902SRodney W. Grimes 		*p++ = '\0';
2459b50d902SRodney W. Grimes 		t->tm_sec = ATOI2(p);
2469b50d902SRodney W. Grimes 	}
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	yearset = 0;
2499b50d902SRodney W. Grimes 	switch(strlen(arg)) {
2509b50d902SRodney W. Grimes 	case 12:			/* CCYYMMDDhhmm */
2519b50d902SRodney W. Grimes 		t->tm_year = ATOI2(arg);
252d38ca307SJoerg Wunsch 		t->tm_year *= 100;
2539b50d902SRodney W. Grimes 		yearset = 1;
25493b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2559b50d902SRodney W. Grimes 	case 10:			/* YYMMDDhhmm */
2569b50d902SRodney W. Grimes 		if (yearset) {
2579b50d902SRodney W. Grimes 			yearset = ATOI2(arg);
2589b50d902SRodney W. Grimes 			t->tm_year += yearset;
2599b50d902SRodney W. Grimes 		} else {
2609b50d902SRodney W. Grimes 			yearset = ATOI2(arg);
2619b50d902SRodney W. Grimes 			if (yearset < 69)
2629b50d902SRodney W. Grimes 				t->tm_year = yearset + 2000;
2639b50d902SRodney W. Grimes 			else
2649b50d902SRodney W. Grimes 				t->tm_year = yearset + 1900;
2659b50d902SRodney W. Grimes 		}
2669b50d902SRodney W. Grimes 		t->tm_year -= 1900;	/* Convert to UNIX time. */
2679b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2689b50d902SRodney W. Grimes 	case 8:				/* MMDDhhmm */
2699b50d902SRodney W. Grimes 		t->tm_mon = ATOI2(arg);
2709b50d902SRodney W. Grimes 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
2719b50d902SRodney W. Grimes 		t->tm_mday = ATOI2(arg);
2729b50d902SRodney W. Grimes 		t->tm_hour = ATOI2(arg);
2739b50d902SRodney W. Grimes 		t->tm_min = ATOI2(arg);
2749b50d902SRodney W. Grimes 		break;
2759b50d902SRodney W. Grimes 	default:
2769b50d902SRodney W. Grimes 		goto terr;
2779b50d902SRodney W. Grimes 	}
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes 	t->tm_isdst = -1;		/* Figure out DST. */
2809b50d902SRodney W. Grimes 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
2819b50d902SRodney W. Grimes 	if (tvp[0].tv_sec == -1)
2824145c5f1SEitan Adler 		goto terr;
2839b50d902SRodney W. Grimes 
2845faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
2854145c5f1SEitan Adler 	return;
2864145c5f1SEitan Adler 
2874145c5f1SEitan Adler terr:
2884145c5f1SEitan Adler 	errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
2899b50d902SRodney W. Grimes }
2909b50d902SRodney W. Grimes 
2914145c5f1SEitan Adler static void
2925faf2ae1SJilles Tjoelker stime_arg2(const char *arg, int year, struct timespec *tvp)
2939b50d902SRodney W. Grimes {
29439470616SBruce Evans 	time_t now;
2959b50d902SRodney W. Grimes 	struct tm *t;
29651492908SJilles Tjoelker 
29751492908SJilles Tjoelker 	now = time(NULL);
29839470616SBruce Evans 	if ((t = localtime(&now)) == NULL)
2999b50d902SRodney W. Grimes 		err(1, "localtime");
3009b50d902SRodney W. Grimes 
3019b50d902SRodney W. Grimes 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
3029b50d902SRodney W. Grimes 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
3039b50d902SRodney W. Grimes 	t->tm_mday = ATOI2(arg);
3049b50d902SRodney W. Grimes 	t->tm_hour = ATOI2(arg);
3059b50d902SRodney W. Grimes 	t->tm_min = ATOI2(arg);
30634c7ff49SDaniel O'Callaghan 	if (year) {
3079b50d902SRodney W. Grimes 		t->tm_year = ATOI2(arg);
30825c4d008SDaniel O'Callaghan 		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
30934c7ff49SDaniel O'Callaghan 			t->tm_year += 100;
31034c7ff49SDaniel O'Callaghan 	}
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	t->tm_isdst = -1;		/* Figure out DST. */
3139b50d902SRodney W. Grimes 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
3149b50d902SRodney W. Grimes 	if (tvp[0].tv_sec == -1)
3159b50d902SRodney W. Grimes 		errx(1,
3169b50d902SRodney W. Grimes 	"out of range or illegal time specification: MMDDhhmm[yy]");
3179b50d902SRodney W. Grimes 
3185faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
3199b50d902SRodney W. Grimes }
3209b50d902SRodney W. Grimes 
3214145c5f1SEitan Adler static void
3225faf2ae1SJilles Tjoelker stime_darg(const char *arg, struct timespec *tvp)
32358771450SJilles Tjoelker {
32458771450SJilles Tjoelker 	struct tm t = { .tm_sec = 0 };
32558771450SJilles Tjoelker 	const char *fmt, *colon;
32658771450SJilles Tjoelker 	char *p;
32758771450SJilles Tjoelker 	int val, isutc = 0;
32858771450SJilles Tjoelker 
3295faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = 0;
33058771450SJilles Tjoelker 	t.tm_isdst = -1;
33158771450SJilles Tjoelker 	colon = strchr(arg, ':');
33258771450SJilles Tjoelker 	if (colon == NULL || strchr(colon + 1, ':') == NULL)
33358771450SJilles Tjoelker 		goto bad;
33458771450SJilles Tjoelker 	fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
33558771450SJilles Tjoelker 	    "%Y-%m-%d %H:%M:%S";
33658771450SJilles Tjoelker 	p = strptime(arg, fmt, &t);
33758771450SJilles Tjoelker 	if (p == NULL)
33858771450SJilles Tjoelker 		goto bad;
33958771450SJilles Tjoelker 	/* POSIX: must have at least one digit after dot */
34058771450SJilles Tjoelker 	if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
34158771450SJilles Tjoelker 		p++;
3425faf2ae1SJilles Tjoelker 		val = 100000000;
34358771450SJilles Tjoelker 		while (isdigit((unsigned char)*p)) {
3445faf2ae1SJilles Tjoelker 			tvp[0].tv_nsec += val * (*p - '0');
34558771450SJilles Tjoelker 			p++;
34658771450SJilles Tjoelker 			val /= 10;
34758771450SJilles Tjoelker 		}
34858771450SJilles Tjoelker 	}
34958771450SJilles Tjoelker 	if (*p == 'Z') {
35058771450SJilles Tjoelker 		isutc = 1;
35158771450SJilles Tjoelker 		p++;
35258771450SJilles Tjoelker 	}
35358771450SJilles Tjoelker 	if (*p != '\0')
35458771450SJilles Tjoelker 		goto bad;
35558771450SJilles Tjoelker 
35658771450SJilles Tjoelker 	tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
35758771450SJilles Tjoelker 
35858771450SJilles Tjoelker 	tvp[1] = tvp[0];
35958771450SJilles Tjoelker 	return;
36058771450SJilles Tjoelker 
36158771450SJilles Tjoelker bad:
36258771450SJilles Tjoelker 	errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
36358771450SJilles Tjoelker }
36458771450SJilles Tjoelker 
3653481910dSGreg Lehey /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
3663481910dSGreg Lehey int
3674145c5f1SEitan Adler timeoffset(const char *arg)
3683481910dSGreg Lehey {
3693481910dSGreg Lehey 	int offset;
3703481910dSGreg Lehey 	int isneg;
3713481910dSGreg Lehey 
3723481910dSGreg Lehey 	offset = 0;
3733481910dSGreg Lehey 	isneg = *arg == '-';
3743481910dSGreg Lehey 	if (isneg)
3753481910dSGreg Lehey 		arg++;
3763481910dSGreg Lehey 	switch (strlen(arg)) {
3773481910dSGreg Lehey 	default:				/* invalid */
3783481910dSGreg Lehey 		errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
3793481910dSGreg Lehey 
3803481910dSGreg Lehey 	case 6:					/* HHMMSS */
3813481910dSGreg Lehey 		offset = ATOI2(arg);
3823481910dSGreg Lehey 		/* FALLTHROUGH */
3833481910dSGreg Lehey 	case 4:					/* MMSS */
3843481910dSGreg Lehey 		offset = offset * 60 + ATOI2(arg);
3853481910dSGreg Lehey 		/* FALLTHROUGH */
3863481910dSGreg Lehey 	case 2:					/* SS */
3873481910dSGreg Lehey 		offset = offset * 60 + ATOI2(arg);
3883481910dSGreg Lehey 	}
3893481910dSGreg Lehey 	if (isneg)
3903481910dSGreg Lehey 		return (-offset);
3913481910dSGreg Lehey 	else
3923481910dSGreg Lehey 		return (offset);
3933481910dSGreg Lehey }
3943481910dSGreg Lehey 
3954145c5f1SEitan Adler static void
3965faf2ae1SJilles Tjoelker stime_file(const char *fname, struct timespec *tsp)
3979b50d902SRodney W. Grimes {
3989b50d902SRodney W. Grimes 	struct stat sb;
3999b50d902SRodney W. Grimes 
4009b50d902SRodney W. Grimes 	if (stat(fname, &sb))
4019b50d902SRodney W. Grimes 		err(1, "%s", fname);
4025faf2ae1SJilles Tjoelker 	tsp[0] = sb.st_atim;
4035faf2ae1SJilles Tjoelker 	tsp[1] = sb.st_mtim;
4049b50d902SRodney W. Grimes }
4059b50d902SRodney W. Grimes 
4064145c5f1SEitan Adler static void
407b268ae64SEitan Adler usage(const char *myname)
4089b50d902SRodney W. Grimes {
40958771450SJilles Tjoelker 	fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
41058771450SJilles Tjoelker 		"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
41158771450SJilles Tjoelker 		"       [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
41258771450SJilles Tjoelker 		"file ...\n", myname);
4139b50d902SRodney W. Grimes 	exit(1);
4149b50d902SRodney W. Grimes }
415