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