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#define FOR_touch
27#include "toys.h"
28
29GLOBALS(
30 char *time;
31 char *file;
32 char *date;
33)
34
35void touch_main(void)
36{
37 struct timespec ts[2];
38 char **ss;
39 int fd, i;
40
41
42 ts[0].tv_nsec = UTIME_NOW;
43 if (toys.optflags & (FLAG_t|FLAG_d)) {
44 char *s, *date, **format;
45 struct tm tm;
46 int len = 0;
47
48
49 ts->tv_sec = time(0);
50 ts->tv_nsec = 0;
51
52
53 if (toys.optflags & FLAG_d) {
54 format = (char *[]){"%Y-%m-%dT%T", "%Y-%m-%d %T", 0};
55 date = TT.date;
56 } else {
57 format = (char *[]){"%m%d%H%M", "%y%m%d%H%M", "%C%y%m%d%H%M", 0};
58 date = TT.time;
59 }
60
61
62 i = strlen(s = date);
63 if (i && toupper(date[i-1])=='Z') {
64 date[i-1] = 0;
65 setenv("TZ", "UTC0", 1);
66 }
67
68 while (*format) {
69 if (toys.optflags&FLAG_t) {
70 s = strchr(date, '.');
71 if ((s ? s-date : strlen(date)) != strlen(*format)) {
72 format++;
73 continue;
74 }
75 }
76 localtime_r(&(ts->tv_sec), &tm);
77
78 tm.tm_isdst = 0;
79 tm.tm_sec = 0;
80 s = strptime(date, *format++, &tm);
81
82
83 if (s && *s=='.' && isdigit(s[1])) {
84 s++;
85 if (toys.optflags&FLAG_t)
86 if (1 == sscanf(s, "%2u%n", &(tm.tm_sec), &len)) s += len;
87 if (1 == sscanf(s, "%lu%n", &ts->tv_nsec, &len)) {
88 s += len;
89 if (ts->tv_nsec > 999999999) s = 0;
90 else while (len++ < 9) ts->tv_nsec *= 10;
91 }
92 }
93 if (s && !*s) break;
94 }
95
96 errno = 0;
97 ts->tv_sec = mktime(&tm);
98 if (!s || *s || ts->tv_sec == -1) perror_exit("bad '%s'", date);
99 }
100 ts[1]=ts[0];
101
102
103
104 if (TT.file) {
105 struct stat st;
106
107 xstat(TT.file, &st);
108 ts[0] = st.st_atim;
109 ts[1] = st.st_mtim;
110 }
111
112
113 i = toys.optflags & (FLAG_a|FLAG_m);
114 if (i && i!=(FLAG_a|FLAG_m)) ts[i!=FLAG_m].tv_nsec = UTIME_OMIT;
115
116
117 for (ss = toys.optargs; *ss;) {
118 char *s = *ss++;
119
120 if (!strcmp(s, "-")) {
121 if (!futimens(1, ts)) continue;
122 } else {
123
124 if (!utimensat(AT_FDCWD, s, ts,
125 (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) continue;
126 if (toys.optflags & FLAG_c) continue;
127 if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) {
128 close(fd);
129 if (toys.optflags) ss--;
130 continue;
131 }
132 }
133 perror_msg("'%s'", s);
134 }
135}
136