1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define FOR_touch
30#include "toys.h"
31
32GLOBALS(
33 char *t, *r, *d;
34)
35
36void touch_main(void)
37{
38 struct timespec ts[2];
39 char **ss;
40 int fd, i;
41
42
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
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
68 for (ss = toys.optargs; *ss;) {
69 char *s = *ss++;
70
71 if (!strcmp(s, "-")) {
72 if (!futimens(1, ts)) continue;
73 } else {
74
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