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