Mercurial > hg > toybox
annotate toys/posix/touch.c @ 1511:df792340e3f7 draft
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 01 Oct 2014 19:57:34 -0500 |
parents | 0d19abb90c52 |
children | 910a958980dc |
rev | line source |
---|---|
1063 | 1 /* touch.c : change timestamp of a file |
699 | 2 * |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
3 * Copyright 2012 Choubey Ji <warior.linux@gmail.com> |
699 | 4 * |
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html | |
6 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
7 USE_TOUCH(NEWTOY(touch, "acd:mr:t:[!dtr]", TOYFLAG_BIN)) |
699 | 8 |
9 config TOUCH | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
10 bool "touch" |
699 | 11 default y |
12 help | |
1063 | 13 usage: touch [-amc] [-d DATE] [-t TIME] [-r FILE] FILE... |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
14 |
699 | 15 Update the access and modification times of each FILE to the current time. |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
16 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
17 -a change access time |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
18 -m change modification time |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
19 -c don't create file |
1063 | 20 -d set time to DATE (in YYYY-MM-DDThh:mm:SS[.frac][tz] format) |
21 -t set time to TIME (in [[CC]YY]MMDDhhmm[.ss][frac] format) | |
22 -r set time same as reference FILE | |
699 | 23 */ |
24 | |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
25 #define FOR_touch |
699 | 26 #include "toys.h" |
27 | |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
28 GLOBALS( |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
29 char *time; |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
30 char *file; |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
31 char *date; |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
32 ) |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
33 |
1063 | 34 // Fetch access and/or modification time of a file |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
35 int fetch(char *file, struct timeval *tv, unsigned flags) |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
36 { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
37 struct stat st; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
38 |
1511
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
39 if (stat(file, &st)) return 1; |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
40 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
41 if (flags & FLAG_a) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
42 tv[0].tv_sec = st.st_atime; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
43 tv[0].tv_usec = st.st_atim.tv_nsec/1000; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
44 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
45 if (flags & FLAG_m) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
46 tv[1].tv_sec = st.st_mtime; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
47 tv[1].tv_usec = st.st_mtim.tv_nsec/1000; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
48 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
49 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
50 return 0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
51 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
52 |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
53 void touch_main(void) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
54 { |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
55 struct timeval tv[2]; |
1478
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
56 char **ss; |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
57 int flag, fd, i; |
1063 | 58 |
59 // Set time from clock? | |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
60 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
61 gettimeofday(tv, NULL); |
1063 | 62 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
63 if (toys.optflags & (FLAG_t|FLAG_d)) { |
1478
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
64 char *s, *date; |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
65 struct tm tm; |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
66 int len; |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
67 |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
68 localtime_r(&(tv->tv_sec), &tm); |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
69 |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
70 // Set time from -d? |
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
71 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
72 if (toys.optflags & FLAG_d) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
73 date = TT.date; |
1063 | 74 i = strlen(date); |
1478
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
75 if (i) { |
1511
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
76 // Trailing Z means UTC timezone, don't expect libc to know this. |
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
77 if (toupper(date[i-1])=='Z') { |
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
78 date[i-1] = 0; |
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
79 setenv("TZ", "UTC0", 1); |
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
80 localtime_r(&(tv->tv_sec), &tm); |
df792340e3f7
Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents:
1478
diff
changeset
|
81 } |
1063 | 82 s = strptime(date, "%Y-%m-%dT%T", &tm); |
83 if (s && *s=='.') { | |
84 sscanf(s, ".%d%n", &i, &len); | |
85 s += len; | |
86 tv->tv_usec = i; | |
87 } | |
88 } else s = 0; | |
89 | |
90 // Set time from -t? | |
91 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
92 } else { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
93 strcpy(toybuf, "%Y%m%d%H%M"); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
94 date = TT.time; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
95 for (i=0;i<3;i++) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
96 s = strptime(date, toybuf+(i&2), &tm); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
97 if (s) break; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
98 toybuf[1]='y'; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
99 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
100 if (s && *s=='.') { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
101 int count = sscanf(s, ".%2d%u%n", &(tm.tm_sec), &i, &len); |
1063 | 102 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
103 if (count==2) tv->tv_usec = i; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
104 s += len; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
105 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
106 } |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
107 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
108 errno = 0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
109 tv->tv_sec = mktime(&tm); |
1478
0d19abb90c52
Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents:
1063
diff
changeset
|
110 if (!s || *s || errno == EOVERFLOW) perror_exit("bad '%s'", date); |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
111 } |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
112 tv[1]=tv[0]; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
113 |
1063 | 114 // Set time from -r? |
115 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
116 if (TT.file && fetch(TT.file, tv, FLAG_a|FLAG_m)) |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
117 perror_exit("-r '%s'", TT.file); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
118 |
1063 | 119 // Ok, we've got a time. Flip -am flags so now it's the ones we _keep_. |
120 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
121 flag = (~toys.optflags) & (FLAG_m|FLAG_a); |
1063 | 122 |
123 // Loop through files on command line | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
124 for (ss=toys.optargs; *ss;) { |
1063 | 125 if ((flag == (FLAG_m|FLAG_a) || !fetch(*ss, tv, flag)) && !utimes(*ss, tv)) |
126 ss++; | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
127 else if (toys.optflags & FLAG_c) ss++; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
128 else if (-1 != (fd = open(*ss, O_CREAT, 0666))) close(fd); |
780
6cc69be43c42
Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents:
711
diff
changeset
|
129 else perror_msg("'%s'", *ss++); |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
130 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
131 } |