Annotation of src/usr.bin/touch/touch.c, Revision 1.24
1.24 ! deraadt 1: /* $OpenBSD: touch.c,v 1.23 2015/03/17 19:31:30 millert 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 <locale.h>
45: #include <time.h>
46: #include <unistd.h>
47:
1.18 guenther 48: void stime_arg1(char *, struct timespec *);
49: void stime_arg2(char *, int, struct timespec *);
50: void stime_argd(char *, struct timespec *);
51: void stime_file(char *, struct timespec *);
1.13 henning 52: __dead void usage(void);
1.1 deraadt 53:
54: int
1.10 deraadt 55: main(int argc, char *argv[])
1.1 deraadt 56: {
1.18 guenther 57: struct timespec ts[2];
1.14 henning 58: int aflag, cflag, mflag, ch, fd, len, rval, timeset;
59: char *p;
1.1 deraadt 60:
1.13 henning 61: (void)setlocale(LC_ALL, "");
1.24 ! deraadt 62:
! 63: if (tame("stdio rpath wpath cpath fattr", NULL) == -1)
! 64: err(1, "tame");
1.1 deraadt 65:
1.11 otto 66: aflag = cflag = mflag = timeset = 0;
1.18 guenther 67: while ((ch = getopt(argc, argv, "acd:fmr:t:")) != -1)
1.12 deraadt 68: switch (ch) {
1.1 deraadt 69: case 'a':
70: aflag = 1;
71: break;
72: case 'c':
73: cflag = 1;
74: break;
1.18 guenther 75: case 'd':
76: timeset = 1;
77: stime_argd(optarg, ts);
78: break;
1.1 deraadt 79: case 'f':
80: break;
81: case 'm':
82: mflag = 1;
83: break;
84: case 'r':
85: timeset = 1;
1.18 guenther 86: stime_file(optarg, ts);
1.1 deraadt 87: break;
88: case 't':
89: timeset = 1;
1.18 guenther 90: stime_arg1(optarg, ts);
1.1 deraadt 91: break;
92: default:
93: usage();
94: }
95: argc -= optind;
96: argv += optind;
97:
98: /* Default is both -a and -m. */
99: if (aflag == 0 && mflag == 0)
100: aflag = mflag = 1;
101:
102: /*
103: * If no -r or -t flag, at least two operands, the first of which
104: * is an 8 or 10 digit number, use the obsolete time specification.
105: */
106: if (!timeset && argc > 1) {
107: (void)strtol(argv[0], &p, 10);
108: len = p - argv[0];
109: if (*p == '\0' && (len == 8 || len == 10)) {
110: timeset = 1;
1.18 guenther 111: stime_arg2(*argv++, len == 10, ts);
1.1 deraadt 112: }
113: }
114:
115: /* Otherwise use the current time of day. */
116: if (!timeset)
1.18 guenther 117: ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
118:
119: if (!aflag)
120: ts[0].tv_nsec = UTIME_OMIT;
121: if (!mflag)
122: ts[1].tv_nsec = UTIME_OMIT;
1.1 deraadt 123:
124: if (*argv == NULL)
125: usage();
126:
127: for (rval = 0; *argv; ++argv) {
1.18 guenther 128: /* Update the file's timestamp if it exists. */
129: if (! utimensat(AT_FDCWD, *argv, ts, 0))
130: continue;
131: if (errno != ENOENT) {
132: rval = 1;
133: warn("%s", *argv);
134: continue;
1.4 pjanzen 135: }
1.1 deraadt 136:
1.18 guenther 137: /* Didn't exist; should we create it? */
138: if (cflag)
1.1 deraadt 139: continue;
140:
1.18 guenther 141: /* Create the file. */
142: fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE);
143: if (fd == -1 || futimens(fd, ts) || close(fd)) {
1.1 deraadt 144: rval = 1;
145: warn("%s", *argv);
146: }
147: }
148: exit(rval);
149: }
150:
1.6 pjanzen 151: #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
1.1 deraadt 152:
153: void
1.18 guenther 154: stime_arg1(char *arg, struct timespec *tsp)
1.1 deraadt 155: {
1.15 millert 156: struct tm *lt;
1.13 henning 157: time_t tmptime;
158: int yearset;
1.15 millert 159: char *dot, *p;
1.1 deraadt 160: /* Start with the current time. */
1.19 guenther 161: tmptime = time(NULL);
1.15 millert 162: if ((lt = localtime(&tmptime)) == NULL)
1.1 deraadt 163: err(1, "localtime");
164: /* [[CC]YY]MMDDhhmm[.SS] */
1.15 millert 165: for (p = arg, dot = NULL; *p != '\0'; p++) {
1.16 millert 166: if (*p == '.' && dot == NULL)
1.15 millert 167: dot = p;
168: else if (!isdigit((unsigned char)*p))
169: goto terr;
170: }
171: if (dot == NULL)
172: lt->tm_sec = 0; /* Seconds defaults to 0. */
1.1 deraadt 173: else {
1.15 millert 174: *dot++ = '\0';
175: if (strlen(dot) != 2)
176: goto terr;
177: lt->tm_sec = ATOI2(dot);
178: if (lt->tm_sec > 61) /* Could be leap second. */
1.1 deraadt 179: goto terr;
180: }
1.12 deraadt 181:
1.1 deraadt 182: yearset = 0;
1.12 deraadt 183: switch (strlen(arg)) {
1.1 deraadt 184: case 12: /* CCYYMMDDhhmm */
1.23 millert 185: lt->tm_year = (ATOI2(arg) * 100) - 1900;
1.1 deraadt 186: yearset = 1;
1.8 henning 187: /* FALLTHROUGH */
1.1 deraadt 188: case 10: /* YYMMDDhhmm */
189: if (yearset) {
190: yearset = ATOI2(arg);
1.15 millert 191: lt->tm_year += yearset;
1.1 deraadt 192: } else {
193: yearset = ATOI2(arg);
1.20 guenther 194: /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
1.23 millert 195: lt->tm_year = yearset;
1.20 guenther 196: if (yearset < 69)
197: lt->tm_year += 100;
1.1 deraadt 198: }
199: /* FALLTHROUGH */
200: case 8: /* MMDDhhmm */
1.15 millert 201: lt->tm_mon = ATOI2(arg);
202: if (lt->tm_mon > 12 || lt->tm_mon == 0)
203: goto terr;
204: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
205: lt->tm_mday = ATOI2(arg);
206: if (lt->tm_mday > 31 || lt->tm_mday == 0)
207: goto terr;
208: lt->tm_hour = ATOI2(arg);
209: if (lt->tm_hour > 23)
210: goto terr;
211: lt->tm_min = ATOI2(arg);
212: if (lt->tm_min > 59)
213: goto terr;
1.1 deraadt 214: break;
215: default:
216: goto terr;
217: }
218:
1.15 millert 219: lt->tm_isdst = -1; /* Figure out DST. */
1.18 guenther 220: tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
221: if (tsp[0].tv_sec == -1)
1.1 deraadt 222: terr: errx(1,
223: "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
224:
1.18 guenther 225: tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
1.1 deraadt 226: }
227:
228: void
1.18 guenther 229: stime_arg2(char *arg, int year, struct timespec *tsp)
1.1 deraadt 230: {
1.15 millert 231: struct tm *lt;
1.13 henning 232: time_t tmptime;
1.1 deraadt 233: /* Start with the current time. */
1.19 guenther 234: tmptime = time(NULL);
1.15 millert 235: if ((lt = localtime(&tmptime)) == NULL)
1.1 deraadt 236: err(1, "localtime");
237:
1.15 millert 238: lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */
239: if (lt->tm_mon > 12 || lt->tm_mon == 0)
240: goto terr;
241: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
242: lt->tm_mday = ATOI2(arg);
243: if (lt->tm_mday > 31 || lt->tm_mday == 0)
244: goto terr;
245: lt->tm_hour = ATOI2(arg);
246: if (lt->tm_hour > 23)
247: goto terr;
248: lt->tm_min = ATOI2(arg);
249: if (lt->tm_min > 59)
250: goto terr;
1.4 pjanzen 251: if (year) {
1.20 guenther 252: year = ATOI2(arg);
253: /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
1.23 millert 254: lt->tm_year = year;
1.20 guenther 255: if (year < 69)
256: lt->tm_year += 100;
1.4 pjanzen 257: }
1.15 millert 258: lt->tm_sec = 0;
1.1 deraadt 259:
1.15 millert 260: lt->tm_isdst = -1; /* Figure out DST. */
1.18 guenther 261: tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
262: if (tsp[0].tv_sec == -1)
1.15 millert 263: terr: errx(1,
1.4 pjanzen 264: "out of range or illegal time specification: MMDDhhmm[YY]");
1.1 deraadt 265:
1.18 guenther 266: tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
1.1 deraadt 267: }
268:
269: void
1.18 guenther 270: stime_file(char *fname, struct timespec *tsp)
1.1 deraadt 271: {
1.13 henning 272: struct stat sb;
1.1 deraadt 273:
274: if (stat(fname, &sb))
275: err(1, "%s", fname);
1.18 guenther 276: tsp[0] = sb.st_atim;
277: tsp[1] = sb.st_mtim;
278: }
279:
280: void
281: stime_argd(char *arg, struct timespec *tsp)
282: {
283: struct tm tm;
284: char *frac, *p;
285: int utc = 0;
286:
287: /* accept YYYY-MM-DD(T| )hh:mm:ss[(.|,)frac][Z] */
288: memset(&tm, 0, sizeof(tm));
289: p = strptime(arg, "%F", &tm);
290: if (p == NULL || (*p != 'T' && *p != ' '))
291: goto terr;
292: p = strptime(p + 1, "%T", &tm);
293: if (p == NULL)
294: goto terr;
295: tsp[0].tv_nsec = 0;
296: if (*p == '.' || *p == ',') {
297: frac = ++p;
298: while (isdigit((unsigned char)*p)) {
299: if (p - frac < 9) {
300: tsp[0].tv_nsec = tsp[0].tv_nsec * 10 +
301: *p - '0';
302: }
303: p++;
304: }
305: if (p == frac)
306: goto terr;
307:
308: /* fill in the trailing zeros */
309: while (p - frac-- < 9)
310: tsp[0].tv_nsec *= 10;
311: }
312: if (*p == 'Z') {
313: utc = 1;
314: p++;
315: }
316: if (*p != '\0')
317: goto terr;
318:
319: tm.tm_isdst = -1;
320: tsp[0].tv_sec = utc ? timegm(&tm) : mktime(&tm);
321: if (tsp[0].tv_sec == -1)
322: terr: errx(1,
323: "out of range or illegal time specification: YYYY-MM-DDThh:mm:ss[.frac][Z]");
324: tsp[1] = tsp[0];
1.1 deraadt 325: }
326:
327: __dead void
1.10 deraadt 328: usage(void)
1.1 deraadt 329: {
330: (void)fprintf(stderr,
1.21 jmc 331: "usage: touch [-acm] [-d ccyy-mm-ddTHH:MM:SS[.frac][Z]] [-r file]\n"
332: " [-t [[cc]yy]mmddHHMM[.SS]] file ...\n");
1.1 deraadt 333: exit(1);
334: }