Annotation of src/usr.bin/touch/touch.c, Revision 1.27
1.27 ! cheloha 1: /* $OpenBSD: touch.c,v 1.26 2019/03/10 15:11:52 schwarze Exp $ */
1.1 deraadt 2: /* $NetBSD: touch.c,v 1.11 1995/08/31 22:10:06 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.9 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #include <sys/types.h>
34: #include <sys/stat.h>
35: #include <sys/time.h>
36:
1.15 millert 37: #include <ctype.h>
1.1 deraadt 38: #include <err.h>
39: #include <errno.h>
40: #include <fcntl.h>
41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
44: #include <time.h>
45: #include <unistd.h>
46:
1.18 guenther 47: void stime_arg1(char *, struct timespec *);
48: void stime_arg2(char *, int, struct timespec *);
49: void stime_argd(char *, struct timespec *);
50: void stime_file(char *, struct timespec *);
1.26 schwarze 51: static void __dead usage(void);
1.1 deraadt 52:
53: int
1.10 deraadt 54: main(int argc, char *argv[])
1.1 deraadt 55: {
1.18 guenther 56: struct timespec ts[2];
1.14 henning 57: int aflag, cflag, mflag, ch, fd, len, rval, timeset;
58: char *p;
1.1 deraadt 59:
1.25 deraadt 60: if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
61: err(1, "pledge");
1.1 deraadt 62:
1.11 otto 63: aflag = cflag = mflag = timeset = 0;
1.18 guenther 64: while ((ch = getopt(argc, argv, "acd:fmr:t:")) != -1)
1.12 deraadt 65: switch (ch) {
1.1 deraadt 66: case 'a':
67: aflag = 1;
68: break;
69: case 'c':
70: cflag = 1;
71: break;
1.18 guenther 72: case 'd':
73: timeset = 1;
74: stime_argd(optarg, ts);
75: break;
1.1 deraadt 76: case 'f':
77: break;
78: case 'm':
79: mflag = 1;
80: break;
81: case 'r':
82: timeset = 1;
1.18 guenther 83: stime_file(optarg, ts);
1.1 deraadt 84: break;
85: case 't':
86: timeset = 1;
1.18 guenther 87: stime_arg1(optarg, ts);
1.1 deraadt 88: break;
89: default:
90: usage();
91: }
92: argc -= optind;
93: argv += optind;
94:
95: /* Default is both -a and -m. */
96: if (aflag == 0 && mflag == 0)
97: aflag = mflag = 1;
98:
99: /*
100: * If no -r or -t flag, at least two operands, the first of which
101: * is an 8 or 10 digit number, use the obsolete time specification.
102: */
103: if (!timeset && argc > 1) {
104: (void)strtol(argv[0], &p, 10);
105: len = p - argv[0];
106: if (*p == '\0' && (len == 8 || len == 10)) {
107: timeset = 1;
1.18 guenther 108: stime_arg2(*argv++, len == 10, ts);
1.1 deraadt 109: }
110: }
111:
112: /* Otherwise use the current time of day. */
113: if (!timeset)
1.18 guenther 114: ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
115:
116: if (!aflag)
117: ts[0].tv_nsec = UTIME_OMIT;
118: if (!mflag)
119: ts[1].tv_nsec = UTIME_OMIT;
1.1 deraadt 120:
121: if (*argv == NULL)
122: usage();
123:
124: for (rval = 0; *argv; ++argv) {
1.18 guenther 125: /* Update the file's timestamp if it exists. */
126: if (! utimensat(AT_FDCWD, *argv, ts, 0))
127: continue;
128: if (errno != ENOENT) {
129: rval = 1;
130: warn("%s", *argv);
131: continue;
1.4 pjanzen 132: }
1.1 deraadt 133:
1.18 guenther 134: /* Didn't exist; should we create it? */
135: if (cflag)
1.1 deraadt 136: continue;
137:
1.18 guenther 138: /* Create the file. */
139: fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE);
1.27 ! cheloha 140: if (fd == -1) {
1.1 deraadt 141: rval = 1;
142: warn("%s", *argv);
1.27 ! cheloha 143: continue;
! 144: }
! 145: if (futimens(fd, ts) == -1) {
! 146: warn("%s", *argv);
! 147: rval = 1;
! 148: }
! 149: if (close(fd) == -1) {
! 150: warn("%s", *argv);
! 151: rval = 1;
1.1 deraadt 152: }
153: }
1.26 schwarze 154: return rval;
1.1 deraadt 155: }
156:
1.6 pjanzen 157: #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
1.1 deraadt 158:
159: void
1.18 guenther 160: stime_arg1(char *arg, struct timespec *tsp)
1.1 deraadt 161: {
1.15 millert 162: struct tm *lt;
1.13 henning 163: time_t tmptime;
164: int yearset;
1.15 millert 165: char *dot, *p;
1.1 deraadt 166: /* Start with the current time. */
1.19 guenther 167: tmptime = time(NULL);
1.15 millert 168: if ((lt = localtime(&tmptime)) == NULL)
1.1 deraadt 169: err(1, "localtime");
170: /* [[CC]YY]MMDDhhmm[.SS] */
1.15 millert 171: for (p = arg, dot = NULL; *p != '\0'; p++) {
1.16 millert 172: if (*p == '.' && dot == NULL)
1.15 millert 173: dot = p;
174: else if (!isdigit((unsigned char)*p))
175: goto terr;
176: }
177: if (dot == NULL)
178: lt->tm_sec = 0; /* Seconds defaults to 0. */
1.1 deraadt 179: else {
1.15 millert 180: *dot++ = '\0';
181: if (strlen(dot) != 2)
182: goto terr;
183: lt->tm_sec = ATOI2(dot);
184: if (lt->tm_sec > 61) /* Could be leap second. */
1.1 deraadt 185: goto terr;
186: }
1.12 deraadt 187:
1.1 deraadt 188: yearset = 0;
1.12 deraadt 189: switch (strlen(arg)) {
1.1 deraadt 190: case 12: /* CCYYMMDDhhmm */
1.23 millert 191: lt->tm_year = (ATOI2(arg) * 100) - 1900;
1.1 deraadt 192: yearset = 1;
1.8 henning 193: /* FALLTHROUGH */
1.1 deraadt 194: case 10: /* YYMMDDhhmm */
195: if (yearset) {
196: yearset = ATOI2(arg);
1.15 millert 197: lt->tm_year += yearset;
1.1 deraadt 198: } else {
199: yearset = ATOI2(arg);
1.20 guenther 200: /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
1.23 millert 201: lt->tm_year = yearset;
1.20 guenther 202: if (yearset < 69)
203: lt->tm_year += 100;
1.1 deraadt 204: }
205: /* FALLTHROUGH */
206: case 8: /* MMDDhhmm */
1.15 millert 207: lt->tm_mon = ATOI2(arg);
208: if (lt->tm_mon > 12 || lt->tm_mon == 0)
209: goto terr;
210: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
211: lt->tm_mday = ATOI2(arg);
212: if (lt->tm_mday > 31 || lt->tm_mday == 0)
213: goto terr;
214: lt->tm_hour = ATOI2(arg);
215: if (lt->tm_hour > 23)
216: goto terr;
217: lt->tm_min = ATOI2(arg);
218: if (lt->tm_min > 59)
219: goto terr;
1.1 deraadt 220: break;
221: default:
222: goto terr;
223: }
224:
1.15 millert 225: lt->tm_isdst = -1; /* Figure out DST. */
1.18 guenther 226: tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
227: if (tsp[0].tv_sec == -1)
1.1 deraadt 228: terr: errx(1,
229: "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
230:
1.18 guenther 231: tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
1.1 deraadt 232: }
233:
234: void
1.18 guenther 235: stime_arg2(char *arg, int year, struct timespec *tsp)
1.1 deraadt 236: {
1.15 millert 237: struct tm *lt;
1.13 henning 238: time_t tmptime;
1.1 deraadt 239: /* Start with the current time. */
1.19 guenther 240: tmptime = time(NULL);
1.15 millert 241: if ((lt = localtime(&tmptime)) == NULL)
1.1 deraadt 242: err(1, "localtime");
243:
1.15 millert 244: lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */
245: if (lt->tm_mon > 12 || lt->tm_mon == 0)
246: goto terr;
247: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
248: lt->tm_mday = ATOI2(arg);
249: if (lt->tm_mday > 31 || lt->tm_mday == 0)
250: goto terr;
251: lt->tm_hour = ATOI2(arg);
252: if (lt->tm_hour > 23)
253: goto terr;
254: lt->tm_min = ATOI2(arg);
255: if (lt->tm_min > 59)
256: goto terr;
1.4 pjanzen 257: if (year) {
1.20 guenther 258: year = ATOI2(arg);
259: /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
1.23 millert 260: lt->tm_year = year;
1.20 guenther 261: if (year < 69)
262: lt->tm_year += 100;
1.4 pjanzen 263: }
1.15 millert 264: lt->tm_sec = 0;
1.1 deraadt 265:
1.15 millert 266: lt->tm_isdst = -1; /* Figure out DST. */
1.18 guenther 267: tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
268: if (tsp[0].tv_sec == -1)
1.15 millert 269: terr: errx(1,
1.4 pjanzen 270: "out of range or illegal time specification: MMDDhhmm[YY]");
1.1 deraadt 271:
1.18 guenther 272: tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
1.1 deraadt 273: }
274:
275: void
1.18 guenther 276: stime_file(char *fname, struct timespec *tsp)
1.1 deraadt 277: {
1.13 henning 278: struct stat sb;
1.1 deraadt 279:
280: if (stat(fname, &sb))
281: err(1, "%s", fname);
1.18 guenther 282: tsp[0] = sb.st_atim;
283: tsp[1] = sb.st_mtim;
284: }
285:
286: void
287: stime_argd(char *arg, struct timespec *tsp)
288: {
289: struct tm tm;
290: char *frac, *p;
291: int utc = 0;
292:
293: /* accept YYYY-MM-DD(T| )hh:mm:ss[(.|,)frac][Z] */
294: memset(&tm, 0, sizeof(tm));
295: p = strptime(arg, "%F", &tm);
296: if (p == NULL || (*p != 'T' && *p != ' '))
297: goto terr;
298: p = strptime(p + 1, "%T", &tm);
299: if (p == NULL)
300: goto terr;
301: tsp[0].tv_nsec = 0;
302: if (*p == '.' || *p == ',') {
303: frac = ++p;
304: while (isdigit((unsigned char)*p)) {
305: if (p - frac < 9) {
306: tsp[0].tv_nsec = tsp[0].tv_nsec * 10 +
307: *p - '0';
308: }
309: p++;
310: }
311: if (p == frac)
312: goto terr;
313:
314: /* fill in the trailing zeros */
315: while (p - frac-- < 9)
316: tsp[0].tv_nsec *= 10;
317: }
318: if (*p == 'Z') {
319: utc = 1;
320: p++;
321: }
322: if (*p != '\0')
323: goto terr;
324:
325: tm.tm_isdst = -1;
326: tsp[0].tv_sec = utc ? timegm(&tm) : mktime(&tm);
327: if (tsp[0].tv_sec == -1)
328: terr: errx(1,
329: "out of range or illegal time specification: YYYY-MM-DDThh:mm:ss[.frac][Z]");
330: tsp[1] = tsp[0];
1.1 deraadt 331: }
332:
1.26 schwarze 333: static void __dead
1.10 deraadt 334: usage(void)
1.1 deraadt 335: {
336: (void)fprintf(stderr,
1.21 jmc 337: "usage: touch [-acm] [-d ccyy-mm-ddTHH:MM:SS[.frac][Z]] [-r file]\n"
338: " [-t [[cc]yy]mmddHHMM[.SS]] file ...\n");
1.1 deraadt 339: exit(1);
340: }