xref: /freebsd/usr.bin/touch/touch.c (revision aa69e0f212630bd6d4ec1c3c54e117be16653e8a)
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 
329b50d902SRodney W. Grimes #include <sys/types.h>
339b50d902SRodney W. Grimes #include <sys/stat.h>
349b50d902SRodney W. Grimes #include <sys/time.h>
359b50d902SRodney W. Grimes 
3658771450SJilles Tjoelker #include <ctype.h>
379b50d902SRodney W. Grimes #include <err.h>
389b50d902SRodney W. Grimes #include <errno.h>
399b50d902SRodney W. Grimes #include <fcntl.h>
401b54381bSGreg Lehey #include <libgen.h>
419b50d902SRodney W. Grimes #include <stdio.h>
429b50d902SRodney W. Grimes #include <stdlib.h>
439b50d902SRodney W. Grimes #include <string.h>
449b50d902SRodney W. Grimes #include <time.h>
459b50d902SRodney W. Grimes #include <unistd.h>
469b50d902SRodney W. Grimes 
475faf2ae1SJilles Tjoelker static void	stime_arg1(const char *, struct timespec *);
485faf2ae1SJilles Tjoelker static void	stime_arg2(const char *, int, struct timespec *);
495faf2ae1SJilles Tjoelker static void	stime_darg(const char *, struct timespec *);
505faf2ae1SJilles Tjoelker static void	stime_file(const char *, struct timespec *);
514145c5f1SEitan Adler static int	timeoffset(const char *);
52b268ae64SEitan Adler static void	usage(const char *);
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes int
main(int argc,char * argv[])55f4ac32deSDavid Malone main(int argc, char *argv[])
569b50d902SRodney W. Grimes {
579b50d902SRodney W. Grimes 	struct stat sb;
585faf2ae1SJilles Tjoelker 	struct timespec ts[2];
595faf2ae1SJilles Tjoelker 	int atflag;
60401c9fdaSJaakko Heinonen 	int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
619b50d902SRodney W. Grimes 	char *p;
623481910dSGreg Lehey 	char *myname;
639b50d902SRodney W. Grimes 
641b54381bSGreg Lehey 	myname = basename(argv[0]);
65401c9fdaSJaakko Heinonen 	Aflag = aflag = cflag = mflag = timeset = 0;
665faf2ae1SJilles Tjoelker 	atflag = 0;
6751492908SJilles Tjoelker 	ts[0].tv_sec = ts[1].tv_sec = 0;
6851492908SJilles Tjoelker 	ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
699b50d902SRodney W. Grimes 
7058771450SJilles Tjoelker 	while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
719b50d902SRodney W. Grimes 		switch(ch) {
723481910dSGreg Lehey 		case 'A':
733481910dSGreg Lehey 			Aflag = timeoffset(optarg);
743481910dSGreg Lehey 			break;
759b50d902SRodney W. Grimes 		case 'a':
769b50d902SRodney W. Grimes 			aflag = 1;
779b50d902SRodney W. Grimes 			break;
789b50d902SRodney W. Grimes 		case 'c':
799b50d902SRodney W. Grimes 			cflag = 1;
809b50d902SRodney W. Grimes 			break;
8158771450SJilles Tjoelker 		case 'd':
8258771450SJilles Tjoelker 			timeset = 1;
835faf2ae1SJilles Tjoelker 			stime_darg(optarg, ts);
8458771450SJilles Tjoelker 			break;
859b50d902SRodney W. Grimes 		case 'f':
86401c9fdaSJaakko Heinonen 			/* No-op for compatibility. */
879b50d902SRodney W. Grimes 			break;
88842d1c6cSDavid E. O'Brien 		case 'h':
89842d1c6cSDavid E. O'Brien 			cflag = 1;
905faf2ae1SJilles Tjoelker 			atflag = AT_SYMLINK_NOFOLLOW;
91842d1c6cSDavid E. O'Brien 			break;
929b50d902SRodney W. Grimes 		case 'm':
939b50d902SRodney W. Grimes 			mflag = 1;
949b50d902SRodney W. Grimes 			break;
959b50d902SRodney W. Grimes 		case 'r':
969b50d902SRodney W. Grimes 			timeset = 1;
975faf2ae1SJilles Tjoelker 			stime_file(optarg, ts);
989b50d902SRodney W. Grimes 			break;
999b50d902SRodney W. Grimes 		case 't':
1009b50d902SRodney W. Grimes 			timeset = 1;
1015faf2ae1SJilles Tjoelker 			stime_arg1(optarg, ts);
1029b50d902SRodney W. Grimes 			break;
1039b50d902SRodney W. Grimes 		default:
1043481910dSGreg Lehey 			usage(myname);
1059b50d902SRodney W. Grimes 		}
1069b50d902SRodney W. Grimes 	argc -= optind;
1079b50d902SRodney W. Grimes 	argv += optind;
1089b50d902SRodney W. Grimes 
1091b54381bSGreg Lehey 	if (aflag == 0 && mflag == 0)
1109b50d902SRodney W. Grimes 		aflag = mflag = 1;
1119b50d902SRodney W. Grimes 
1121b54381bSGreg Lehey 	if (timeset) {
1131b54381bSGreg Lehey 		if (Aflag) {
1149b50d902SRodney W. Grimes 			/*
1151b54381bSGreg Lehey 			 * We're setting the time to an offset from a specified
1161b54381bSGreg Lehey 			 * time.  God knows why, but it means that we can set
1171b54381bSGreg Lehey 			 * that time once and for all here.
1189b50d902SRodney W. Grimes 			 */
1191b54381bSGreg Lehey 			if (aflag)
1205faf2ae1SJilles Tjoelker 				ts[0].tv_sec += Aflag;
1211b54381bSGreg Lehey 			if (mflag)
1225faf2ae1SJilles Tjoelker 				ts[1].tv_sec += Aflag;
1231b54381bSGreg Lehey 			Aflag = 0;		/* done our job */
1241b54381bSGreg Lehey 		}
1251b54381bSGreg Lehey 	} else {
1261b54381bSGreg Lehey 		/*
1271b54381bSGreg Lehey 		 * If no -r or -t flag, at least two operands, the first of
1281b54381bSGreg Lehey 		 * which is an 8 or 10 digit number, use the obsolete time
1291b54381bSGreg Lehey 		 * specification, otherwise use the current time.
1301b54381bSGreg Lehey 		 */
1311b54381bSGreg Lehey 		if (argc > 1) {
1321b54381bSGreg Lehey 			strtol(argv[0], &p, 10);
1339b50d902SRodney W. Grimes 			len = p - argv[0];
1349b50d902SRodney W. Grimes 			if (*p == '\0' && (len == 8 || len == 10)) {
1359b50d902SRodney W. Grimes 				timeset = 1;
1365faf2ae1SJilles Tjoelker 				stime_arg2(*argv++, len == 10, ts);
1379b50d902SRodney W. Grimes 			}
1389b50d902SRodney W. Grimes 		}
1391b54381bSGreg Lehey 		/* Both times default to the same. */
1405faf2ae1SJilles Tjoelker 		ts[1] = ts[0];
1411b54381bSGreg Lehey 	}
1429b50d902SRodney W. Grimes 
14351492908SJilles Tjoelker 	if (!aflag)
14451492908SJilles Tjoelker 		ts[0].tv_nsec = UTIME_OMIT;
14551492908SJilles Tjoelker 	if (!mflag)
14651492908SJilles Tjoelker 		ts[1].tv_nsec = UTIME_OMIT;
14751492908SJilles Tjoelker 
1489b50d902SRodney W. Grimes 	if (*argv == NULL)
1493481910dSGreg Lehey 		usage(myname);
1509b50d902SRodney W. Grimes 
1511b54381bSGreg Lehey 	if (Aflag)
1521b54381bSGreg Lehey 		cflag = 1;
1531b54381bSGreg Lehey 
1549b50d902SRodney W. Grimes 	for (rval = 0; *argv; ++argv) {
1559b50d902SRodney W. Grimes 		/* See if the file exists. */
1565faf2ae1SJilles Tjoelker 		if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
157c64b097bSJaakko Heinonen 			if (errno != ENOENT) {
158c64b097bSJaakko Heinonen 				rval = 1;
159c64b097bSJaakko Heinonen 				warn("%s", *argv);
160c64b097bSJaakko Heinonen 				continue;
161c64b097bSJaakko Heinonen 			}
1629b50d902SRodney W. Grimes 			if (!cflag) {
1639b50d902SRodney W. Grimes 				/* Create the file. */
1649b50d902SRodney W. Grimes 				fd = open(*argv,
1659b50d902SRodney W. Grimes 				    O_WRONLY | O_CREAT, DEFFILEMODE);
166cb54c500SMariusz Zaborski 				if (fd == -1) {
1679b50d902SRodney W. Grimes 					rval = 1;
1689b50d902SRodney W. Grimes 					warn("%s", *argv);
1699b50d902SRodney W. Grimes 					continue;
1709b50d902SRodney W. Grimes 				}
171cb54c500SMariusz Zaborski 				if (fstat(fd, &sb) < 0) {
172cb54c500SMariusz Zaborski 					warn("%s", *argv);
173cb54c500SMariusz Zaborski 					rval = 1;
174cb54c500SMariusz Zaborski 				}
175cb54c500SMariusz Zaborski 				if (close(fd) < 0) {
176cb54c500SMariusz Zaborski 					warn("%s", *argv);
177cb54c500SMariusz Zaborski 					rval = 1;
178cb54c500SMariusz Zaborski 				}
1799b50d902SRodney W. Grimes 
1809b50d902SRodney W. Grimes 				/* If using the current time, we're done. */
1819b50d902SRodney W. Grimes 				if (!timeset)
1829b50d902SRodney W. Grimes 					continue;
1839b50d902SRodney W. Grimes 			} else
1849b50d902SRodney W. Grimes 				continue;
1859ef5c48bSBill Fumerola 		}
1869b50d902SRodney W. Grimes 
1871b54381bSGreg Lehey 		/*
1881b54381bSGreg Lehey 		 * We're adjusting the times based on the file times, not a
1891b54381bSGreg Lehey 		 * specified time (that gets handled above).
1901b54381bSGreg Lehey 		 */
1913481910dSGreg Lehey 		if (Aflag) {
1921b54381bSGreg Lehey 			if (aflag) {
1935faf2ae1SJilles Tjoelker 				ts[0] = sb.st_atim;
1945faf2ae1SJilles Tjoelker 				ts[0].tv_sec += Aflag;
1951b54381bSGreg Lehey 			}
1961b54381bSGreg Lehey 			if (mflag) {
1975faf2ae1SJilles Tjoelker 				ts[1] = sb.st_mtim;
1985faf2ae1SJilles Tjoelker 				ts[1].tv_sec += Aflag;
1993481910dSGreg Lehey 			}
2001b54381bSGreg Lehey 		}
2019b50d902SRodney W. Grimes 
2025faf2ae1SJilles Tjoelker 		if (!utimensat(AT_FDCWD, *argv, ts, atflag))
2039b50d902SRodney W. Grimes 			continue;
2049b50d902SRodney W. Grimes 
205c64b097bSJaakko Heinonen 		rval = 1;
2064d01a8fbSEric Melville 		warn("%s", *argv);
2079b50d902SRodney W. Grimes 	}
2089b50d902SRodney W. Grimes 	exit(rval);
2099b50d902SRodney W. Grimes }
2109b50d902SRodney W. Grimes 
2119b50d902SRodney W. Grimes #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
2129b50d902SRodney W. Grimes 
2134145c5f1SEitan Adler static void
stime_arg1(const char * arg,struct timespec * tvp)2145faf2ae1SJilles Tjoelker stime_arg1(const char *arg, struct timespec *tvp)
2159b50d902SRodney W. Grimes {
21639470616SBruce Evans 	time_t now;
2179b50d902SRodney W. Grimes 	struct tm *t;
2189b50d902SRodney W. Grimes 	int yearset;
2199b50d902SRodney W. Grimes 	char *p;
22051492908SJilles Tjoelker 
22151492908SJilles Tjoelker 	now = time(NULL);
22239470616SBruce Evans 	if ((t = localtime(&now)) == NULL)
2239b50d902SRodney W. Grimes 		err(1, "localtime");
2249b50d902SRodney W. Grimes 					/* [[CC]YY]MMDDhhmm[.SS] */
2259b50d902SRodney W. Grimes 	if ((p = strchr(arg, '.')) == NULL)
2269b50d902SRodney W. Grimes 		t->tm_sec = 0;		/* Seconds defaults to 0. */
2279b50d902SRodney W. Grimes 	else {
2289b50d902SRodney W. Grimes 		if (strlen(p + 1) != 2)
2299b50d902SRodney W. Grimes 			goto terr;
2309b50d902SRodney W. Grimes 		*p++ = '\0';
2319b50d902SRodney W. Grimes 		t->tm_sec = ATOI2(p);
2329b50d902SRodney W. Grimes 	}
2339b50d902SRodney W. Grimes 
2349b50d902SRodney W. Grimes 	yearset = 0;
2359b50d902SRodney W. Grimes 	switch (strlen(arg)) {
2369b50d902SRodney W. Grimes 	case 12:			/* CCYYMMDDhhmm */
2379b50d902SRodney W. Grimes 		t->tm_year = ATOI2(arg);
238d38ca307SJoerg Wunsch 		t->tm_year *= 100;
2399b50d902SRodney W. Grimes 		yearset = 1;
24093b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2419b50d902SRodney W. Grimes 	case 10:			/* YYMMDDhhmm */
2429b50d902SRodney W. Grimes 		if (yearset) {
2439b50d902SRodney W. Grimes 			yearset = ATOI2(arg);
2449b50d902SRodney W. Grimes 			t->tm_year += yearset;
2459b50d902SRodney W. Grimes 		} else {
2469b50d902SRodney W. Grimes 			yearset = ATOI2(arg);
2479b50d902SRodney W. Grimes 			if (yearset < 69)
2489b50d902SRodney W. Grimes 				t->tm_year = yearset + 2000;
2499b50d902SRodney W. Grimes 			else
2509b50d902SRodney W. Grimes 				t->tm_year = yearset + 1900;
2519b50d902SRodney W. Grimes 		}
2529b50d902SRodney W. Grimes 		t->tm_year -= 1900;	/* Convert to UNIX time. */
2539b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2549b50d902SRodney W. Grimes 	case 8:				/* MMDDhhmm */
2559b50d902SRodney W. Grimes 		t->tm_mon = ATOI2(arg);
2569b50d902SRodney W. Grimes 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
2579b50d902SRodney W. Grimes 		t->tm_mday = ATOI2(arg);
2589b50d902SRodney W. Grimes 		t->tm_hour = ATOI2(arg);
2599b50d902SRodney W. Grimes 		t->tm_min = ATOI2(arg);
2609b50d902SRodney W. Grimes 		break;
2619b50d902SRodney W. Grimes 	default:
2629b50d902SRodney W. Grimes 		goto terr;
2639b50d902SRodney W. Grimes 	}
2649b50d902SRodney W. Grimes 
2659b50d902SRodney W. Grimes 	t->tm_isdst = -1;		/* Figure out DST. */
266*aa69e0f2SDag-Erling Smørgrav 	t->tm_yday = -1;
2679b50d902SRodney W. Grimes 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
268*aa69e0f2SDag-Erling Smørgrav 	if (t->tm_yday == -1)
2694145c5f1SEitan Adler 		goto terr;
2709b50d902SRodney W. Grimes 
2715faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
2724145c5f1SEitan Adler 	return;
2734145c5f1SEitan Adler 
2744145c5f1SEitan Adler terr:
275*aa69e0f2SDag-Erling Smørgrav 	errx(1, "out of range or illegal time specification: "
276*aa69e0f2SDag-Erling Smørgrav 	    "[[CC]YY]MMDDhhmm[.SS]");
2779b50d902SRodney W. Grimes }
2789b50d902SRodney W. Grimes 
2794145c5f1SEitan Adler static void
stime_arg2(const char * arg,int year,struct timespec * tvp)2805faf2ae1SJilles Tjoelker stime_arg2(const char *arg, int year, struct timespec *tvp)
2819b50d902SRodney W. Grimes {
28239470616SBruce Evans 	time_t now;
2839b50d902SRodney W. Grimes 	struct tm *t;
28451492908SJilles Tjoelker 
28551492908SJilles Tjoelker 	now = time(NULL);
28639470616SBruce Evans 	if ((t = localtime(&now)) == NULL)
2879b50d902SRodney W. Grimes 		err(1, "localtime");
2889b50d902SRodney W. Grimes 
2899b50d902SRodney W. Grimes 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
2909b50d902SRodney W. Grimes 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
2919b50d902SRodney W. Grimes 	t->tm_mday = ATOI2(arg);
2929b50d902SRodney W. Grimes 	t->tm_hour = ATOI2(arg);
2939b50d902SRodney W. Grimes 	t->tm_min = ATOI2(arg);
29434c7ff49SDaniel O'Callaghan 	if (year) {
2959b50d902SRodney W. Grimes 		t->tm_year = ATOI2(arg);
29625c4d008SDaniel O'Callaghan 		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
29734c7ff49SDaniel O'Callaghan 			t->tm_year += 100;
29834c7ff49SDaniel O'Callaghan 	}
2999b50d902SRodney W. Grimes 
3009b50d902SRodney W. Grimes 	t->tm_isdst = -1;		/* Figure out DST. */
301*aa69e0f2SDag-Erling Smørgrav 	t->tm_yday = -1;
3029b50d902SRodney W. Grimes 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
303*aa69e0f2SDag-Erling Smørgrav 	if (t->tm_yday == -1)
304*aa69e0f2SDag-Erling Smørgrav 		errx(1, "out of range or illegal time specification: "
305*aa69e0f2SDag-Erling Smørgrav 		    "MMDDhhmm[yy]");
3069b50d902SRodney W. Grimes 
3075faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
3089b50d902SRodney W. Grimes }
3099b50d902SRodney W. Grimes 
3104145c5f1SEitan Adler static void
stime_darg(const char * arg,struct timespec * tvp)3115faf2ae1SJilles Tjoelker stime_darg(const char *arg, struct timespec *tvp)
31258771450SJilles Tjoelker {
31358771450SJilles Tjoelker 	struct tm t = { .tm_sec = 0 };
31458771450SJilles Tjoelker 	const char *fmt, *colon;
31558771450SJilles Tjoelker 	char *p;
31658771450SJilles Tjoelker 	int val, isutc = 0;
31758771450SJilles Tjoelker 
3185faf2ae1SJilles Tjoelker 	tvp[0].tv_nsec = 0;
31958771450SJilles Tjoelker 	t.tm_isdst = -1;
32058771450SJilles Tjoelker 	colon = strchr(arg, ':');
32158771450SJilles Tjoelker 	if (colon == NULL || strchr(colon + 1, ':') == NULL)
32258771450SJilles Tjoelker 		goto bad;
32358771450SJilles Tjoelker 	fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
32458771450SJilles Tjoelker 	    "%Y-%m-%d %H:%M:%S";
32558771450SJilles Tjoelker 	p = strptime(arg, fmt, &t);
32658771450SJilles Tjoelker 	if (p == NULL)
32758771450SJilles Tjoelker 		goto bad;
32858771450SJilles Tjoelker 	/* POSIX: must have at least one digit after dot */
32958771450SJilles Tjoelker 	if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
33058771450SJilles Tjoelker 		p++;
3315faf2ae1SJilles Tjoelker 		val = 100000000;
33258771450SJilles Tjoelker 		while (isdigit((unsigned char)*p)) {
3335faf2ae1SJilles Tjoelker 			tvp[0].tv_nsec += val * (*p - '0');
33458771450SJilles Tjoelker 			p++;
33558771450SJilles Tjoelker 			val /= 10;
33658771450SJilles Tjoelker 		}
33758771450SJilles Tjoelker 	}
33858771450SJilles Tjoelker 	if (*p == 'Z') {
33958771450SJilles Tjoelker 		isutc = 1;
34058771450SJilles Tjoelker 		p++;
34158771450SJilles Tjoelker 	}
34258771450SJilles Tjoelker 	if (*p != '\0')
34358771450SJilles Tjoelker 		goto bad;
34458771450SJilles Tjoelker 
345*aa69e0f2SDag-Erling Smørgrav 	t.tm_yday = -1;
34658771450SJilles Tjoelker 	tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
347*aa69e0f2SDag-Erling Smørgrav 	if (t.tm_yday == -1)
348*aa69e0f2SDag-Erling Smørgrav 		goto bad;
34958771450SJilles Tjoelker 
35058771450SJilles Tjoelker 	tvp[1] = tvp[0];
35158771450SJilles Tjoelker 	return;
35258771450SJilles Tjoelker 
35358771450SJilles Tjoelker bad:
354*aa69e0f2SDag-Erling Smørgrav 	errx(1, "out of range or illegal time specification: "
355*aa69e0f2SDag-Erling Smørgrav 	    "YYYY-MM-DDThh:mm:SS[.frac][tz]");
35658771450SJilles Tjoelker }
35758771450SJilles Tjoelker 
3583481910dSGreg Lehey /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
3593481910dSGreg Lehey int
timeoffset(const char * arg)3604145c5f1SEitan Adler timeoffset(const char *arg)
3613481910dSGreg Lehey {
3623481910dSGreg Lehey 	int offset;
3633481910dSGreg Lehey 	int isneg;
3643481910dSGreg Lehey 
3653481910dSGreg Lehey 	offset = 0;
3663481910dSGreg Lehey 	isneg = *arg == '-';
3673481910dSGreg Lehey 	if (isneg)
3683481910dSGreg Lehey 		arg++;
3693481910dSGreg Lehey 	switch (strlen(arg)) {
3703481910dSGreg Lehey 	default:				/* invalid */
3713481910dSGreg Lehey 		errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
3723481910dSGreg Lehey 
3733481910dSGreg Lehey 	case 6:					/* HHMMSS */
3743481910dSGreg Lehey 		offset = ATOI2(arg);
3753481910dSGreg Lehey 		/* FALLTHROUGH */
3763481910dSGreg Lehey 	case 4:					/* MMSS */
3773481910dSGreg Lehey 		offset = offset * 60 + ATOI2(arg);
3783481910dSGreg Lehey 		/* FALLTHROUGH */
3793481910dSGreg Lehey 	case 2:					/* SS */
3803481910dSGreg Lehey 		offset = offset * 60 + ATOI2(arg);
3813481910dSGreg Lehey 	}
3823481910dSGreg Lehey 	if (isneg)
3833481910dSGreg Lehey 		return (-offset);
3843481910dSGreg Lehey 	else
3853481910dSGreg Lehey 		return (offset);
3863481910dSGreg Lehey }
3873481910dSGreg Lehey 
3884145c5f1SEitan Adler static void
stime_file(const char * fname,struct timespec * tsp)3895faf2ae1SJilles Tjoelker stime_file(const char *fname, struct timespec *tsp)
3909b50d902SRodney W. Grimes {
3919b50d902SRodney W. Grimes 	struct stat sb;
3929b50d902SRodney W. Grimes 
3939b50d902SRodney W. Grimes 	if (stat(fname, &sb))
3949b50d902SRodney W. Grimes 		err(1, "%s", fname);
3955faf2ae1SJilles Tjoelker 	tsp[0] = sb.st_atim;
3965faf2ae1SJilles Tjoelker 	tsp[1] = sb.st_mtim;
3979b50d902SRodney W. Grimes }
3989b50d902SRodney W. Grimes 
3994145c5f1SEitan Adler static void
usage(const char * myname)400b268ae64SEitan Adler usage(const char *myname)
4019b50d902SRodney W. Grimes {
40258771450SJilles Tjoelker 	fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
40358771450SJilles Tjoelker 		"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
40458771450SJilles Tjoelker 		"       [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
40558771450SJilles Tjoelker 		"file ...\n", myname);
4069b50d902SRodney W. Grimes 	exit(1);
4079b50d902SRodney W. Grimes }
408