file_cmds-45.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) && rw(*argv, &sb, fflag))
205 rval = 1;
206 }
207 exit(rval);
208 }
209
210 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
211
212 void
213 stime_arg1(arg, tvp)
214 char *arg;
215 struct timeval *tvp;
216 {
217 struct tm *t;
218 time_t tmptime;
219 int yearset;
220 char *p;
221 /* Start with the current time. */
222 tmptime = tvp[0].tv_sec;
223 if ((t = localtime(&tmptime)) == NULL)
224 err(1, "localtime");
225 /* [[CC]YY]MMDDhhmm[.SS] */
226 if ((p = strchr(arg, '.')) == NULL)
227 t->tm_sec = 0; /* Seconds defaults to 0. */
228 else {
229 if (strlen(p + 1) != 2)
230 goto terr;
231 *p++ = '\0';
232 t->tm_sec = ATOI2(p);
233 }
234
235 yearset = 0;
236 switch (strlen(arg)) {
237 case 12: /* CCYYMMDDhhmm */
238 t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
239 yearset = 1;
240 /* FALLTHOUGH */
241 case 10: /* YYMMDDhhmm */
242 if (yearset) {
243 t->tm_year += ATOI2(arg);
244 } else {
245 yearset = ATOI2(arg);
246 if (yearset < 69)
247 t->tm_year = yearset + 2000 - TM_YEAR_BASE;
248 else
249 t->tm_year = yearset + 1900 - TM_YEAR_BASE;
250 }
251 /* FALLTHROUGH */
252 case 8: /* MMDDhhmm */
253 t->tm_mon = ATOI2(arg);
254 --t->tm_mon; /* Convert from 01-12 to 00-11 */
255 /* FALLTHROUGH */
256 case 6:
257 t->tm_mday = ATOI2(arg);
258 /* FALLTHROUGH */
259 case 4:
260 t->tm_hour = ATOI2(arg);
261 /* FALLTHROUGH */
262 case 2:
263 t->tm_min = ATOI2(arg);
264 break;
265 default:
266 goto terr;
267 }
268
269 t->tm_isdst = -1; /* Figure out DST. */
270 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
271 if (tvp[0].tv_sec == -1)
272 terr: errx(1,
273 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
274
275 tvp[0].tv_usec = tvp[1].tv_usec = 0;
276 }
277
278 void
279 stime_arg2(arg, year, tvp)
280 char *arg;
281 int year;
282 struct timeval *tvp;
283 {
284 struct tm *t;
285 time_t tmptime;
286 /* Start with the current time. */
287 tmptime = tvp[0].tv_sec;
288 if ((t = localtime(&tmptime)) == NULL)
289 err(1, "localtime");
290
291 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
292 --t->tm_mon; /* Convert from 01-12 to 00-11 */
293 t->tm_mday = ATOI2(arg);
294 t->tm_hour = ATOI2(arg);
295 t->tm_min = ATOI2(arg);
296 if (year) {
297 year = ATOI2(arg);
298 if (year < 69)
299 t->tm_year = year + 2000 - TM_YEAR_BASE;
300 else
301 t->tm_year = year + 1900 - TM_YEAR_BASE;
302 }
303
304 t->tm_isdst = -1; /* Figure out DST. */
305 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
306 if (tvp[0].tv_sec == -1)
307 errx(1,
308 "out of range or illegal time specification: MMDDhhmm[yy]");
309
310 tvp[0].tv_usec = tvp[1].tv_usec = 0;
311 }
312
313 void
314 stime_file(fname, tvp)
315 char *fname;
316 struct timeval *tvp;
317 {
318 struct stat sb;
319
320 if (stat(fname, &sb))
321 err(1, "%s", fname);
322 TIMESPEC_TO_TIMEVAL(&tvp[0], &sb.st_atimespec);
323 TIMESPEC_TO_TIMEVAL(&tvp[1], &sb.st_mtimespec);
324 }
325
326 int
327 rw(fname, sbp, force)
328 char *fname;
329 struct stat *sbp;
330 int force;
331 {
332 int fd, needed_chmod, rval;
333 u_char byte;
334
335 /* Try regular files and directories. */
336 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
337 warnx("%s: %s", fname, strerror(EFTYPE));
338 return (1);
339 }
340
341 needed_chmod = rval = 0;
342 if ((fd = open(fname, O_RDWR, 0)) == -1) {
343 if (!force || chmod(fname, DEFFILEMODE))
344 goto err;
345 if ((fd = open(fname, O_RDWR, 0)) == -1)
346 goto err;
347 needed_chmod = 1;
348 }
349
350 if (sbp->st_size != 0) {
351 if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
352 goto err;
353 if (lseek(fd, (off_t)0, SEEK_SET) == -1)
354 goto err;
355 if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
356 goto err;
357 } else {
358 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
359 err: rval = 1;
360 warn("%s", fname);
361 } else if (ftruncate(fd, (off_t)0)) {
362 rval = 1;
363 warn("%s: file modified", fname);
364 }
365 }
366
367 if (close(fd) && rval != 1) {
368 rval = 1;
369 warn("%s", fname);
370 }
371 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
372 rval = 1;
373 warn("%s: permissions modified", fname);
374 }
375 return (rval);
376 }
377
378 __dead void
379 usage()
380 {
381 #ifndef __APPLE__
382 (void)fprintf(stderr,
383 "usage: touch [-acfhm] [-r file] [-t time] file ...\n");
384 #else
385 (void)fprintf(stderr,
386 "usage: touch [-acfm] [-r file] [-t time] file ...\n");
387 #endif
388 exit(1);
389 }