Mercurial > hg > toybox
annotate toys/posix/touch.c @ 1037:af1780148f7c draft
Implement ls --color=auto, suggested by Rich Felker.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 01 Sep 2013 08:00:41 -0500 |
parents | bd1225873eb4 |
children | 3c744d5f0765 |
rev | line source |
---|---|
699 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * touch.c : change timestamp of a file | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
4 * Copyright 2012 Choubey Ji <warior.linux@gmail.com> |
699 | 5 * |
6 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
7 * acmrtd |
699 | 8 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
9 USE_TOUCH(NEWTOY(touch, "acd:mr:t:[!dtr]", TOYFLAG_BIN)) |
699 | 10 |
11 config TOUCH | |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
12 bool "touch" |
699 | 13 default y |
14 help | |
1012
bd1225873eb4
Fix usage: lines for a couple commands.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
15 usage: touch [OPTION]... FILE... |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
16 |
699 | 17 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
|
18 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
19 -a change access time |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
20 -m change modification time |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
21 -c don't create file |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
22 -d DATE use YYYY-MM-DDThh:mm:SS[.frac][tz] as time |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
23 -t TIME use [[CC]YY]MMDDhhmm[.ss][frac] as time |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
24 -r FILE use reference file's time |
699 | 25 */ |
26 | |
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
|
27 #define FOR_touch |
699 | 28 #include "toys.h" |
29 | |
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 GLOBALS( |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
31 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
|
32 char *file; |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
33 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
|
34 ) |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
35 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
36 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
|
37 { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
38 struct stat st; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
39 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
40 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
|
41 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
42 if (flags & FLAG_a) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
43 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
|
44 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
|
45 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
46 if (flags & FLAG_m) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
47 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
|
48 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
|
49 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
50 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
51 return 0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
52 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
53 |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
54 void touch_main(void) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
55 { |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
56 struct timeval tv[2]; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
57 struct tm tm; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
58 char **ss; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
59 int flag; |
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); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
62 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
|
63 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
64 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
|
65 char *date, *s; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
66 int i, len; |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
67 |
711
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; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
70 i = strlen(date)-1; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
71 if (*date && toupper(date[i])=='Z') { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
72 putenv("TZ=UTC"); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
73 strncpy(toybuf, date, sizeof(toybuf)-1); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
74 date = toybuf; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
75 date[i]=0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
76 gmtime_r(&(tv->tv_sec), &tm); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
77 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
78 s = strptime(date, "%Y-%m-%dT%T", &tm); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
79 if (s && *s=='.') { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
80 sscanf(s, ".%d%n", &i, &len); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
81 s += len; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
82 tv->tv_usec = i; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
83 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
84 } else { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
85 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
|
86 date = TT.time; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
87 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
|
88 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
|
89 if (s) break; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
90 toybuf[1]='y'; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
91 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
92 if (s && *s=='.') { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
93 int count = sscanf(s, ".%2d%u%n", &(tm.tm_sec), &i, &len); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
94 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
|
95 s += len; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
96 } |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
97 } |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
98 |
711
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
99 errno = 0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
100 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
|
101 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
|
102 // 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
|
103 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
|
104 } |
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 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
108 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
|
109 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
|
110 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
111 flag = (~toys.optflags) & (FLAG_m|FLAG_a); |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
112 if (flag == (FLAG_m|FLAG_a)) flag = 0; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
113 for (ss=toys.optargs; *ss;) { |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
114 int fd; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
115 |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
116 if ((!flag || !fetch(*ss, tv, flag)) && !utimes(*ss, tv)) ss++; |
dcc6136e6659
Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents:
702
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
121 } |