1 /* touch.c : change timestamp of a file
2  *
3  * Copyright 2012 Choubey Ji <warior.linux@gmail.com>
4  *
5  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
6  *
7  * -f is ignored for BSD/macOS compatibility. busybox/coreutils also support
8  * this, but only coreutils documents it in --help output.
9 
10 USE_TOUCH(NEWTOY(touch, "<1acd:fmr:t:h[!dtr]", TOYFLAG_BIN))
11 
12 config TOUCH
13   bool "touch"
14   default y
15   help
16     usage: touch [-amch] [-d DATE] [-t TIME] [-r FILE] FILE...
17 
18     Update the access and modification times of each FILE to the current time.
19 
20     -a	Change access time
21     -m	Change modification time
22     -c	Don't create file
23     -h	Change symlink
24     -d	Set time to DATE (in YYYY-MM-DDThh:mm:SS[.frac][tz] format)
25     -t	Set time to TIME (in [[CC]YY]MMDDhhmm[.ss][frac] format)
26     -r	Set time same as reference FILE
27 */
28 
29 #define FOR_touch
30 #include "toys.h"
31 
32 GLOBALS(
33   char *t, *r, *d;
34 )
35 
touch_main(void)36 void touch_main(void)
37 {
38   struct timespec ts[2];
39   char **ss;
40   int fd, i;
41 
42   // use current time if no -t or -d
43   ts[0].tv_nsec = UTIME_NOW;
44 
45   if (FLAG(t) || FLAG(d)) {
46     time_t t = time(0);
47     unsigned nano;
48 
49     xparsedate(TT.t ? TT.t : TT.d, &t, &nano, 0);
50     ts->tv_sec = t;
51     ts->tv_nsec = nano;
52   }
53   ts[1]=ts[0];
54 
55   if (TT.r) {
56     struct stat st;
57 
58     xstat(TT.r, &st);
59     ts[0] = st.st_atim;
60     ts[1] = st.st_mtim;
61   }
62 
63   // Which time(s) should we actually change?
64   i = toys.optflags & (FLAG_a|FLAG_m);
65   if (i && i!=(FLAG_a|FLAG_m)) ts[i!=FLAG_m].tv_nsec = UTIME_OMIT;
66 
67   // Loop through files on command line
68   for (ss = toys.optargs; *ss;) {
69     char *s = *ss++;
70 
71     if (!strcmp(s, "-")) {
72       if (!futimens(1, ts)) continue;
73     } else {
74       // cheat: FLAG_h is rightmost flag, so its value is 1
75       if (!utimensat(AT_FDCWD, s, ts, FLAG(h)*AT_SYMLINK_NOFOLLOW)) continue;
76       if (FLAG(c)) continue;
77       if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) {
78         close(fd);
79         if (toys.optflags) ss--;
80         continue;
81       }
82     }
83     perror_msg("'%s'", s);
84   }
85 }
86