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