annotate toys/posix/touch.c @ 1478:0d19abb90c52 draft

Minor cleanup pass on touch.
author Rob Landley <rob@landley.net>
date Wed, 17 Sep 2014 18:24:10 -0500
parents 51f0dda223b8
children df792340e3f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
1 /* touch.c : change timestamp of a file
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default y
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
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
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
20 -d set time to DATE (in YYYY-MM-DDThh:mm:SS[.frac][tz] format)
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
21 -t set time to TIME (in [[CC]YY]MMDDhhmm[.ss][frac] format)
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
22 -r set time same as reference FILE
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
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
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
39 if (stat(TT.file, &st)) return 1;
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
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
58
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
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
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
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
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
74 i = strlen(date);
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
75 if (i) {
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
76 s = strptime(date, "%Y-%m-%dT%T", &tm);
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
77 if (s && *s=='.') {
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
78 sscanf(s, ".%d%n", &i, &len);
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
79 s += len;
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
80 tv->tv_usec = i;
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
81 }
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
82 } else s = 0;
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
83
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
84 // Set time from -t?
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
85
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
86 } else {
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
87 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
88 date = TT.time;
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
89 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
90 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
91 if (s) break;
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
92 toybuf[1]='y';
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
93 }
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
94 if (s && *s=='.') {
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
95 int count = sscanf(s, ".%2d%u%n", &(tm.tm_sec), &i, &len);
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
96
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
97 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
98 s += len;
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 }
700
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
101
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
102 errno = 0;
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
103 tv->tv_sec = mktime(&tm);
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
104 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
105 }
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
106 tv[1]=tv[0];
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
107
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
108 // Set time from -r?
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
109
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
110 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
111 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
112
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
113 // Ok, we've got a time. Flip -am flags so now it's the ones we _keep_.
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
114
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
115 flag = (~toys.optflags) & (FLAG_m|FLAG_a);
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
116
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
117 // 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
118 for (ss=toys.optargs; *ss;) {
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
119 if ((flag == (FLAG_m|FLAG_a) || !fetch(*ss, tv, flag)) && !utimes(*ss, tv))
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
120 ss++;
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
121 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
122 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
123 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
124 }
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
125 }