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