xref: /freebsd/usr.bin/touch/touch.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <libgen.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
54 
55 static void	stime_arg1(const char *, struct timespec *);
56 static void	stime_arg2(const char *, int, struct timespec *);
57 static void	stime_darg(const char *, struct timespec *);
58 static void	stime_file(const char *, struct timespec *);
59 static int	timeoffset(const char *);
60 static void	usage(const char *);
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	struct stat sb;
66 	struct timespec ts[2];
67 	int atflag;
68 	int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
69 	char *p;
70 	char *myname;
71 
72 	myname = basename(argv[0]);
73 	Aflag = aflag = cflag = mflag = timeset = 0;
74 	atflag = 0;
75 	ts[0].tv_sec = ts[1].tv_sec = 0;
76 	ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
77 
78 	while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
79 		switch(ch) {
80 		case 'A':
81 			Aflag = timeoffset(optarg);
82 			break;
83 		case 'a':
84 			aflag = 1;
85 			break;
86 		case 'c':
87 			cflag = 1;
88 			break;
89 		case 'd':
90 			timeset = 1;
91 			stime_darg(optarg, ts);
92 			break;
93 		case 'f':
94 			/* No-op for compatibility. */
95 			break;
96 		case 'h':
97 			cflag = 1;
98 			atflag = AT_SYMLINK_NOFOLLOW;
99 			break;
100 		case 'm':
101 			mflag = 1;
102 			break;
103 		case 'r':
104 			timeset = 1;
105 			stime_file(optarg, ts);
106 			break;
107 		case 't':
108 			timeset = 1;
109 			stime_arg1(optarg, ts);
110 			break;
111 		default:
112 			usage(myname);
113 		}
114 	argc -= optind;
115 	argv += optind;
116 
117 	if (aflag == 0 && mflag == 0)
118 		aflag = mflag = 1;
119 
120 	if (timeset) {
121 		if (Aflag) {
122 			/*
123 			 * We're setting the time to an offset from a specified
124 			 * time.  God knows why, but it means that we can set
125 			 * that time once and for all here.
126 			 */
127 			if (aflag)
128 				ts[0].tv_sec += Aflag;
129 			if (mflag)
130 				ts[1].tv_sec += Aflag;
131 			Aflag = 0;		/* done our job */
132 		}
133 	} else {
134 		/*
135 		 * If no -r or -t flag, at least two operands, the first of
136 		 * which is an 8 or 10 digit number, use the obsolete time
137 		 * specification, otherwise use the current time.
138 		 */
139 		if (argc > 1) {
140 			strtol(argv[0], &p, 10);
141 			len = p - argv[0];
142 			if (*p == '\0' && (len == 8 || len == 10)) {
143 				timeset = 1;
144 				stime_arg2(*argv++, len == 10, ts);
145 			}
146 		}
147 		/* Both times default to the same. */
148 		ts[1] = ts[0];
149 	}
150 
151 	if (!aflag)
152 		ts[0].tv_nsec = UTIME_OMIT;
153 	if (!mflag)
154 		ts[1].tv_nsec = UTIME_OMIT;
155 
156 	if (*argv == NULL)
157 		usage(myname);
158 
159 	if (Aflag)
160 		cflag = 1;
161 
162 	for (rval = 0; *argv; ++argv) {
163 		/* See if the file exists. */
164 		if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
165 			if (errno != ENOENT) {
166 				rval = 1;
167 				warn("%s", *argv);
168 				continue;
169 			}
170 			if (!cflag) {
171 				/* Create the file. */
172 				fd = open(*argv,
173 				    O_WRONLY | O_CREAT, DEFFILEMODE);
174 				if (fd == -1) {
175 					rval = 1;
176 					warn("%s", *argv);
177 					continue;
178 				}
179 				if (fstat(fd, &sb) < 0) {
180 					warn("%s", *argv);
181 					rval = 1;
182 				}
183 				if (close(fd) < 0) {
184 					warn("%s", *argv);
185 					rval = 1;
186 				}
187 
188 				/* If using the current time, we're done. */
189 				if (!timeset)
190 					continue;
191 			} else
192 				continue;
193 		}
194 
195 		/*
196 		 * We're adjusting the times based on the file times, not a
197 		 * specified time (that gets handled above).
198 		 */
199 		if (Aflag) {
200 			if (aflag) {
201 				ts[0] = sb.st_atim;
202 				ts[0].tv_sec += Aflag;
203 			}
204 			if (mflag) {
205 				ts[1] = sb.st_mtim;
206 				ts[1].tv_sec += Aflag;
207 			}
208 		}
209 
210 		if (!utimensat(AT_FDCWD, *argv, ts, atflag))
211 			continue;
212 
213 		rval = 1;
214 		warn("%s", *argv);
215 	}
216 	exit(rval);
217 }
218 
219 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
220 
221 static void
222 stime_arg1(const char *arg, struct timespec *tvp)
223 {
224 	time_t now;
225 	struct tm *t;
226 	int yearset;
227 	char *p;
228 
229 	now = time(NULL);
230 	if ((t = localtime(&now)) == NULL)
231 		err(1, "localtime");
232 					/* [[CC]YY]MMDDhhmm[.SS] */
233 	if ((p = strchr(arg, '.')) == NULL)
234 		t->tm_sec = 0;		/* Seconds defaults to 0. */
235 	else {
236 		if (strlen(p + 1) != 2)
237 			goto terr;
238 		*p++ = '\0';
239 		t->tm_sec = ATOI2(p);
240 	}
241 
242 	yearset = 0;
243 	switch(strlen(arg)) {
244 	case 12:			/* CCYYMMDDhhmm */
245 		t->tm_year = ATOI2(arg);
246 		t->tm_year *= 100;
247 		yearset = 1;
248 		/* FALLTHROUGH */
249 	case 10:			/* YYMMDDhhmm */
250 		if (yearset) {
251 			yearset = ATOI2(arg);
252 			t->tm_year += yearset;
253 		} else {
254 			yearset = ATOI2(arg);
255 			if (yearset < 69)
256 				t->tm_year = yearset + 2000;
257 			else
258 				t->tm_year = yearset + 1900;
259 		}
260 		t->tm_year -= 1900;	/* Convert to UNIX time. */
261 		/* FALLTHROUGH */
262 	case 8:				/* MMDDhhmm */
263 		t->tm_mon = ATOI2(arg);
264 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
265 		t->tm_mday = ATOI2(arg);
266 		t->tm_hour = ATOI2(arg);
267 		t->tm_min = ATOI2(arg);
268 		break;
269 	default:
270 		goto terr;
271 	}
272 
273 	t->tm_isdst = -1;		/* Figure out DST. */
274 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
275 	if (tvp[0].tv_sec == -1)
276 		goto terr;
277 
278 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
279 	return;
280 
281 terr:
282 	errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
283 }
284 
285 static void
286 stime_arg2(const char *arg, int year, struct timespec *tvp)
287 {
288 	time_t now;
289 	struct tm *t;
290 
291 	now = time(NULL);
292 	if ((t = localtime(&now)) == NULL)
293 		err(1, "localtime");
294 
295 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
296 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
297 	t->tm_mday = ATOI2(arg);
298 	t->tm_hour = ATOI2(arg);
299 	t->tm_min = ATOI2(arg);
300 	if (year) {
301 		t->tm_year = ATOI2(arg);
302 		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
303 			t->tm_year += 100;
304 	}
305 
306 	t->tm_isdst = -1;		/* Figure out DST. */
307 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
308 	if (tvp[0].tv_sec == -1)
309 		errx(1,
310 	"out of range or illegal time specification: MMDDhhmm[yy]");
311 
312 	tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
313 }
314 
315 static void
316 stime_darg(const char *arg, struct timespec *tvp)
317 {
318 	struct tm t = { .tm_sec = 0 };
319 	const char *fmt, *colon;
320 	char *p;
321 	int val, isutc = 0;
322 
323 	tvp[0].tv_nsec = 0;
324 	t.tm_isdst = -1;
325 	colon = strchr(arg, ':');
326 	if (colon == NULL || strchr(colon + 1, ':') == NULL)
327 		goto bad;
328 	fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
329 	    "%Y-%m-%d %H:%M:%S";
330 	p = strptime(arg, fmt, &t);
331 	if (p == NULL)
332 		goto bad;
333 	/* POSIX: must have at least one digit after dot */
334 	if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
335 		p++;
336 		val = 100000000;
337 		while (isdigit((unsigned char)*p)) {
338 			tvp[0].tv_nsec += val * (*p - '0');
339 			p++;
340 			val /= 10;
341 		}
342 	}
343 	if (*p == 'Z') {
344 		isutc = 1;
345 		p++;
346 	}
347 	if (*p != '\0')
348 		goto bad;
349 
350 	tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
351 
352 	tvp[1] = tvp[0];
353 	return;
354 
355 bad:
356 	errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
357 }
358 
359 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
360 int
361 timeoffset(const char *arg)
362 {
363 	int offset;
364 	int isneg;
365 
366 	offset = 0;
367 	isneg = *arg == '-';
368 	if (isneg)
369 		arg++;
370 	switch (strlen(arg)) {
371 	default:				/* invalid */
372 		errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
373 
374 	case 6:					/* HHMMSS */
375 		offset = ATOI2(arg);
376 		/* FALLTHROUGH */
377 	case 4:					/* MMSS */
378 		offset = offset * 60 + ATOI2(arg);
379 		/* FALLTHROUGH */
380 	case 2:					/* SS */
381 		offset = offset * 60 + ATOI2(arg);
382 	}
383 	if (isneg)
384 		return (-offset);
385 	else
386 		return (offset);
387 }
388 
389 static void
390 stime_file(const char *fname, struct timespec *tsp)
391 {
392 	struct stat sb;
393 
394 	if (stat(fname, &sb))
395 		err(1, "%s", fname);
396 	tsp[0] = sb.st_atim;
397 	tsp[1] = sb.st_mtim;
398 }
399 
400 static void
401 usage(const char *myname)
402 {
403 	fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
404 		"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
405 		"       [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
406 		"file ...\n", myname);
407 	exit(1);
408 }
409