Annotation of src/usr.bin/touch/touch.c, Revision 1.3
1.3 ! millert 1: /* $OpenBSD: touch.c,v 1.2 1996/06/26 05:41:45 deraadt 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)touch.c 8.2 (Berkeley) 4/28/95";
46: #endif
1.3 ! millert 47: static char rcsid[] = "$OpenBSD: touch.c,v 1.2 1996/06/26 05:41:45 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/stat.h>
52: #include <sys/time.h>
53:
54: #include <err.h>
55: #include <errno.h>
56: #include <fcntl.h>
57: #include <stdio.h>
58: #include <stdlib.h>
59: #include <string.h>
60: #include <locale.h>
61: #include <time.h>
62: #include <unistd.h>
63:
64: int rw __P((char *, struct stat *, int));
65: void stime_arg1 __P((char *, struct timeval *));
66: void stime_arg2 __P((char *, int, struct timeval *));
67: void stime_file __P((char *, struct timeval *));
68: void usage __P((void));
69:
70: int
71: main(argc, argv)
72: int argc;
73: char *argv[];
74: {
75: struct stat sb;
76: struct timeval tv[2];
77: int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
78: char *p;
79:
80: setlocale(LC_ALL, "");
81:
82: aflag = cflag = fflag = mflag = timeset = 0;
83: if (gettimeofday(&tv[0], NULL))
84: err(1, "gettimeofday");
85:
1.3 ! millert 86: while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
1.1 deraadt 87: switch(ch) {
88: case 'a':
89: aflag = 1;
90: break;
91: case 'c':
92: cflag = 1;
93: break;
94: case 'f':
95: fflag = 1;
96: break;
97: case 'm':
98: mflag = 1;
99: break;
100: case 'r':
101: timeset = 1;
102: stime_file(optarg, tv);
103: break;
104: case 't':
105: timeset = 1;
106: stime_arg1(optarg, tv);
107: break;
108: case '?':
109: default:
110: usage();
111: }
112: argc -= optind;
113: argv += optind;
114:
115: /* Default is both -a and -m. */
116: if (aflag == 0 && mflag == 0)
117: aflag = mflag = 1;
118:
119: /*
120: * If no -r or -t flag, at least two operands, the first of which
121: * is an 8 or 10 digit number, use the obsolete time specification.
122: */
123: if (!timeset && argc > 1) {
124: (void)strtol(argv[0], &p, 10);
125: len = p - argv[0];
126: if (*p == '\0' && (len == 8 || len == 10)) {
127: timeset = 1;
128: stime_arg2(*argv++, len == 10, tv);
129: }
130: }
131:
132: /* Otherwise use the current time of day. */
133: if (!timeset)
134: tv[1] = tv[0];
135:
136: if (*argv == NULL)
137: usage();
138:
139: for (rval = 0; *argv; ++argv) {
140: /* See if the file exists. */
141: if (stat(*argv, &sb))
142: if (!cflag) {
143: /* Create the file. */
144: fd = open(*argv,
145: O_WRONLY | O_CREAT, DEFFILEMODE);
146: if (fd == -1 || fstat(fd, &sb) || close(fd)) {
147: rval = 1;
148: warn("%s", *argv);
149: continue;
150: }
151:
152: /* If using the current time, we're done. */
153: if (!timeset)
154: continue;
155: } else
156: continue;
157:
158: if (!aflag)
159: TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
160: if (!mflag)
161: TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
162:
163: /* Try utimes(2). */
164: if (!utimes(*argv, tv))
165: continue;
166:
167: /* If the user specified a time, nothing else we can do. */
168: if (timeset) {
169: rval = 1;
170: warn("%s", *argv);
171: }
172:
173: /*
174: * System V and POSIX 1003.1 require that a NULL argument
175: * set the access/modification times to the current time.
176: * The permission checks are different, too, in that the
177: * ability to write the file is sufficient. Take a shot.
178: */
179: if (!utimes(*argv, NULL))
180: continue;
181:
182: /* Try reading/writing. */
183: if (rw(*argv, &sb, fflag))
184: rval = 1;
185: }
186: exit(rval);
187: }
188:
189: #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
190:
191: void
192: stime_arg1(arg, tvp)
193: char *arg;
194: struct timeval *tvp;
195: {
196: struct tm *t;
197: time_t tmptime;
198: int yearset;
199: char *p;
200: /* Start with the current time. */
201: tmptime = tvp[0].tv_sec;
202: if ((t = localtime(&tmptime)) == NULL)
203: err(1, "localtime");
204: /* [[CC]YY]MMDDhhmm[.SS] */
205: if ((p = strchr(arg, '.')) == NULL)
206: t->tm_sec = 0; /* Seconds defaults to 0. */
207: else {
208: if (strlen(p + 1) != 2)
209: goto terr;
210: *p++ = '\0';
211: t->tm_sec = ATOI2(p);
212: }
213:
214: yearset = 0;
215: switch(strlen(arg)) {
216: case 12: /* CCYYMMDDhhmm */
217: t->tm_year = ATOI2(arg);
218: t->tm_year *= 100;
219: yearset = 1;
220: /* FALLTHOUGH */
221: case 10: /* YYMMDDhhmm */
222: if (yearset) {
223: yearset = ATOI2(arg);
224: t->tm_year += yearset;
225: } else {
226: yearset = ATOI2(arg);
227: if (yearset < 69)
228: t->tm_year = yearset + 2000;
229: else
230: t->tm_year = yearset + 1900;
231: }
232: t->tm_year -= 1900; /* Convert to UNIX time. */
233: /* FALLTHROUGH */
234: case 8: /* MMDDhhmm */
235: t->tm_mon = ATOI2(arg);
236: --t->tm_mon; /* Convert from 01-12 to 00-11 */
237: t->tm_mday = ATOI2(arg);
238: t->tm_hour = ATOI2(arg);
239: t->tm_min = ATOI2(arg);
240: break;
241: default:
242: goto terr;
243: }
244:
245: t->tm_isdst = -1; /* Figure out DST. */
246: tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
247: if (tvp[0].tv_sec == -1)
248: terr: errx(1,
249: "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
250:
251: tvp[0].tv_usec = tvp[1].tv_usec = 0;
252: }
253:
254: void
255: stime_arg2(arg, year, tvp)
256: char *arg;
257: int year;
258: struct timeval *tvp;
259: {
260: struct tm *t;
261: time_t tmptime;
262: /* Start with the current time. */
263: tmptime = tvp[0].tv_sec;
264: if ((t = localtime(&tmptime)) == NULL)
265: err(1, "localtime");
266:
267: t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
268: --t->tm_mon; /* Convert from 01-12 to 00-11 */
269: t->tm_mday = ATOI2(arg);
270: t->tm_hour = ATOI2(arg);
271: t->tm_min = ATOI2(arg);
272: if (year)
273: t->tm_year = ATOI2(arg);
274:
275: t->tm_isdst = -1; /* Figure out DST. */
276: tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
277: if (tvp[0].tv_sec == -1)
278: errx(1,
279: "out of range or illegal time specification: MMDDhhmm[yy]");
280:
281: tvp[0].tv_usec = tvp[1].tv_usec = 0;
282: }
283:
284: void
285: stime_file(fname, tvp)
286: char *fname;
287: struct timeval *tvp;
288: {
289: struct stat sb;
290:
291: if (stat(fname, &sb))
292: err(1, "%s", fname);
293: TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
294: TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
295: }
296:
297: int
298: rw(fname, sbp, force)
299: char *fname;
300: struct stat *sbp;
301: int force;
302: {
303: int fd, needed_chmod, rval;
304: u_char byte;
305:
306: /* Try regular files and directories. */
307: if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
308: warnx("%s: %s", fname, strerror(EFTYPE));
309: return (1);
310: }
311:
312: needed_chmod = rval = 0;
313: if ((fd = open(fname, O_RDWR, 0)) == -1) {
314: if (!force || chmod(fname, DEFFILEMODE))
315: goto err;
316: if ((fd = open(fname, O_RDWR, 0)) == -1)
317: goto err;
318: needed_chmod = 1;
319: }
320:
321: if (sbp->st_size != 0) {
322: if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
323: goto err;
324: if (lseek(fd, (off_t)0, SEEK_SET) == -1)
325: goto err;
326: if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
327: goto err;
328: } else {
329: if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
330: err: rval = 1;
331: warn("%s", fname);
332: } else if (ftruncate(fd, (off_t)0)) {
333: rval = 1;
334: warn("%s: file modified", fname);
335: }
336: }
337:
338: if (close(fd) && rval != 1) {
339: rval = 1;
340: warn("%s", fname);
341: }
342: if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
343: rval = 1;
344: warn("%s: permissions modified", fname);
345: }
346: return (rval);
347: }
348:
349: __dead void
350: usage()
351: {
352: (void)fprintf(stderr,
353: "usage: touch [-acfm] [-r file] [-t time] file ...\n");
354: exit(1);
355: }