Annotation of src/usr.bin/touch/touch.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: touch.c,v 1.15 2007/05/25 13:56:59 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>
1.5 pjanzen 46: #include <tzfile.h>
1.1 deraadt 47: #include <unistd.h>
48:
1.13 henning 49: void stime_arg1(char *, struct timeval *);
50: void stime_arg2(char *, int, struct timeval *);
51: void stime_file(char *, struct timeval *);
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.14 henning 57: struct stat sb;
58: struct timeval tv[2];
59: int aflag, cflag, mflag, ch, fd, len, rval, timeset;
60: char *p;
1.1 deraadt 61:
1.13 henning 62: (void)setlocale(LC_ALL, "");
1.1 deraadt 63:
1.11 otto 64: aflag = cflag = mflag = timeset = 0;
1.1 deraadt 65: if (gettimeofday(&tv[0], NULL))
66: err(1, "gettimeofday");
67:
1.3 millert 68: while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
1.12 deraadt 69: switch (ch) {
1.1 deraadt 70: case 'a':
71: aflag = 1;
72: break;
73: case 'c':
74: cflag = 1;
75: break;
76: case 'f':
77: break;
78: case 'm':
79: mflag = 1;
80: break;
81: case 'r':
82: timeset = 1;
83: stime_file(optarg, tv);
84: break;
85: case 't':
86: timeset = 1;
87: stime_arg1(optarg, tv);
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;
108: stime_arg2(*argv++, len == 10, tv);
109: }
110: }
111:
112: /* Otherwise use the current time of day. */
113: if (!timeset)
114: tv[1] = tv[0];
115:
116: if (*argv == NULL)
117: usage();
118:
119: for (rval = 0; *argv; ++argv) {
120: /* See if the file exists. */
1.4 pjanzen 121: if (stat(*argv, &sb)) {
1.1 deraadt 122: if (!cflag) {
123: /* Create the file. */
124: fd = open(*argv,
125: O_WRONLY | O_CREAT, DEFFILEMODE);
126: if (fd == -1 || fstat(fd, &sb) || close(fd)) {
127: rval = 1;
128: warn("%s", *argv);
129: continue;
130: }
131:
132: /* If using the current time, we're done. */
133: if (!timeset)
134: continue;
135: } else
136: continue;
1.4 pjanzen 137: }
1.1 deraadt 138:
139: if (!aflag)
140: TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
141: if (!mflag)
142: TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
143:
144: /* Try utimes(2). */
145: if (!utimes(*argv, tv))
146: continue;
147:
148: /* If the user specified a time, nothing else we can do. */
149: if (timeset) {
150: rval = 1;
151: warn("%s", *argv);
152: }
153:
154: /*
155: * System V and POSIX 1003.1 require that a NULL argument
156: * set the access/modification times to the current time.
157: * The permission checks are different, too, in that the
158: * ability to write the file is sufficient. Take a shot.
159: */
160: if (!utimes(*argv, NULL))
161: continue;
162:
1.11 otto 163: rval = 1;
164: warn("%s", *argv);
1.1 deraadt 165: }
166: exit(rval);
167: }
168:
1.6 pjanzen 169: #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
1.1 deraadt 170:
171: void
1.10 deraadt 172: stime_arg1(char *arg, struct timeval *tvp)
1.1 deraadt 173: {
1.15 millert 174: struct tm *lt;
1.13 henning 175: time_t tmptime;
176: int yearset;
1.15 millert 177: char *dot, *p;
1.1 deraadt 178: /* Start with the current time. */
179: tmptime = tvp[0].tv_sec;
1.15 millert 180: if ((lt = localtime(&tmptime)) == NULL)
1.1 deraadt 181: err(1, "localtime");
182: /* [[CC]YY]MMDDhhmm[.SS] */
1.15 millert 183: for (p = arg, dot = NULL; *p != '\0'; p++) {
1.16 ! millert 184: if (*p == '.' && dot == NULL)
1.15 millert 185: dot = p;
186: else if (!isdigit((unsigned char)*p))
187: goto terr;
188: }
189: if (dot == NULL)
190: lt->tm_sec = 0; /* Seconds defaults to 0. */
1.1 deraadt 191: else {
1.15 millert 192: *dot++ = '\0';
193: if (strlen(dot) != 2)
194: goto terr;
195: lt->tm_sec = ATOI2(dot);
196: if (lt->tm_sec > 61) /* Could be leap second. */
1.1 deraadt 197: goto terr;
198: }
1.12 deraadt 199:
1.1 deraadt 200: yearset = 0;
1.12 deraadt 201: switch (strlen(arg)) {
1.1 deraadt 202: case 12: /* CCYYMMDDhhmm */
1.15 millert 203: lt->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
1.1 deraadt 204: yearset = 1;
1.8 henning 205: /* FALLTHROUGH */
1.1 deraadt 206: case 10: /* YYMMDDhhmm */
207: if (yearset) {
208: yearset = ATOI2(arg);
1.15 millert 209: lt->tm_year += yearset;
1.1 deraadt 210: } else {
211: yearset = ATOI2(arg);
1.15 millert 212: /* Preserve current century. */
213: lt->tm_year = ((lt->tm_year / 100) * 100) + yearset;
1.1 deraadt 214: }
215: /* FALLTHROUGH */
216: case 8: /* MMDDhhmm */
1.15 millert 217: lt->tm_mon = ATOI2(arg);
218: if (lt->tm_mon > 12 || lt->tm_mon == 0)
219: goto terr;
220: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
221: lt->tm_mday = ATOI2(arg);
222: if (lt->tm_mday > 31 || lt->tm_mday == 0)
223: goto terr;
224: lt->tm_hour = ATOI2(arg);
225: if (lt->tm_hour > 23)
226: goto terr;
227: lt->tm_min = ATOI2(arg);
228: if (lt->tm_min > 59)
229: goto terr;
1.1 deraadt 230: break;
231: default:
232: goto terr;
233: }
234:
1.15 millert 235: lt->tm_isdst = -1; /* Figure out DST. */
236: tvp[0].tv_sec = tvp[1].tv_sec = mktime(lt);
1.1 deraadt 237: if (tvp[0].tv_sec == -1)
238: terr: errx(1,
239: "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
240:
241: tvp[0].tv_usec = tvp[1].tv_usec = 0;
242: }
243:
244: void
1.10 deraadt 245: stime_arg2(char *arg, int year, struct timeval *tvp)
1.1 deraadt 246: {
1.15 millert 247: struct tm *lt;
1.13 henning 248: time_t tmptime;
1.1 deraadt 249: /* Start with the current time. */
250: tmptime = tvp[0].tv_sec;
1.15 millert 251: if ((lt = localtime(&tmptime)) == NULL)
1.1 deraadt 252: err(1, "localtime");
253:
1.15 millert 254: lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */
255: if (lt->tm_mon > 12 || lt->tm_mon == 0)
256: goto terr;
257: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
258: lt->tm_mday = ATOI2(arg);
259: if (lt->tm_mday > 31 || lt->tm_mday == 0)
260: goto terr;
261: lt->tm_hour = ATOI2(arg);
262: if (lt->tm_hour > 23)
263: goto terr;
264: lt->tm_min = ATOI2(arg);
265: if (lt->tm_min > 59)
266: goto terr;
1.4 pjanzen 267: if (year) {
1.15 millert 268: year = ATOI2(arg); /* Preserve current century. */
269: lt->tm_year = ((lt->tm_year / 100) * 100) + year;
1.4 pjanzen 270: }
1.15 millert 271: lt->tm_sec = 0;
1.1 deraadt 272:
1.15 millert 273: lt->tm_isdst = -1; /* Figure out DST. */
274: tvp[0].tv_sec = tvp[1].tv_sec = mktime(lt);
1.1 deraadt 275: if (tvp[0].tv_sec == -1)
1.15 millert 276: terr: errx(1,
1.4 pjanzen 277: "out of range or illegal time specification: MMDDhhmm[YY]");
1.1 deraadt 278:
279: tvp[0].tv_usec = tvp[1].tv_usec = 0;
280: }
281:
282: void
1.10 deraadt 283: stime_file(char *fname, struct timeval *tvp)
1.1 deraadt 284: {
1.13 henning 285: struct stat sb;
1.1 deraadt 286:
287: if (stat(fname, &sb))
288: err(1, "%s", fname);
289: TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
290: TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
291: }
292:
293: __dead void
1.10 deraadt 294: usage(void)
1.1 deraadt 295: {
1.13 henning 296: extern char *__progname;
297:
1.1 deraadt 298: (void)fprintf(stderr,
1.13 henning 299: "usage: %s [-acm] [-r file] [-t time] file ...\n", __progname);
1.1 deraadt 300: exit(1);
301: }