touch.c revision 1.5 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.5 1993/08/01 18:04:42 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 int rw __P((char *, struct stat *, int));
59 void stime_arg1 __P((char *, struct timeval *));
60 void stime_arg2 __P((char *, int, struct timeval *));
61 void stime_file __P((char *, struct timeval *));
62 void usage __P((void));
63
64 /*temporary we aren't 4.4 portability hack*/
65 #define TIMET_TO_TIMEVAL(timevalp, tp) (timevalp)->tv_sec = *(tp);
66
67 int
68 main(argc, argv)
69 int argc;
70 char *argv[];
71 {
72 struct stat sb;
73 struct timeval tv[2];
74 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
75 char *p;
76
77 aflag = cflag = fflag = mflag = timeset = 0;
78 if (gettimeofday(&tv[0], NULL))
79 err(1, "gettimeofday");
80
81 while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
82 switch(ch) {
83 case 'a':
84 aflag = 1;
85 break;
86 case 'c':
87 cflag = 1;
88 break;
89 case 'f':
90 fflag = 1;
91 break;
92 case 'm':
93 mflag = 1;
94 break;
95 case 'r':
96 timeset = 1;
97 stime_file(optarg, tv);
98 break;
99 case 't':
100 timeset = 1;
101 stime_arg1(optarg, tv);
102 break;
103 case '?':
104 default:
105 usage();
106 }
107 argc -= optind;
108 argv += optind;
109
110 /* Default is both -a and -m. */
111 if (aflag == 0 && mflag == 0)
112 aflag = mflag = 1;
113
114 /*
115 * If no -r or -t flag, at least two operands, the first of which
116 * is an 8 or 10 digit number, use the obsolete time specification.
117 */
118 if (!timeset && argc > 1) {
119 (void)strtol(argv[0], &p, 10);
120 len = p - argv[0];
121 if (*p == '\0' && (len == 8 || len == 10)) {
122 timeset = 1;
123 stime_arg2(argv[0], len == 10, tv);
124 argv++;
125 }
126 }
127
128 /* Otherwise use the current time of day. */
129 if (!timeset)
130 tv[1] = tv[0];
131
132 if (*argv == NULL)
133 usage();
134
135 for (rval = 0; *argv; ++argv) {
136 /* See if the file exists. */
137 if (stat(*argv, &sb))
138 if (!cflag) {
139 /* Create the file. */
140 fd = open(*argv,
141 O_WRONLY | O_CREAT, DEFFILEMODE);
142 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
143 rval = 1;
144 warn("%s", *argv);
145 continue;
146 }
147
148 /* If using the current time, we're done. */
149 if (!timeset)
150 continue;
151 } else
152 continue;
153
154 if (!aflag)
155 /* TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);*/
156 TIMET_TO_TIMEVAL(&tv[0], &sb.st_atime);
157 if (!mflag)
158 /* TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);*/
159 TIMET_TO_TIMEVAL(&tv[1], &sb.st_mtime);
160
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 *= 1000;
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 TIMET_TO_TIMEVAL(tvp, &sb.st_atime);
290 /* TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);*/
291 TIMET_TO_TIMEVAL(tvp + 1, &sb.st_mtime);
292 }
293
294 int
295 rw(fname, sbp, force)
296 char *fname;
297 struct stat *sbp;
298 int force;
299 {
300 int fd, needed_chmod, rval;
301 u_char byte;
302
303 /* Try regular files and directories. */
304 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
305 warnx("%s: %s", fname, strerror(EFTYPE));
306 return (1);
307 }
308
309 needed_chmod = rval = 0;
310 if ((fd = open(fname, O_RDWR, 0)) == -1) {
311 if (!force || chmod(fname, DEFFILEMODE))
312 goto err;
313 if ((fd = open(fname, O_RDWR, 0)) == -1)
314 goto err;
315 needed_chmod = 1;
316 }
317
318 if (sbp->st_size != 0) {
319 if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
320 goto err;
321 if (lseek(fd, (off_t)0, SEEK_SET) == -1)
322 goto err;
323 if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
324 goto err;
325 } else {
326 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
327 err: rval = 1;
328 warn("%s", fname);
329 } else if (ftruncate(fd, (off_t)0)) {
330 rval = 1;
331 warn("%s: file modified", fname);
332 }
333 }
334
335 if (close(fd) && rval != 1) {
336 rval = 1;
337 warn("%s", fname);
338 }
339 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
340 rval = 1;
341 warn("%s: permissions modified", fname);
342 }
343 return (rval);
344 }
345
346 volatile void
347 usage()
348 {
349 (void)fprintf(stderr,
350 "usage: touch [-acfm] [-r file] [-t time] file ...\n");
351 exit(1);
352 }
353