Mercurial > hg > toybox
comparison toys/posix/touch.c @ 1063:51f0dda223b8 draft
Minor cleanups.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 09 Sep 2013 11:18:18 -0500 |
parents | 3c744d5f0765 |
children | 0d19abb90c52 |
comparison
equal
deleted
inserted
replaced
1062:3c744d5f0765 | 1063:51f0dda223b8 |
---|---|
1 /* vi: set sw=4 ts=4: | 1 /* touch.c : change timestamp of a file |
2 * | 2 * |
3 * touch.c : change timestamp of a file | |
4 * Copyright 2012 Choubey Ji <warior.linux@gmail.com> | 3 * Copyright 2012 Choubey Ji <warior.linux@gmail.com> |
5 * | 4 * |
6 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html | 5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html |
7 * acmrtd | |
8 | 6 |
9 USE_TOUCH(NEWTOY(touch, "acd:mr:t:[!dtr]", TOYFLAG_BIN)) | 7 USE_TOUCH(NEWTOY(touch, "acd:mr:t:[!dtr]", TOYFLAG_BIN)) |
10 | 8 |
11 config TOUCH | 9 config TOUCH |
12 bool "touch" | 10 bool "touch" |
13 default y | 11 default y |
14 help | 12 help |
15 usage: touch [OPTION]... FILE... | 13 usage: touch [-amc] [-d DATE] [-t TIME] [-r FILE] FILE... |
16 | 14 |
17 Update the access and modification times of each FILE to the current time. | 15 Update the access and modification times of each FILE to the current time. |
18 | 16 |
19 -a change access time | 17 -a change access time |
20 -m change modification time | 18 -m change modification time |
21 -c don't create file | 19 -c don't create file |
22 -d DATE use YYYY-MM-DDThh:mm:SS[.frac][tz] as time | 20 -d set time to DATE (in YYYY-MM-DDThh:mm:SS[.frac][tz] format) |
23 -t TIME use [[CC]YY]MMDDhhmm[.ss][frac] as time | 21 -t set time to TIME (in [[CC]YY]MMDDhhmm[.ss][frac] format) |
24 -r FILE use reference file's time | 22 -r set time same as reference FILE |
25 */ | 23 */ |
26 | 24 |
27 #define FOR_touch | 25 #define FOR_touch |
28 #include "toys.h" | 26 #include "toys.h" |
29 | 27 |
31 char *time; | 29 char *time; |
32 char *file; | 30 char *file; |
33 char *date; | 31 char *date; |
34 ) | 32 ) |
35 | 33 |
34 // Fetch access and/or modification time of a file | |
36 int fetch(char *file, struct timeval *tv, unsigned flags) | 35 int fetch(char *file, struct timeval *tv, unsigned flags) |
37 { | 36 { |
38 struct stat st; | 37 struct stat st; |
39 | 38 |
40 if (stat(TT.file, &st)) return 1; | 39 if (stat(TT.file, &st)) return 1; |
53 | 52 |
54 void touch_main(void) | 53 void touch_main(void) |
55 { | 54 { |
56 struct timeval tv[2]; | 55 struct timeval tv[2]; |
57 struct tm tm; | 56 struct tm tm; |
58 char **ss; | 57 char **ss, *date, *s; |
59 int flag; | 58 int flag, fd, i, len; |
59 | |
60 // Set time from clock? | |
60 | 61 |
61 gettimeofday(tv, NULL); | 62 gettimeofday(tv, NULL); |
62 localtime_r(&(tv->tv_sec), &tm); | 63 localtime_r(&(tv->tv_sec), &tm); |
63 | 64 |
65 // Set time from -d? | |
66 | |
64 if (toys.optflags & (FLAG_t|FLAG_d)) { | 67 if (toys.optflags & (FLAG_t|FLAG_d)) { |
65 char *date, *s; | |
66 int i, len; | |
67 | |
68 if (toys.optflags & FLAG_d) { | 68 if (toys.optflags & FLAG_d) { |
69 date = TT.date; | 69 date = TT.date; |
70 i = strlen(date)-1; | 70 i = strlen(date); |
71 if (*date && toupper(date[i])=='Z') { | 71 if (i && i < sizeof(toybuf)) { |
72 putenv("TZ=UTC"); | 72 // Trailing Z means UTC timezone, don't expect libc to know this. |
73 strncpy(toybuf, date, sizeof(toybuf)-1); | 73 if (toupper(date[i])=='Z') { |
74 date = toybuf; | 74 putenv("TZ=UTC"); |
75 if (i > sizeof(toybuf)-1) i = sizeof(toybuf)-1; | 75 strcpy(toybuf, date); |
76 date[i]=0; | 76 toybuf[i] = 0; |
77 gmtime_r(&(tv->tv_sec), &tm); | 77 date = toybuf; |
78 } | 78 gmtime_r(&(tv->tv_sec), &tm); |
79 s = strptime(date, "%Y-%m-%dT%T", &tm); | 79 } |
80 if (s && *s=='.') { | 80 s = strptime(date, "%Y-%m-%dT%T", &tm); |
81 sscanf(s, ".%d%n", &i, &len); | 81 if (s && *s=='.') { |
82 s += len; | 82 sscanf(s, ".%d%n", &i, &len); |
83 tv->tv_usec = i; | 83 s += len; |
84 } | 84 tv->tv_usec = i; |
85 } | |
86 } else s = 0; | |
87 | |
88 // Set time from -t? | |
89 | |
85 } else { | 90 } else { |
86 strcpy(toybuf, "%Y%m%d%H%M"); | 91 strcpy(toybuf, "%Y%m%d%H%M"); |
87 date = TT.time; | 92 date = TT.time; |
88 for (i=0;i<3;i++) { | 93 for (i=0;i<3;i++) { |
89 s = strptime(date, toybuf+(i&2), &tm); | 94 s = strptime(date, toybuf+(i&2), &tm); |
90 if (s) break; | 95 if (s) break; |
91 toybuf[1]='y'; | 96 toybuf[1]='y'; |
92 } | 97 } |
93 if (s && *s=='.') { | 98 if (s && *s=='.') { |
94 int count = sscanf(s, ".%2d%u%n", &(tm.tm_sec), &i, &len); | 99 int count = sscanf(s, ".%2d%u%n", &(tm.tm_sec), &i, &len); |
100 | |
95 if (count==2) tv->tv_usec = i; | 101 if (count==2) tv->tv_usec = i; |
96 s += len; | 102 s += len; |
97 } | 103 } |
98 } | 104 } |
99 | 105 |
104 perror_exit("bad '%s'", date); | 110 perror_exit("bad '%s'", date); |
105 } | 111 } |
106 } | 112 } |
107 tv[1]=tv[0]; | 113 tv[1]=tv[0]; |
108 | 114 |
115 // Set time from -r? | |
116 | |
109 if (TT.file && fetch(TT.file, tv, FLAG_a|FLAG_m)) | 117 if (TT.file && fetch(TT.file, tv, FLAG_a|FLAG_m)) |
110 perror_exit("-r '%s'", TT.file); | 118 perror_exit("-r '%s'", TT.file); |
111 | 119 |
120 // Ok, we've got a time. Flip -am flags so now it's the ones we _keep_. | |
121 | |
112 flag = (~toys.optflags) & (FLAG_m|FLAG_a); | 122 flag = (~toys.optflags) & (FLAG_m|FLAG_a); |
113 if (flag == (FLAG_m|FLAG_a)) flag = 0; | 123 |
124 // Loop through files on command line | |
114 for (ss=toys.optargs; *ss;) { | 125 for (ss=toys.optargs; *ss;) { |
115 int fd; | 126 if ((flag == (FLAG_m|FLAG_a) || !fetch(*ss, tv, flag)) && !utimes(*ss, tv)) |
116 | 127 ss++; |
117 if ((!flag || !fetch(*ss, tv, flag)) && !utimes(*ss, tv)) ss++; | |
118 else if (toys.optflags & FLAG_c) ss++; | 128 else if (toys.optflags & FLAG_c) ss++; |
119 else if (-1 != (fd = open(*ss, O_CREAT, 0666))) close(fd); | 129 else if (-1 != (fd = open(*ss, O_CREAT, 0666))) close(fd); |
120 else perror_msg("'%s'", *ss++); | 130 else perror_msg("'%s'", *ss++); |
121 } | 131 } |
122 } | 132 } |