Mercurial > hg > toybox
annotate toys/posix/touch.c @ 1239:9a13e6637d7b draft
Decided not to go with the sflate implementation of deflate/inflate. The decompression side's already reimplemented in compress, and I'm working on compression side.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 02 Apr 2014 06:37:14 -0500 |
parents | 51f0dda223b8 |
children | 0d19abb90c52 |
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 |
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]; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
56 struct tm tm; |
1063 | 57 char **ss, *date, *s; |
58 int flag, fd, i, len; | |
59 | |
60 // 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
|
61 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
62 gettimeofday(tv, NULL); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
63 localtime_r(&(tv->tv_sec), &tm); |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
64 |
1063 | 65 // Set time from -d? |
66 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
67 if (toys.optflags & (FLAG_t|FLAG_d)) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
68 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
|
69 date = TT.date; |
1063 | 70 i = strlen(date); |
71 if (i && i < sizeof(toybuf)) { | |
72 // Trailing Z means UTC timezone, don't expect libc to know this. | |
73 if (toupper(date[i])=='Z') { | |
74 putenv("TZ=UTC"); | |
75 strcpy(toybuf, date); | |
76 toybuf[i] = 0; | |
77 date = toybuf; | |
78 gmtime_r(&(tv->tv_sec), &tm); | |
79 } | |
80 s = strptime(date, "%Y-%m-%dT%T", &tm); | |
81 if (s && *s=='.') { | |
82 sscanf(s, ".%d%n", &i, &len); | |
83 s += len; | |
84 tv->tv_usec = i; | |
85 } | |
86 } else s = 0; | |
87 | |
88 // Set time from -t? | |
89 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
90 } else { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
91 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
|
92 date = TT.time; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
93 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
|
94 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
|
95 if (s) break; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
96 toybuf[1]='y'; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
97 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
98 if (s && *s=='.') { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
99 int count = sscanf(s, ".%2d%u%n", &(tm.tm_sec), &i, &len); |
1063 | 100 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
101 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
|
102 s += len; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
103 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
104 } |
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 errno = 0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
107 tv->tv_sec = mktime(&tm); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
108 if (!s || *s || errno == EOVERFLOW) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
109 // Warn Indiana Jones the monkey died. |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
110 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 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
112 } |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
113 tv[1]=tv[0]; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
114 |
1063 | 115 // Set time from -r? |
116 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
117 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
|
118 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
|
119 |
1063 | 120 // Ok, we've got a time. Flip -am flags so now it's the ones we _keep_. |
121 | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
122 flag = (~toys.optflags) & (FLAG_m|FLAG_a); |
1063 | 123 |
124 // 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
|
125 for (ss=toys.optargs; *ss;) { |
1063 | 126 if ((flag == (FLAG_m|FLAG_a) || !fetch(*ss, tv, flag)) && !utimes(*ss, tv)) |
127 ss++; | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
132 } |