xref: /freebsd-14.2/usr.bin/touch/touch.c (revision ae2888dd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif
38 
39 #ifndef lint
40 static const char sccsid[] = "@(#)touch.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <libgen.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 
58 static void	stime_arg1(const char *, struct timespec *);
59 static void	stime_arg2(const char *, int, struct timespec *);
60 static void	stime_darg(const char *, struct timespec *);
61 static void	stime_file(const char *, struct timespec *);
62 static int	timeoffset(const char *);
63 static void	usage(const char *);
64 
65 int
main(int argc,char * argv[])66 main(int argc, char *argv[])
67 {
68 	struct stat sb;
69 	struct timespec ts[2];
70 	int atflag;
71 	int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
72 	char *p;
73 	char *myname;
74 
75 	myname = basename(argv[0]);
76 	Aflag = aflag = cflag = mflag = timeset = 0;
77 	atflag = 0;
78 	ts[0].tv_sec = ts[1].tv_sec = 0;
79 	ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
80 
81 	while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
82 		switch(ch) {
83 		case 'A':
84 			Aflag = timeoffset(optarg);
85 			break;
86 		case 'a':
87 			aflag = 1;
88 			break;
89 		case 'c':
90 			cflag = 1;
91 			break;
92 		case 'd':
93 			timeset = 1;
94 			stime_darg(optarg, ts);
95 			break;
96 		case 'f':
97 			/* No-op for compatibility. */
98 			break;
99 		case 'h':
100 			cflag = 1;
101 			atflag = AT_SYMLINK_NOFOLLOW;
102 			break;
103 		case 'm':
104 			mflag = 1;
105 			break;
106 		case 'r':
107 			timeset = 1;
108 			stime_file(optarg, ts);
109 			break;
110 		case 't':
111 			timeset = 1;
112 			stime_arg1(optarg, ts);
113 			break;
114 		default:
115 			usage(myname);
116 		}
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (aflag == 0 && mflag == 0)
121 		aflag = mflag = 1;
122 
123 	if (timeset) {
124 		if (Aflag) {
125 			/*
126 			 * We're setting the time to an offset from a specified
127 			 * time.  God knows why, but it means that we can set
128 			 * that time once and for all here.
129 			 */
130 			if (aflag)
131 				ts[0].tv_sec += Aflag;
132 			if (mflag)
133 				ts[1].tv_sec += Aflag;
134 			Aflag = 0;		/* done our job */
135 		}
136 	} else {
137 		/*
138 		 * If no -r or -t flag, at least two operands, the first of
139 		 * which is an 8 or 10 digit number, use the obsolete time
140 		 * specification, otherwise use the current time.
141 		 */
142 		if (argc > 1) {
143 			strtol(argv[0], &p, 10);
144 			len = p - argv[0];
145 			if (*p == '\0' && (len == 8 || len == 10)) {
146 				timeset = 1;
147 				stime_arg2(*argv++, len == 10, ts);
148 			}
149 		}
150 		/* Both times default to the same. */
151 		ts[1] = ts[0];
152 	}
153 
154 	if (!aflag)
155 		ts[0].tv_nsec = UTIME_OMIT;
156 	if (!mflag)
157 		ts[1].tv_nsec = UTIME_OMIT;
158 
159 	if (*argv == NULL)
160 		usage(myname);
161 
162 	if (Aflag)
163 		cflag = 1;
164 
165 	for (rval = 0; *argv; ++argv) {
166 		/* See if the file exists. */
167 		if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
168 			if (errno != ENOENT) {
169 				rval = 1;
170 				warn("%s", *argv);
171 				continue;
172 			}
173 			if (!cflag) {
174 				/* Create the file. */
175 				fd = open(*argv,
176 				    O_WRONLY | O_CREAT, DEFFILEMODE);
177 				if (fd == -1) {
178 					rval = 1;
179 					warn("%s", *argv);
180 					continue;
181 				}
182 				if (fstat(fd, &sb) < 0) {
183 					warn("%s", *argv);
184 					rval = 1;
185 				}
186 				if (close(fd) < 0) {
187 					warn("%s", *argv);
188 					rval = 1;
189 				}
190 
191 				/* If using the current time, we're done. */
192 				if (!timeset)
193 					continue;
194 			} else
195 				continue;
196 		}
197 
198 		/*
199 		 * We're adjusting the times based on the file times, not a
200 		 * specified time (that gets handled above).
201 		 */
202 		if (Aflag) {
203 			if (aflag) {
204 				ts[0] = sb.st_atim;
205 				ts[0].tv_sec += Aflag;
206 			}
207 			if (mflag) {
208 				ts[1] = sb.st_mtim;
209 				ts[1].tv_sec += Aflag;
210 			}
211 		}
212 
213 		if (!utimensat(AT_FDCWD, *argv, ts, atflag))
214 			continue;
215 
216 		rval = 1;
217 		warn("%s", *argv);
218 	}
219 	exit(rval);
220 }
221 
222 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
223 
224 static void
stime_arg1(const char * arg,struct timespec * tvp)225 stime_arg1(const char *arg, struct timespec *tvp)
226 {
227 	time_t now;
228 	struct tm *t;
229 	int yearset;
230 	char *p;
231 
232 	now = time(NULL);
233 	if ((t = localtime(&now)) == NULL)
234 		err(1, "localtime");
235 					/* [[CC]YY]MMDDhhmm[.SS] */
236 	if ((p = strchr(arg, '.')) == NULL)
237 		t->tm_sec = 0;		/* Seconds defaults to 0. */
238 	else {
239 		if (strlen(p + 1) != 2)
240 			goto terr;
241 		*p++ = '\0';
242 		t->tm_sec = ATOI2(p);
243 	}
244 
245 	yearset = 0;
246 	switch (strlen(arg)) {
247 	case 12:			/* CCYYMMDDhhmm */
248 		t->tm_year = ATOI2(arg);
249 		t->tm_year *= 100;
250 		yearset = 1;
251 		/* FALLTHROUGH */
252 	case 10:			/* YYMMDDhhmm */
253 		if (yearset) {
254 			yearset = ATOI2(arg);
255 			t->tm_year += yearset;
256 		} else {
257 			yearset = ATOI2(arg);
258 			if (yearset < 69)
259 				t->tm_year = yearset + 2000;
260 			else
261 				t->tm_year = yearset + 1900;
262 		}
263 		t->tm_year -= 1900;	/* Convert to UNIX time. */
264 		/* FALLTHROUGH */
265 	case 8:				/* MMDDhhmm */
266 		t->tm_mon = ATOI2(arg);
267 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
268 		t->tm_mday = ATOI2(arg);
269 		t->tm_hour = ATOI2(arg);
270 		t->tm_min = ATOI2(arg);
271 		break;
272 	default:
273 		goto terr;
274 	}
275 
276 	t->tm_isdst = -1;		/* Figure out DST. */
277 	t->tm_yday = -1;
278 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
279 	if (t->tm_yday == -1)
280 		goto terr;
281 
282 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
283 	return;
284 
285 terr:
286 	errx(1, "out of range or illegal time specification: "
287 	    "[[CC]YY]MMDDhhmm[.SS]");
288 }
289 
290 static void
stime_arg2(const char * arg,int year,struct timespec * tvp)291 stime_arg2(const char *arg, int year, struct timespec *tvp)
292 {
293 	time_t now;
294 	struct tm *t;
295 
296 	now = time(NULL);
297 	if ((t = localtime(&now)) == NULL)
298 		err(1, "localtime");
299 
300 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
301 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
302 	t->tm_mday = ATOI2(arg);
303 	t->tm_hour = ATOI2(arg);
304 	t->tm_min = ATOI2(arg);
305 	if (year) {
306 		t->tm_year = ATOI2(arg);
307 		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
308 			t->tm_year += 100;
309 	}
310 
311 	t->tm_isdst = -1;		/* Figure out DST. */
312 	t->tm_yday = -1;
313 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
314 	if (t->tm_yday == -1)
315 		errx(1, "out of range or illegal time specification: "
316 		    "MMDDhhmm[yy]");
317 
318 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
319 }
320 
321 static void
stime_darg(const char * arg,struct timespec * tvp)322 stime_darg(const char *arg, struct timespec *tvp)
323 {
324 	struct tm t = { .tm_sec = 0 };
325 	const char *fmt, *colon;
326 	char *p;
327 	int val, isutc = 0;
328 
329 	tvp[0].tv_nsec = 0;
330 	t.tm_isdst = -1;
331 	colon = strchr(arg, ':');
332 	if (colon == NULL || strchr(colon + 1, ':') == NULL)
333 		goto bad;
334 	fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
335 	    "%Y-%m-%d %H:%M:%S";
336 	p = strptime(arg, fmt, &t);
337 	if (p == NULL)
338 		goto bad;
339 	/* POSIX: must have at least one digit after dot */
340 	if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
341 		p++;
342 		val = 100000000;
343 		while (isdigit((unsigned char)*p)) {
344 			tvp[0].tv_nsec += val * (*p - '0');
345 			p++;
346 			val /= 10;
347 		}
348 	}
349 	if (*p == 'Z') {
350 		isutc = 1;
351 		p++;
352 	}
353 	if (*p != '\0')
354 		goto bad;
355 
356 	t.tm_yday = -1;
357 	tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
358 	if (t.tm_yday == -1)
359 		goto bad;
360 
361 	tvp[1] = tvp[0];
362 	return;
363 
364 bad:
365 	errx(1, "out of range or illegal time specification: "
366 	    "YYYY-MM-DDThh:mm:SS[.frac][tz]");
367 }
368 
369 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
370 int
timeoffset(const char * arg)371 timeoffset(const char *arg)
372 {
373 	int offset;
374 	int isneg;
375 
376 	offset = 0;
377 	isneg = *arg == '-';
378 	if (isneg)
379 		arg++;
380 	switch (strlen(arg)) {
381 	default:				/* invalid */
382 		errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
383 
384 	case 6:					/* HHMMSS */
385 		offset = ATOI2(arg);
386 		/* FALLTHROUGH */
387 	case 4:					/* MMSS */
388 		offset = offset * 60 + ATOI2(arg);
389 		/* FALLTHROUGH */
390 	case 2:					/* SS */
391 		offset = offset * 60 + ATOI2(arg);
392 	}
393 	if (isneg)
394 		return (-offset);
395 	else
396 		return (offset);
397 }
398 
399 static void
stime_file(const char * fname,struct timespec * tsp)400 stime_file(const char *fname, struct timespec *tsp)
401 {
402 	struct stat sb;
403 
404 	if (stat(fname, &sb))
405 		err(1, "%s", fname);
406 	tsp[0] = sb.st_atim;
407 	tsp[1] = sb.st_mtim;
408 }
409 
410 static void
usage(const char * myname)411 usage(const char *myname)
412 {
413 	fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
414 		"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
415 		"       [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
416 		"file ...\n", myname);
417 	exit(1);
418 }
419