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