xref: /freebsd/usr.bin/touch/touch.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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
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
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 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
278 	if (tvp[0].tv_sec == -1)
279 		goto terr;
280 
281 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
282 	return;
283 
284 terr:
285 	errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
286 }
287 
288 static void
289 stime_arg2(const char *arg, int year, struct timespec *tvp)
290 {
291 	time_t now;
292 	struct tm *t;
293 
294 	now = time(NULL);
295 	if ((t = localtime(&now)) == NULL)
296 		err(1, "localtime");
297 
298 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
299 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
300 	t->tm_mday = ATOI2(arg);
301 	t->tm_hour = ATOI2(arg);
302 	t->tm_min = ATOI2(arg);
303 	if (year) {
304 		t->tm_year = ATOI2(arg);
305 		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
306 			t->tm_year += 100;
307 	}
308 
309 	t->tm_isdst = -1;		/* Figure out DST. */
310 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
311 	if (tvp[0].tv_sec == -1)
312 		errx(1,
313 	"out of range or illegal time specification: MMDDhhmm[yy]");
314 
315 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
316 }
317 
318 static void
319 stime_darg(const char *arg, struct timespec *tvp)
320 {
321 	struct tm t = { .tm_sec = 0 };
322 	const char *fmt, *colon;
323 	char *p;
324 	int val, isutc = 0;
325 
326 	tvp[0].tv_nsec = 0;
327 	t.tm_isdst = -1;
328 	colon = strchr(arg, ':');
329 	if (colon == NULL || strchr(colon + 1, ':') == NULL)
330 		goto bad;
331 	fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
332 	    "%Y-%m-%d %H:%M:%S";
333 	p = strptime(arg, fmt, &t);
334 	if (p == NULL)
335 		goto bad;
336 	/* POSIX: must have at least one digit after dot */
337 	if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
338 		p++;
339 		val = 100000000;
340 		while (isdigit((unsigned char)*p)) {
341 			tvp[0].tv_nsec += val * (*p - '0');
342 			p++;
343 			val /= 10;
344 		}
345 	}
346 	if (*p == 'Z') {
347 		isutc = 1;
348 		p++;
349 	}
350 	if (*p != '\0')
351 		goto bad;
352 
353 	tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
354 
355 	tvp[1] = tvp[0];
356 	return;
357 
358 bad:
359 	errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
360 }
361 
362 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
363 int
364 timeoffset(const char *arg)
365 {
366 	int offset;
367 	int isneg;
368 
369 	offset = 0;
370 	isneg = *arg == '-';
371 	if (isneg)
372 		arg++;
373 	switch (strlen(arg)) {
374 	default:				/* invalid */
375 		errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
376 
377 	case 6:					/* HHMMSS */
378 		offset = ATOI2(arg);
379 		/* FALLTHROUGH */
380 	case 4:					/* MMSS */
381 		offset = offset * 60 + ATOI2(arg);
382 		/* FALLTHROUGH */
383 	case 2:					/* SS */
384 		offset = offset * 60 + ATOI2(arg);
385 	}
386 	if (isneg)
387 		return (-offset);
388 	else
389 		return (offset);
390 }
391 
392 static void
393 stime_file(const char *fname, struct timespec *tsp)
394 {
395 	struct stat sb;
396 
397 	if (stat(fname, &sb))
398 		err(1, "%s", fname);
399 	tsp[0] = sb.st_atim;
400 	tsp[1] = sb.st_mtim;
401 }
402 
403 static void
404 usage(const char *myname)
405 {
406 	fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
407 		"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
408 		"       [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
409 		"file ...\n", myname);
410 	exit(1);
411 }
412