touch.c revision 1.19 1 /* $NetBSD: touch.c,v 1.19 1998/01/20 21:04:04 mycroft 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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.2 (Berkeley) 4/28/95";
45 #endif
46 __RCSID("$NetBSD: touch.c,v 1.19 1998/01/20 21:04:04 mycroft 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 <tzfile.h>
62 #include <unistd.h>
63
64 int main __P((int, char **));
65 int rw __P((char *, struct stat *, int));
66 void stime_arg1 __P((char *, struct timeval *));
67 void stime_arg2 __P((char *, int, struct timeval *));
68 void stime_file __P((char *, struct timeval *));
69 void usage __P((void));
70
71 int
72 main(argc, argv)
73 int argc;
74 char *argv[];
75 {
76 struct stat sb;
77 struct timeval tv[2];
78 int aflag, cflag, fflag, hflag, mflag, ch, fd, len, rval, timeset;
79 char *p;
80 int (*change_file_times) __P((const char *, const struct timeval *));
81 int (*get_file_status) __P((const char *, struct stat *));
82
83 setlocale(LC_ALL, "");
84
85 aflag = cflag = fflag = hflag = mflag = timeset = 0;
86 if (gettimeofday(&tv[0], NULL))
87 err(1, "gettimeofday");
88
89 while ((ch = getopt(argc, argv, "acfhmr:t:")) != -1)
90 switch(ch) {
91 case 'a':
92 aflag = 1;
93 break;
94 case 'c':
95 cflag = 1;
96 break;
97 case 'f':
98 fflag = 1;
99 break;
100 case 'h':
101 hflag = 1;
102 break;
103 case 'm':
104 mflag = 1;
105 break;
106 case 'r':
107 timeset = 1;
108 stime_file(optarg, tv);
109 break;
110 case 't':
111 timeset = 1;
112 stime_arg1(optarg, tv);
113 break;
114 case '?':
115 default:
116 usage();
117 }
118 argc -= optind;
119 argv += optind;
120
121 /* Default is both -a and -m. */
122 if (aflag == 0 && mflag == 0)
123 aflag = mflag = 1;
124
125 if (hflag) {
126 cflag = 1; /* Don't create new file */
127 change_file_times = lutimes;
128 get_file_status = lstat;
129 } else {
130 change_file_times = utimes;
131 get_file_status = stat;
132 }
133
134 /*
135 * If no -r or -t flag, at least two operands, the first of which
136 * is an 8 or 10 digit number, use the obsolete time specification.
137 */
138 if (!timeset && argc > 1) {
139 (void)strtol(argv[0], &p, 10);
140 len = p - argv[0];
141 if (*p == '\0' && (len == 8 || len == 10)) {
142 timeset = 1;
143 stime_arg2(*argv++, len == 10, tv);
144 }
145 }
146
147 /* Otherwise use the current time of day. */
148 if (!timeset)
149 tv[1] = tv[0];
150
151 if (*argv == NULL)
152 usage();
153
154 for (rval = 0; *argv; ++argv) {
155 /* See if the file exists. */
156 if ((*get_file_status)(*argv, &sb))
157 if (!cflag) {
158 /* Create the file. */
159 fd = open(*argv,
160 O_WRONLY | O_CREAT, DEFFILEMODE);
161 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
162 rval = 1;
163 warn("%s", *argv);
164 continue;
165 }
166
167 /* If using the current time, we're done. */
168 if (!timeset)
169 continue;
170 } else
171 continue;
172
173 if (!aflag)
174 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
175 if (!mflag)
176 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
177
178 /* Try utimes(2). */
179 if (!(*change_file_times)(*argv, tv))
180 continue;
181
182 /* If the user specified a time, nothing else we can do. */
183 if (timeset) {
184 rval = 1;
185 warn("%s", *argv);
186 }
187
188 /*
189 * System V and POSIX 1003.1 require that a NULL argument
190 * set the access/modification times to the current time.
191 * The permission checks are different, too, in that the
192 * ability to write the file is sufficient. Take a shot.
193 */
194 if (!(*change_file_times)(*argv, NULL))
195 continue;
196
197 /* Try reading/writing. */
198 if (!S_ISLNK(sb.st_mode) && rw(*argv, &sb, fflag))
199 rval = 1;
200 }
201 exit(rval);
202 }
203
204 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
205
206 void
207 stime_arg1(arg, tvp)
208 char *arg;
209 struct timeval *tvp;
210 {
211 struct tm *t;
212 time_t tmptime;
213 int yearset;
214 char *p;
215 /* Start with the current time. */
216 tmptime = tvp[0].tv_sec;
217 if ((t = localtime(&tmptime)) == NULL)
218 err(1, "localtime");
219 /* [[CC]YY]MMDDhhmm[.SS] */
220 if ((p = strchr(arg, '.')) == NULL)
221 t->tm_sec = 0; /* Seconds defaults to 0. */
222 else {
223 if (strlen(p + 1) != 2)
224 goto terr;
225 *p++ = '\0';
226 t->tm_sec = ATOI2(p);
227 }
228
229 yearset = 0;
230 switch(strlen(arg)) {
231 case 12: /* CCYYMMDDhhmm */
232 t->tm_year = ATOI2(arg);
233 t->tm_year *= 100;
234 yearset = 1;
235 /* FALLTHOUGH */
236 case 10: /* YYMMDDhhmm */
237 if (yearset) {
238 yearset = ATOI2(arg);
239 t->tm_year += yearset;
240 } else {
241 yearset = ATOI2(arg);
242 if (yearset < 69)
243 t->tm_year = yearset + 2000;
244 else
245 t->tm_year = yearset + 1900;
246 }
247 t->tm_year -= TM_YEAR_BASE; /* Convert to UNIX time. */
248 /* FALLTHROUGH */
249 case 8: /* MMDDhhmm */
250 t->tm_mon = ATOI2(arg);
251 --t->tm_mon; /* Convert from 01-12 to 00-11 */
252 t->tm_mday = ATOI2(arg);
253 t->tm_hour = ATOI2(arg);
254 t->tm_min = ATOI2(arg);
255 break;
256 default:
257 goto terr;
258 }
259
260 t->tm_isdst = -1; /* Figure out DST. */
261 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
262 if (tvp[0].tv_sec == -1)
263 terr: errx(1,
264 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
265
266 tvp[0].tv_usec = tvp[1].tv_usec = 0;
267 }
268
269 void
270 stime_arg2(arg, year, tvp)
271 char *arg;
272 int year;
273 struct timeval *tvp;
274 {
275 struct tm *t;
276 time_t tmptime;
277 /* Start with the current time. */
278 tmptime = tvp[0].tv_sec;
279 if ((t = localtime(&tmptime)) == NULL)
280 err(1, "localtime");
281
282 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
283 --t->tm_mon; /* Convert from 01-12 to 00-11 */
284 t->tm_mday = ATOI2(arg);
285 t->tm_hour = ATOI2(arg);
286 t->tm_min = ATOI2(arg);
287 if (year)
288 t->tm_year = ATOI2(arg);
289
290 t->tm_isdst = -1; /* Figure out DST. */
291 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
292 if (tvp[0].tv_sec == -1)
293 errx(1,
294 "out of range or illegal time specification: MMDDhhmm[yy]");
295
296 tvp[0].tv_usec = tvp[1].tv_usec = 0;
297 }
298
299 void
300 stime_file(fname, tvp)
301 char *fname;
302 struct timeval *tvp;
303 {
304 struct stat sb;
305
306 if (stat(fname, &sb))
307 err(1, "%s", fname);
308 TIMESPEC_TO_TIMEVAL(&tvp[0], &sb.st_atimespec);
309 TIMESPEC_TO_TIMEVAL(&tvp[1], &sb.st_mtimespec);
310 }
311
312 int
313 rw(fname, sbp, force)
314 char *fname;
315 struct stat *sbp;
316 int force;
317 {
318 int fd, needed_chmod, rval;
319 u_char byte;
320
321 /* Try regular files and directories. */
322 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
323 warnx("%s: %s", fname, strerror(EFTYPE));
324 return (1);
325 }
326
327 needed_chmod = rval = 0;
328 if ((fd = open(fname, O_RDWR, 0)) == -1) {
329 if (!force || chmod(fname, DEFFILEMODE))
330 goto err;
331 if ((fd = open(fname, O_RDWR, 0)) == -1)
332 goto err;
333 needed_chmod = 1;
334 }
335
336 if (sbp->st_size != 0) {
337 if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
338 goto err;
339 if (lseek(fd, (off_t)0, SEEK_SET) == -1)
340 goto err;
341 if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
342 goto err;
343 } else {
344 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
345 err: rval = 1;
346 warn("%s", fname);
347 } else if (ftruncate(fd, (off_t)0)) {
348 rval = 1;
349 warn("%s: file modified", fname);
350 }
351 }
352
353 if (close(fd) && rval != 1) {
354 rval = 1;
355 warn("%s", fname);
356 }
357 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
358 rval = 1;
359 warn("%s: permissions modified", fname);
360 }
361 return (rval);
362 }
363
364 __dead void
365 usage()
366 {
367 (void)fprintf(stderr,
368 "usage: touch [-acfhm] [-r file] [-t time] file ...\n");
369 exit(1);
370 }
371