touch.c revision 1.8 1 /*
2 * Copyright (c) 1993 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1993 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)touch.c 5.5 (Berkeley) 3/7/93";*/
42 static char rcsid[] = "$Id: touch.c,v 1.8 1993/12/31 19:33:02 jtc Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <locale.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <err.h>
58
59 int rw __P((char *, struct stat *, int));
60 void stime_arg1 __P((char *, struct timeval *));
61 void stime_arg2 __P((char *, int, struct timeval *));
62 void stime_file __P((char *, struct timeval *));
63 void usage __P((void));
64
65 #ifndef notyet
66 #define TIMET_TO_TIMEVAL(timevalp, tp) (timevalp)->tv_sec = *(tp);
67 #endif
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[0], len == 10, tv);
128 argv++;
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 #ifdef notyet
160 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
161 #else
162 TIMET_TO_TIMEVAL(&tv[0], &sb.st_atime);
163 #endif
164 if (!mflag)
165 #ifdef notyet
166 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
167 #else
168 TIMET_TO_TIMEVAL(&tv[1], &sb.st_mtime);
169 #endif
170
171
172 /* Try utimes(2). */
173 if (!utimes(*argv, tv))
174 continue;
175
176 /* If the user specified a time, nothing else we can do. */
177 if (timeset) {
178 rval = 1;
179 warn("%s", *argv);
180 continue;
181 }
182
183 /*
184 * System V and POSIX 1003.1 require that a NULL argument
185 * set the access/modification times to the current time.
186 * The permission checks are different, too, in that the
187 * ability to write the file is sufficient. Take a shot.
188 */
189 if (!utimes(*argv, NULL))
190 continue;
191
192 /* Try reading/writing. */
193 if (rw(*argv, &sb, fflag))
194 rval = 1;
195 }
196 exit(rval);
197 }
198
199 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
200
201 void
202 stime_arg1(arg, tvp)
203 char *arg;
204 struct timeval *tvp;
205 {
206 struct tm *t;
207 int yearset;
208 char *p;
209 /* Start with the current time. */
210 if ((t = localtime(&tvp[0].tv_sec)) == NULL)
211 err(1, "localtime");
212 /* [[CC]YY]MMDDhhmm[.SS] */
213 if ((p = strchr(arg, '.')) == NULL)
214 t->tm_sec = 0; /* Seconds defaults to 0. */
215 else {
216 if (strlen(p + 1) != 2)
217 goto terr;
218 *p++ = '\0';
219 t->tm_sec = ATOI2(p);
220 }
221
222 yearset = 0;
223 switch(strlen(arg)) {
224 case 12: /* CCYYMMDDhhmm */
225 t->tm_year = ATOI2(arg);
226 t->tm_year *= 100;
227 yearset = 1;
228 /* FALLTHOUGH */
229 case 10: /* YYMMDDhhmm */
230 if (yearset) {
231 yearset = ATOI2(arg);
232 t->tm_year += yearset;
233 } else {
234 yearset = ATOI2(arg);
235 if (yearset < 69)
236 t->tm_year = yearset + 2000;
237 else
238 t->tm_year = yearset + 1900;
239 }
240 t->tm_year -= 1900; /* Convert to UNIX time. */
241 /* FALLTHROUGH */
242 case 8: /* MMDDhhmm */
243 t->tm_mon = ATOI2(arg);
244 --t->tm_mon; /* Convert from 01-12 to 00-11 */
245 t->tm_mday = ATOI2(arg);
246 t->tm_hour = ATOI2(arg);
247 t->tm_min = ATOI2(arg);
248 break;
249 default:
250 goto terr;
251 }
252
253 t->tm_isdst = -1; /* Figure out DST. */
254 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
255 if (tvp[0].tv_sec == -1)
256 terr: errx(1,
257 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
258
259 tvp[0].tv_usec = tvp[1].tv_usec = 0;
260 }
261
262 void
263 stime_arg2(arg, year, tvp)
264 char *arg;
265 int year;
266 struct timeval *tvp;
267 {
268 struct tm *t;
269 /* Start with the current time. */
270 if ((t = localtime(&tvp[0].tv_sec)) == NULL)
271 err(1, "localtime");
272
273 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
274 --t->tm_mon; /* Convert from 01-12 to 00-11 */
275 t->tm_mday = ATOI2(arg);
276 t->tm_hour = ATOI2(arg);
277 t->tm_min = ATOI2(arg);
278 if (year)
279 t->tm_year = ATOI2(arg);
280
281 t->tm_isdst = -1; /* Figure out DST. */
282 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
283 if (tvp[0].tv_sec == -1)
284 errx(1,
285 "out of range or illegal time specification: MMDDhhmm[yy]");
286
287 tvp[0].tv_usec = tvp[1].tv_usec = 0;
288 }
289
290 void
291 stime_file(fname, tvp)
292 char *fname;
293 struct timeval *tvp;
294 {
295 struct stat sb;
296
297 if (stat(fname, &sb))
298 err(1, "%s", fname);
299 #ifdef notyet
300 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
301 #else
302 TIMET_TO_TIMEVAL(tvp, &sb.st_atime);
303 #endif
304 #ifdef notyet
305 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
306 #else
307 TIMET_TO_TIMEVAL(tvp + 1, &sb.st_mtime);
308 #endif
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 volatile void
364 usage()
365 {
366 (void)fprintf(stderr,
367 "usage: touch [-acfm] [-r file] [-t time] file ...\n");
368 exit(1);
369 }
370