touch.c revision 1.38 1 /* $NetBSD: touch.c,v 1.38 2024/02/08 02:54:07 kre 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)touch.c 8.2 (Berkeley) 4/28/95";
41 #endif
42 __RCSID("$NetBSD: touch.c,v 1.38 2024/02/08 02:54:07 kre Exp $");
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <limits.h>
54 #include <math.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <locale.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62 #include <util.h>
63 #include <getopt.h>
64
65 static void stime_arg0(const char *, struct timespec *);
66 static void stime_arg1(char *, struct timespec *);
67 static void stime_arg2(const char *, int, struct timespec *);
68 static void stime_file(const char *, struct timespec *);
69 static int stime_posix(const char *, struct timespec *);
70 static int difftm(const struct tm *, const struct tm *);
71 __dead static void usage(void);
72
73 struct option touch_longopts[] = {
74 { "date", required_argument, 0,
75 'd' },
76 { "reference", required_argument, 0,
77 'r' },
78 { NULL, 0, 0,
79 0 },
80 };
81
82 #define YEAR_BOUNDARY 69
83 #define LOW_YEAR_CENTURY 2000 /* for 2 digit years < YEAR_BOUNDARY */
84 #define HIGH_YEAR_CENTURY 1900 /* for 2 digit years >= " */
85
86 #define NO_TIME ((time_t)-1) /* time_t might be unsigned */
87
88 int
89 main(int argc, char *argv[])
90 {
91 struct stat sb;
92 struct timespec ts[2];
93 int aflag, cflag, hflag, mflag, ch, fd, len, rval, timeset;
94 char *p;
95 int (*change_file_times)(const char *, const struct timespec *);
96 int (*get_file_status)(const char *, struct stat *);
97
98 setlocale(LC_ALL, "");
99
100 aflag = cflag = hflag = mflag = timeset = 0;
101 if (clock_gettime(CLOCK_REALTIME, &ts[0]))
102 err(1, "clock_gettime");
103
104 while ((ch = getopt_long(argc, argv, "acd:fhmr:t:", touch_longopts,
105 NULL)) != -1)
106 switch (ch) {
107 case 'a':
108 aflag = 1;
109 break;
110 case 'c':
111 cflag = 1;
112 break;
113 case 'd':
114 timeset = 1;
115 if (!stime_posix(optarg, ts))
116 stime_arg0(optarg, ts);
117 break;
118 case 'f':
119 break;
120 case 'h':
121 hflag = 1;
122 break;
123 case 'm':
124 mflag = 1;
125 break;
126 case 'r':
127 timeset = 1;
128 stime_file(optarg, ts);
129 break;
130 case 't':
131 timeset = 1;
132 stime_arg1(optarg, ts);
133 break;
134 case '?':
135 default:
136 usage();
137 }
138 argc -= optind;
139 argv += optind;
140
141 /* Default is both -a and -m. */
142 if (aflag == 0 && mflag == 0)
143 aflag = mflag = 1;
144
145 if (hflag) {
146 cflag = 1; /* Don't create new file */
147 change_file_times = lutimens;
148 get_file_status = lstat;
149 } else {
150 change_file_times = utimens;
151 get_file_status = stat;
152 }
153
154 /*
155 * If no -r or -t flag, at least two operands, the first of which
156 * is an 8 or 10 digit number, use the obsolete time specification.
157 */
158 if (!timeset && argc > 1) {
159 (void)strtol(argv[0], &p, 10);
160 len = p - argv[0];
161 if (*p == '\0' && (len == 8 || len == 10)) {
162 timeset = 1;
163 stime_arg2(*argv++, len == 10, ts);
164 }
165 }
166
167 /* Otherwise use the current time of day. */
168 if (!timeset)
169 ts[1] = ts[0];
170
171 if (*argv == NULL)
172 usage();
173
174 for (rval = EXIT_SUCCESS; *argv; ++argv) {
175 /* See if the file exists. */
176 if ((*get_file_status)(*argv, &sb)) {
177 if (!cflag) {
178 /* Create the file. */
179 fd = open(*argv,
180 O_WRONLY | O_CREAT, DEFFILEMODE);
181 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
182 rval = EXIT_FAILURE;
183 warn("%s", *argv);
184 continue;
185 }
186
187 /* If using the current time, we're done. */
188 if (!timeset)
189 continue;
190 } else
191 continue;
192 }
193 if (!aflag)
194 ts[0] = sb.st_atimespec;
195 if (!mflag)
196 ts[1] = sb.st_mtimespec;
197
198 /* Try utimes(2). */
199 if (!(*change_file_times)(*argv, ts))
200 continue;
201
202 /* If the user specified a time, nothing else we can do. */
203 if (timeset) {
204 rval = EXIT_FAILURE;
205 warn("%s", *argv);
206 }
207
208 /*
209 * System V and POSIX 1003.1 require that a NULL argument
210 * set the access/modification times to the current time.
211 * The permission checks are different, too, in that the
212 * ability to write the file is sufficient. Take a shot.
213 */
214 if (!(*change_file_times)(*argv, NULL))
215 continue;
216
217 rval = EXIT_FAILURE;
218 warn("%s", *argv);
219 }
220 exit(rval);
221 }
222
223 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
224
225 static void
226 stime_arg0(const char *arg, struct timespec *tsp)
227 {
228 tsp[1].tv_sec = tsp[0].tv_sec = parsedate(arg, NULL, NULL);
229 if (tsp[0].tv_sec == NO_TIME)
230 errx(EXIT_FAILURE, "Could not parse `%s'", arg);
231 tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
232 }
233
234 static void
235 stime_arg1(char *arg, struct timespec *tsp)
236 {
237 struct tm *t, tm;
238 time_t tmptime;
239 int yearset;
240 char *p;
241 char *initarg = arg;
242
243 /* Start with the current time. */
244 tmptime = tsp[0].tv_sec;
245 if ((t = localtime(&tmptime)) == NULL)
246 err(EXIT_FAILURE, "localtime");
247 /* [[CC]YY]MMDDhhmm[.ss] */
248 if ((p = strchr(arg, '.')) == NULL)
249 t->tm_sec = 0; /* Seconds defaults to 0. */
250 else {
251 if (strlen(p + 1) != 2)
252 goto terr;
253 *p++ = '\0';
254 t->tm_sec = ATOI2(p);
255 }
256
257 yearset = 0;
258 switch (strlen(arg)) {
259 case 12: /* CCYYMMDDhhmm */
260 t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
261 yearset = 1;
262 /* FALLTHROUGH */
263 case 10: /* YYMMDDhhmm */
264 if (yearset) {
265 t->tm_year += ATOI2(arg);
266 } else {
267 yearset = ATOI2(arg);
268 if (yearset < YEAR_BOUNDARY)
269 t->tm_year = yearset +
270 LOW_YEAR_CENTURY - TM_YEAR_BASE;
271 else
272 t->tm_year = yearset +
273 HIGH_YEAR_CENTURY - TM_YEAR_BASE;
274 }
275 /* FALLTHROUGH */
276 case 8: /* MMDDhhmm */
277 t->tm_mon = ATOI2(arg);
278 --t->tm_mon; /* Convert from 01-12 to 00-11 */
279 /* FALLTHROUGH */
280 case 6:
281 t->tm_mday = ATOI2(arg);
282 /* FALLTHROUGH */
283 case 4:
284 t->tm_hour = ATOI2(arg);
285 /* FALLTHROUGH */
286 case 2:
287 t->tm_min = ATOI2(arg);
288 break;
289 default:
290 goto terr;
291 }
292
293 t->tm_isdst = -1; /* Figure out DST. */
294 tm = *t;
295 tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
296 if (tsp[0].tv_sec == NO_TIME || difftm(t, &tm))
297 terr: errx(EXIT_FAILURE, "out of range or bad time specification:\n"
298 "\t'%s' should be [[CC]YY]MMDDhhmm[.ss]", initarg);
299
300 tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
301 }
302
303 static void
304 stime_arg2(const char *arg, int year, struct timespec *tsp)
305 {
306 struct tm *t, tm;
307 time_t tmptime;
308 /* Start with the current time. */
309 tmptime = tsp[0].tv_sec;
310 if ((t = localtime(&tmptime)) == NULL)
311 err(EXIT_FAILURE, "localtime");
312
313 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
314 --t->tm_mon; /* Convert from 01-12 to 00-11 */
315 t->tm_mday = ATOI2(arg);
316 t->tm_hour = ATOI2(arg);
317 t->tm_min = ATOI2(arg);
318 if (year) {
319 year = ATOI2(arg);
320 if (year < YEAR_BOUNDARY)
321 t->tm_year = year + LOW_YEAR_CENTURY - TM_YEAR_BASE;
322 else
323 t->tm_year = year + HIGH_YEAR_CENTURY - TM_YEAR_BASE;
324 }
325 t->tm_sec = 0;
326
327 t->tm_isdst = -1; /* Figure out DST. */
328 tm = *t;
329 tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
330 if (tsp[0].tv_sec == NO_TIME || difftm(t, &tm))
331 errx(EXIT_FAILURE,
332 "out of range or bad time specification: MMDDhhmm[YY]");
333
334 tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
335 }
336
337 static void
338 stime_file(const char *fname, struct timespec *tsp)
339 {
340 struct stat sb;
341
342 if (stat(fname, &sb))
343 err(1, "%s", fname);
344 tsp[0] = sb.st_atimespec;
345 tsp[1] = sb.st_mtimespec;
346 }
347
348 static int
349 stime_posix(const char *arg, struct timespec *tsp)
350 {
351 struct tm tm, tms;
352 const char *p;
353 char *ep;
354 int utc = 0;
355 long val;
356
357 #define isdigch(c) (isdigit((int)((unsigned char)(c))))
358
359 if ((p = strchr(arg, '-')) == NULL)
360 return 0;
361 if (p - arg < 4) /* at least 4 year digits required */
362 return 0;
363
364 if (!isdigch(arg[0])) /* and the first must be a digit! */
365 return 0;
366
367 (void)memset(&tm, 0, sizeof tm);
368
369 errno = 0;
370 val = strtol(arg, &ep, 10); /* YYYY */
371 if (val < 0 || val > INT_MAX)
372 return 0;
373 if (*ep != '-')
374 return 0;
375 tm.tm_year = (int)val - 1900;
376
377 p = ep + 1;
378
379 if (!isdigch(*p))
380 return 0;
381 val = strtol(p, &ep, 10); /* MM */
382 if (val < 1 || val > 12)
383 return 0;
384 if (*ep != '-' || ep != p + 2)
385 return 0;
386 tm.tm_mon = (int)val - 1;
387
388 p = ep + 1;
389
390 if (!isdigch(*p))
391 return 0;
392 val = strtol(p, &ep, 10); /* DD */
393 if (val < 1 || val > 31)
394 return 0;
395 if ((*ep != 'T' && *ep != ' ') || ep != p + 2)
396 return 0;
397 tm.tm_mday = (int)val;
398
399 p = ep + 1;
400
401 if (!isdigch(*p))
402 return 0;
403 val = strtol(p, &ep, 10); /* hh */
404 if (val < 0 || val > 23)
405 return 0;
406 if (*ep != ':' || ep != p + 2)
407 return 0;
408 tm.tm_hour = (int)val;
409
410 p = ep + 1;
411
412 if (!isdigch(*p))
413 return 0;
414 val = strtol(p, &ep, 10); /* mm */
415 if (val < 0 || val > 59)
416 return 0;
417 if (*ep != ':' || ep != p + 2)
418 return 0;
419 tm.tm_min = (int)val;
420
421 p = ep + 1;
422
423 if (!isdigch(*p))
424 return 0;
425 val = strtol(p, &ep, 10); /* ss (or in POSIX, SS) */
426 if (val < 0 || val > 60)
427 return 0;
428 if ((*ep != '.' && *ep != ',' && *ep != 'Z' && *ep != '\0') ||
429 ep != p + 2)
430 return 0;
431 tm.tm_sec = (int)val;
432
433 if (*ep == ',' || *ep == '.') {
434 double frac;
435 ptrdiff_t fdigs;
436
437 p = ep + 1;
438 if (!isdigch(*p))
439 return 0;
440 val = strtol(p, &ep, 10);
441 if (val < 0)
442 return 0;
443 if (ep == p) /* at least 1 digit required */
444 return 0;
445 if (*ep != 'Z' && *ep != '\0')
446 return 0;
447
448 if (errno != 0)
449 return 0;
450
451 fdigs = ep - p;
452 if (fdigs > 15) {
453 /* avoid being absurd */
454 /* don't want to risk 10^fdigs being INF */
455 if (val == 0)
456 fdigs = 1;
457 else while (fdigs > 15) {
458 val = (val + 5) / 10;
459 fdigs--;
460 }
461 }
462
463 frac = pow(10.0, (double)fdigs);
464
465 tsp[0].tv_nsec = tsp[1].tv_nsec =
466 (long)round(((double)val / frac) * 1000000000.0);
467 } else
468 tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
469
470 if (*ep == 'Z') {
471 if (ep[1] != '\0')
472 return 0;
473 utc = 1;
474 }
475
476 if (errno != 0)
477 return 0;
478
479 tm.tm_isdst = -1;
480 tms = tm;
481 if (utc)
482 tsp[0].tv_sec = tsp[1].tv_sec = timegm(&tm);
483 else
484 tsp[0].tv_sec = tsp[1].tv_sec = mktime(&tm);
485
486 if ((errno != 0 && tsp[1].tv_sec == NO_TIME) || difftm(&tm, &tms))
487 return 0;
488
489 return 1;
490 }
491
492 /*
493 * Determine whether 2 struct tn's are different
494 * return true (1) if theu are, false (0) otherwise.
495 *
496 * Note that we only consider the fields that are set
497 * for mktime() to use - if mktime() returns them
498 * differently than was set, then there was a problem
499 * with the setting.
500 */
501 static int
502 difftm(const struct tm *t1, const struct tm *t2)
503 {
504 #define CHK(fld) do { \
505 if (t1->tm_##fld != t2->tm_##fld) { \
506 return 1; \
507 } \
508 } while(/*CONSTCOND*/0)
509
510 CHK(year);
511 CHK(mon);
512 CHK(mday);
513 CHK(hour);
514 CHK(min);
515 CHK(sec);
516
517 return 0;
518 }
519
520 static void
521 usage(void)
522 {
523 (void)fprintf(stderr,
524 "Usage: %s [-acfhm] [-d|--date datetime] [-r|--reference file]"
525 " [-t time] file ...\n", getprogname());
526 exit(EXIT_FAILURE);
527 }
528