xref: /freebsd/usr.bin/touch/touch.c (revision 51492908f9e7e6f4901ca421980dada135ce4cc8)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
301a9e1c9dSMark Murray #include <sys/cdefs.h>
311a9e1c9dSMark Murray 
321a9e1c9dSMark Murray __FBSDID("$FreeBSD$");
331a9e1c9dSMark Murray 
349b50d902SRodney W. Grimes #ifndef lint
3516dd925fSKris Kennaway static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
381a9e1c9dSMark Murray #endif
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
4116dd925fSKris Kennaway static const char sccsid[] = "@(#)touch.c	8.1 (Berkeley) 6/6/93";
4216dd925fSKris Kennaway #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/types.h>
459b50d902SRodney W. Grimes #include <sys/stat.h>
469b50d902SRodney W. Grimes #include <sys/time.h>
479b50d902SRodney W. Grimes 
4858771450SJilles Tjoelker #include <ctype.h>
499b50d902SRodney W. Grimes #include <err.h>
509b50d902SRodney W. Grimes #include <errno.h>
519b50d902SRodney W. Grimes #include <fcntl.h>
521b54381bSGreg Lehey #include <libgen.h>
539b50d902SRodney W. Grimes #include <stdio.h>
549b50d902SRodney W. Grimes #include <stdlib.h>
559b50d902SRodney W. Grimes #include <string.h>
569b50d902SRodney W. Grimes #include <time.h>
579b50d902SRodney W. Grimes #include <unistd.h>
589b50d902SRodney W. Grimes 
595faf2ae1SJilles Tjoelker static void	stime_arg1(const char *, struct timespec *);
605faf2ae1SJilles Tjoelker static void	stime_arg2(const char *, int, struct timespec *);
615faf2ae1SJilles Tjoelker static void	stime_darg(const char *, struct timespec *);
625faf2ae1SJilles Tjoelker static void	stime_file(const char *, struct timespec *);
634145c5f1SEitan Adler static int	timeoffset(const char *);
64b268ae64SEitan Adler static void	usage(const char *);
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes int
67f4ac32deSDavid Malone main(int argc, char *argv[])
689b50d902SRodney W. Grimes {
699b50d902SRodney W. Grimes 	struct stat sb;
705faf2ae1SJilles Tjoelker 	struct timespec ts[2];
715faf2ae1SJilles Tjoelker 	int atflag;
72401c9fdaSJaakko Heinonen 	int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
739b50d902SRodney W. Grimes 	char *p;
743481910dSGreg Lehey 	char *myname;
759b50d902SRodney W. Grimes 
761b54381bSGreg Lehey 	myname = basename(argv[0]);
77401c9fdaSJaakko Heinonen 	Aflag = aflag = cflag = mflag = timeset = 0;
785faf2ae1SJilles Tjoelker 	atflag = 0;
79*51492908SJilles Tjoelker 	ts[0].tv_sec = ts[1].tv_sec = 0;
80*51492908SJilles Tjoelker 	ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
819b50d902SRodney W. Grimes 
8258771450SJilles Tjoelker 	while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
839b50d902SRodney W. Grimes 		switch(ch) {
843481910dSGreg Lehey 		case 'A':
853481910dSGreg Lehey 			Aflag = timeoffset(optarg);
863481910dSGreg Lehey 			break;
879b50d902SRodney W. Grimes 		case 'a':
889b50d902SRodney W. Grimes 			aflag = 1;
899b50d902SRodney W. Grimes 			break;
909b50d902SRodney W. Grimes 		case 'c':
919b50d902SRodney W. Grimes 			cflag = 1;
929b50d902SRodney W. Grimes 			break;
9358771450SJilles Tjoelker 		case 'd':
9458771450SJilles Tjoelker 			timeset = 1;
955faf2ae1SJilles Tjoelker 			stime_darg(optarg, ts);
9658771450SJilles Tjoelker 			break;
979b50d902SRodney W. Grimes 		case 'f':
98401c9fdaSJaakko Heinonen 			/* No-op for compatibility. */
999b50d902SRodney W. Grimes 			break;
100842d1c6cSDavid E. O'Brien 		case 'h':
101842d1c6cSDavid E. O'Brien 			cflag = 1;
1025faf2ae1SJilles Tjoelker 			atflag = AT_SYMLINK_NOFOLLOW;
103842d1c6cSDavid E. O'Brien 			break;
1049b50d902SRodney W. Grimes 		case 'm':
1059b50d902SRodney W. Grimes 			mflag = 1;
1069b50d902SRodney W. Grimes 			break;
1079b50d902SRodney W. Grimes 		case 'r':
1089b50d902SRodney W. Grimes 			timeset = 1;
1095faf2ae1SJilles Tjoelker 			stime_file(optarg, ts);
1109b50d902SRodney W. Grimes 			break;
1119b50d902SRodney W. Grimes 		case 't':
1129b50d902SRodney W. Grimes 			timeset = 1;
1135faf2ae1SJilles Tjoelker 			stime_arg1(optarg, ts);
1149b50d902SRodney W. Grimes 			break;
1159b50d902SRodney W. Grimes 		default:
1163481910dSGreg Lehey 			usage(myname);
1179b50d902SRodney W. Grimes 		}
1189b50d902SRodney W. Grimes 	argc -= optind;
1199b50d902SRodney W. Grimes 	argv += optind;
1209b50d902SRodney W. Grimes 
1211b54381bSGreg Lehey 	if (aflag == 0 && mflag == 0)
1229b50d902SRodney W. Grimes 		aflag = mflag = 1;
1239b50d902SRodney W. Grimes 
1241b54381bSGreg Lehey 	if (timeset) {
1251b54381bSGreg Lehey 		if (Aflag) {
1269b50d902SRodney W. Grimes 			/*
1271b54381bSGreg Lehey 			 * We're setting the time to an offset from a specified
1281b54381bSGreg Lehey 			 * time.  God knows why, but it means that we can set
1291b54381bSGreg Lehey 			 * that time once and for all here.
1309b50d902SRodney W. Grimes 			 */
1311b54381bSGreg Lehey 			if (aflag)
1325faf2ae1SJilles Tjoelker 				ts[0].tv_sec += Aflag;
1331b54381bSGreg Lehey 			if (mflag)
1345faf2ae1SJilles Tjoelker 				ts[1].tv_sec += Aflag;
1351b54381bSGreg Lehey 			Aflag = 0;		/* done our job */
1361b54381bSGreg Lehey 		}
1371b54381bSGreg Lehey 	} else {
1381b54381bSGreg Lehey 		/*
1391b54381bSGreg Lehey 		 * If no -r or -t flag, at least two operands, the first of
1401b54381bSGreg Lehey 		 * which is an 8 or 10 digit number, use the obsolete time
1411b54381bSGreg Lehey 		 * specification, otherwise use the current time.
1421b54381bSGreg Lehey 		 */
1431b54381bSGreg Lehey 		if (argc > 1) {
1441b54381bSGreg Lehey 			strtol(argv[0], &p, 10);
1459b50d902SRodney W. Grimes 			len = p - argv[0];
1469b50d902SRodney W. Grimes 			if (*p == '\0' && (len == 8 || len == 10)) {
1479b50d902SRodney W. Grimes 				timeset = 1;
1485faf2ae1SJilles Tjoelker 				stime_arg2(*argv++, len == 10, ts);
1499b50d902SRodney W. Grimes 			}
1509b50d902SRodney W. Grimes 		}
1511b54381bSGreg Lehey 		/* Both times default to the same. */
1525faf2ae1SJilles Tjoelker 		ts[1] = ts[0];
1531b54381bSGreg Lehey 	}
1549b50d902SRodney W. Grimes 
155*51492908SJilles Tjoelker 	if (!aflag)
156*51492908SJilles Tjoelker 		ts[0].tv_nsec = UTIME_OMIT;
157*51492908SJilles Tjoelker 	if (!mflag)
158*51492908SJilles Tjoelker 		ts[1].tv_nsec = UTIME_OMIT;
159*51492908SJilles Tjoelker 
1609b50d902SRodney W. Grimes 	if (*argv == NULL)
1613481910dSGreg Lehey 		usage(myname);
1629b50d902SRodney W. Grimes 
1631b54381bSGreg Lehey 	if (Aflag)
1641b54381bSGreg Lehey 		cflag = 1;
1651b54381bSGreg Lehey 
1669b50d902SRodney W. Grimes 	for (rval = 0; *argv; ++argv) {
1679b50d902SRodney W. Grimes 		/* See if the file exists. */
1685faf2ae1SJilles Tjoelker 		if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
169c64b097bSJaakko Heinonen 			if (errno != ENOENT) {
170c64b097bSJaakko Heinonen 				rval = 1;
171c64b097bSJaakko Heinonen 				warn("%s", *argv);
172c64b097bSJaakko Heinonen 				continue;
173c64b097bSJaakko Heinonen 			}
1749b50d902SRodney W. Grimes 			if (!cflag) {
1759b50d902SRodney W. Grimes 				/* Create the file. */
1769b50d902SRodney W. Grimes 				fd = open(*argv,
1779b50d902SRodney W. Grimes 				    O_WRONLY | O_CREAT, DEFFILEMODE);
1789b50d902SRodney W. Grimes 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
1799b50d902SRodney W. Grimes 					rval = 1;
1809b50d902SRodney W. Grimes 					warn("%s", *argv);
1819b50d902SRodney W. Grimes 					continue;
1829b50d902SRodney W. Grimes 				}
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 				/* If using the current time, we're done. */
1859b50d902SRodney W. Grimes 				if (!timeset)
1869b50d902SRodney W. Grimes 					continue;
1879b50d902SRodney W. Grimes 			} else
1889b50d902SRodney W. Grimes 				continue;
1899ef5c48bSBill Fumerola 		}
1909b50d902SRodney W. Grimes 
1911b54381bSGreg Lehey 		/*
1921b54381bSGreg Lehey 		 * We're adjusting the times based on the file times, not a
1931b54381bSGreg Lehey 		 * specified time (that gets handled above).
1941b54381bSGreg Lehey 		 */
1953481910dSGreg Lehey 		if (Aflag) {
1961b54381bSGreg Lehey 			if (aflag) {
1975faf2ae1SJilles Tjoelker 				ts[0] = sb.st_atim;
1985faf2ae1SJilles Tjoelker 				ts[0].tv_sec += Aflag;
1991b54381bSGreg Lehey 			}
2001b54381bSGreg Lehey 			if (mflag) {
2015faf2ae1SJilles Tjoelker 				ts[1] = sb.st_mtim;
2025faf2ae1SJilles Tjoelker 				ts[1].tv_sec += Aflag;
2033481910dSGreg Lehey 			}
2041b54381bSGreg Lehey 		}
2059b50d902SRodney W. Grimes 
2065faf2ae1SJilles Tjoelker 		if (!utimensat(AT_FDCWD, *argv, ts, atflag))
2079b50d902SRodney W. Grimes 			continue;
2089b50d902SRodney W. Grimes 
209c64b097bSJaakko Heinonen 		rval = 1;
2104d01a8fbSEric Melville 		warn("%s", *argv);
2119b50d902SRodney W. Grimes 	}
2129b50d902SRodney W. Grimes 	exit(rval);
2139b50d902SRodney W. Grimes }
2149b50d902SRodney W. Grimes 
2159b50d902SRodney W. Grimes #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
2169b50d902SRodney W. Grimes 
2174145c5f1SEitan Adler static void
2185faf2ae1SJilles Tjoelker stime_arg1(const char *arg, struct timespec *tvp)
2199b50d902SRodney W. Grimes {
22039470616SBruce Evans 	time_t now;
2219b50d902SRodney W. Grimes 	struct tm *t;
2229b50d902SRodney W. Grimes 	int yearset;
2239b50d902SRodney W. Grimes 	char *p;
224*51492908SJilles Tjoelker 
225*51492908SJilles Tjoelker 	now = time(NULL);
22639470616SBruce Evans 	if ((t = localtime(&now)) == NULL)
2279b50d902SRodney W. Grimes 		err(1, "localtime");
2289b50d902SRodney W. Grimes 					/* [[CC]YY]MMDDhhmm[.SS] */
2299b50d902SRodney W. Grimes 	if ((p = strchr(arg, '.')) == NULL)
2309b50d902SRodney W. Grimes 		t->tm_sec = 0;		/* Seconds defaults to 0. */
2319b50d902SRodney W. Grimes 	else {
2329b50d902SRodney W. Grimes 		if (strlen(p + 1) != 2)
2339b50d902SRodney W. Grimes 			goto terr;
2349b50d902SRodney W. Grimes 		*p++ = '\0';
2359b50d902SRodney W. Grimes 		t->tm_sec = ATOI2(p);
2369b50d902SRodney W. Grimes 	}
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes 	yearset = 0;
2399b50d902SRodney W. Grimes 	switch(strlen(arg)) {
2409b50d902SRodney W. Grimes 	case 12:			/* CCYYMMDDhhmm */
2419b50d902SRodney W. Grimes 		t->tm_year = ATOI2(arg);
242d38ca307SJoerg Wunsch 		t->tm_year *= 100;
2439b50d902SRodney W. Grimes 		yearset = 1;
24493b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2459b50d902SRodney W. Grimes 	case 10:			/* YYMMDDhhmm */
2469b50d902SRodney W. Grimes 		if (yearset) {
2479b50d902SRodney W. Grimes 			yearset = ATOI2(arg);
2489b50d902SRodney W. Grimes 			t->tm_year += yearset;
2499b50d902SRodney W. Grimes 		} else {
2509b50d902SRodney W. Grimes 			yearset = ATOI2(arg);
2519b50d902SRodney W. Grimes 			if (yearset < 69)
2529b50d902SRodney W. Grimes 				t->tm_year = yearset + 2000;
2539b50d902SRodney W. Grimes 			else
2549b50d902SRodney W. Grimes 				t->tm_year = yearset + 1900;
2559b50d902SRodney W. Grimes 		}
2569b50d902SRodney W. Grimes 		t->tm_year -= 1900;	/* Convert to UNIX time. */
2579b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2589b50d902SRodney W. Grimes 	case 8:				/* MMDDhhmm */
2599b50d902SRodney W. Grimes 		t->tm_mon = ATOI2(arg);
2609b50d902SRodney W. Grimes 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
2619b50d902SRodney W. Grimes 		t->tm_mday = ATOI2(arg);
2629b50d902SRodney W. Grimes 		t->tm_hour = ATOI2(arg);
2639b50d902SRodney W. Grimes 		t->tm_min = ATOI2(arg);
2649b50d902SRodney W. Grimes 		break;
2659b50d902SRodney W. Grimes 	default:
2669b50d902SRodney W. Grimes 		goto terr;
2679b50d902SRodney W. Grimes 	}
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes 	t->tm_isdst = -1;		/* Figure out DST. */
2709b50d902SRodney W. Grimes 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
2719b50d902SRodney W. Grimes 	if (tvp[0].tv_sec == -1)
2724145c5f1SEitan Adler 		goto terr;
2739b50d902SRodney W. Grimes 
2745faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
2754145c5f1SEitan Adler 	return;
2764145c5f1SEitan Adler 
2774145c5f1SEitan Adler terr:
2784145c5f1SEitan Adler 	errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
2799b50d902SRodney W. Grimes }
2809b50d902SRodney W. Grimes 
2814145c5f1SEitan Adler static void
2825faf2ae1SJilles Tjoelker stime_arg2(const char *arg, int year, struct timespec *tvp)
2839b50d902SRodney W. Grimes {
28439470616SBruce Evans 	time_t now;
2859b50d902SRodney W. Grimes 	struct tm *t;
286*51492908SJilles Tjoelker 
287*51492908SJilles Tjoelker 	now = time(NULL);
28839470616SBruce Evans 	if ((t = localtime(&now)) == NULL)
2899b50d902SRodney W. Grimes 		err(1, "localtime");
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
2929b50d902SRodney W. Grimes 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
2939b50d902SRodney W. Grimes 	t->tm_mday = ATOI2(arg);
2949b50d902SRodney W. Grimes 	t->tm_hour = ATOI2(arg);
2959b50d902SRodney W. Grimes 	t->tm_min = ATOI2(arg);
29634c7ff49SDaniel O'Callaghan 	if (year) {
2979b50d902SRodney W. Grimes 		t->tm_year = ATOI2(arg);
29825c4d008SDaniel O'Callaghan 		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
29934c7ff49SDaniel O'Callaghan 			t->tm_year += 100;
30034c7ff49SDaniel O'Callaghan 	}
3019b50d902SRodney W. Grimes 
3029b50d902SRodney W. Grimes 	t->tm_isdst = -1;		/* Figure out DST. */
3039b50d902SRodney W. Grimes 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
3049b50d902SRodney W. Grimes 	if (tvp[0].tv_sec == -1)
3059b50d902SRodney W. Grimes 		errx(1,
3069b50d902SRodney W. Grimes 	"out of range or illegal time specification: MMDDhhmm[yy]");
3079b50d902SRodney W. Grimes 
3085faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
3099b50d902SRodney W. Grimes }
3109b50d902SRodney W. Grimes 
3114145c5f1SEitan Adler static void
3125faf2ae1SJilles Tjoelker stime_darg(const char *arg, struct timespec *tvp)
31358771450SJilles Tjoelker {
31458771450SJilles Tjoelker 	struct tm t = { .tm_sec = 0 };
31558771450SJilles Tjoelker 	const char *fmt, *colon;
31658771450SJilles Tjoelker 	char *p;
31758771450SJilles Tjoelker 	int val, isutc = 0;
31858771450SJilles Tjoelker 
3195faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = 0;
32058771450SJilles Tjoelker 	t.tm_isdst = -1;
32158771450SJilles Tjoelker 	colon = strchr(arg, ':');
32258771450SJilles Tjoelker 	if (colon == NULL || strchr(colon + 1, ':') == NULL)
32358771450SJilles Tjoelker 		goto bad;
32458771450SJilles Tjoelker 	fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
32558771450SJilles Tjoelker 	    "%Y-%m-%d %H:%M:%S";
32658771450SJilles Tjoelker 	p = strptime(arg, fmt, &t);
32758771450SJilles Tjoelker 	if (p == NULL)
32858771450SJilles Tjoelker 		goto bad;
32958771450SJilles Tjoelker 	/* POSIX: must have at least one digit after dot */
33058771450SJilles Tjoelker 	if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
33158771450SJilles Tjoelker 		p++;
3325faf2ae1SJilles Tjoelker 		val = 100000000;
33358771450SJilles Tjoelker 		while (isdigit((unsigned char)*p)) {
3345faf2ae1SJilles Tjoelker 			tvp[0].tv_nsec += val * (*p - '0');
33558771450SJilles Tjoelker 			p++;
33658771450SJilles Tjoelker 			val /= 10;
33758771450SJilles Tjoelker 		}
33858771450SJilles Tjoelker 	}
33958771450SJilles Tjoelker 	if (*p == 'Z') {
34058771450SJilles Tjoelker 		isutc = 1;
34158771450SJilles Tjoelker 		p++;
34258771450SJilles Tjoelker 	}
34358771450SJilles Tjoelker 	if (*p != '\0')
34458771450SJilles Tjoelker 		goto bad;
34558771450SJilles Tjoelker 
34658771450SJilles Tjoelker 	tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
34758771450SJilles Tjoelker 
34858771450SJilles Tjoelker 	tvp[1] = tvp[0];
34958771450SJilles Tjoelker 	return;
35058771450SJilles Tjoelker 
35158771450SJilles Tjoelker bad:
35258771450SJilles Tjoelker 	errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
35358771450SJilles Tjoelker }
35458771450SJilles Tjoelker 
3553481910dSGreg Lehey /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
3563481910dSGreg Lehey int
3574145c5f1SEitan Adler timeoffset(const char *arg)
3583481910dSGreg Lehey {
3593481910dSGreg Lehey 	int offset;
3603481910dSGreg Lehey 	int isneg;
3613481910dSGreg Lehey 
3623481910dSGreg Lehey 	offset = 0;
3633481910dSGreg Lehey 	isneg = *arg == '-';
3643481910dSGreg Lehey 	if (isneg)
3653481910dSGreg Lehey 		arg++;
3663481910dSGreg Lehey 	switch (strlen(arg)) {
3673481910dSGreg Lehey 	default:				/* invalid */
3683481910dSGreg Lehey 		errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
3693481910dSGreg Lehey 
3703481910dSGreg Lehey 	case 6:					/* HHMMSS */
3713481910dSGreg Lehey 		offset = ATOI2(arg);
3723481910dSGreg Lehey 		/* FALLTHROUGH */
3733481910dSGreg Lehey 	case 4:					/* MMSS */
3743481910dSGreg Lehey 		offset = offset * 60 + ATOI2(arg);
3753481910dSGreg Lehey 		/* FALLTHROUGH */
3763481910dSGreg Lehey 	case 2:					/* SS */
3773481910dSGreg Lehey 		offset = offset * 60 + ATOI2(arg);
3783481910dSGreg Lehey 	}
3793481910dSGreg Lehey 	if (isneg)
3803481910dSGreg Lehey 		return (-offset);
3813481910dSGreg Lehey 	else
3823481910dSGreg Lehey 		return (offset);
3833481910dSGreg Lehey }
3843481910dSGreg Lehey 
3854145c5f1SEitan Adler static void
3865faf2ae1SJilles Tjoelker stime_file(const char *fname, struct timespec *tsp)
3879b50d902SRodney W. Grimes {
3889b50d902SRodney W. Grimes 	struct stat sb;
3899b50d902SRodney W. Grimes 
3909b50d902SRodney W. Grimes 	if (stat(fname, &sb))
3919b50d902SRodney W. Grimes 		err(1, "%s", fname);
3925faf2ae1SJilles Tjoelker 	tsp[0] = sb.st_atim;
3935faf2ae1SJilles Tjoelker 	tsp[1] = sb.st_mtim;
3949b50d902SRodney W. Grimes }
3959b50d902SRodney W. Grimes 
3964145c5f1SEitan Adler static void
397b268ae64SEitan Adler usage(const char *myname)
3989b50d902SRodney W. Grimes {
39958771450SJilles Tjoelker 	fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
40058771450SJilles Tjoelker 		"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
40158771450SJilles Tjoelker 		"       [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
40258771450SJilles Tjoelker 		"file ...\n", myname);
4039b50d902SRodney W. Grimes 	exit(1);
4049b50d902SRodney W. Grimes }
405