diff options
-rw-r--r-- | coreutils/touch.c | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/coreutils/touch.c b/coreutils/touch.c index 189428a79..43312d22a 100644 --- a/coreutils/touch.c +++ b/coreutils/touch.c | |||
@@ -85,8 +85,19 @@ int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
85 | int touch_main(int argc UNUSED_PARAM, char **argv) | 85 | int touch_main(int argc UNUSED_PARAM, char **argv) |
86 | { | 86 | { |
87 | int fd; | 87 | int fd; |
88 | int status = EXIT_SUCCESS; | ||
89 | int opts; | 88 | int opts; |
89 | smalluint status = EXIT_SUCCESS; | ||
90 | #if ENABLE_FEATURE_TOUCH_SUSV3 | ||
91 | char *reference_file = NULL; | ||
92 | char *date_str = NULL; | ||
93 | /* timebuf[0] is atime, timebuf[1] is mtime */ | ||
94 | struct timespec timebuf[2]; | ||
95 | #else | ||
96 | # define reference_file NULL | ||
97 | # define date_str NULL | ||
98 | # define timebuf ((struct timespec*)NULL) | ||
99 | #endif | ||
100 | |||
90 | enum { | 101 | enum { |
91 | OPT_c = (1 << 0), | 102 | OPT_c = (1 << 0), |
92 | OPT_h = (1 << 1) * ENABLE_FEATURE_TOUCH_NODEREF, | 103 | OPT_h = (1 << 1) * ENABLE_FEATURE_TOUCH_NODEREF, |
@@ -105,17 +116,6 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
105 | IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h") | 116 | IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h") |
106 | ; | 117 | ; |
107 | #endif | 118 | #endif |
108 | #if ENABLE_FEATURE_TOUCH_SUSV3 | ||
109 | char *reference_file = NULL; | ||
110 | char *date_str = NULL; | ||
111 | /* timebuf[0] is atime, timebuf[1] is mtime */ | ||
112 | struct timespec timebuf[2]; | ||
113 | #else | ||
114 | # define reference_file NULL | ||
115 | # define date_str NULL | ||
116 | # define timebuf ((struct timespec*)NULL) | ||
117 | #endif | ||
118 | |||
119 | /* -d and -t both set time. In coreutils, | 119 | /* -d and -t both set time. In coreutils, |
120 | * accepted data format differs a bit between -d and -t. | 120 | * accepted data format differs a bit between -d and -t. |
121 | * We accept the same formats for both | 121 | * We accept the same formats for both |
@@ -125,9 +125,10 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
125 | IF_FEATURE_TOUCH_SUSV3("r:d:t:am") | 125 | IF_FEATURE_TOUCH_SUSV3("r:d:t:am") |
126 | /*ignored:*/ "f" IF_NOT_FEATURE_TOUCH_SUSV3("am") | 126 | /*ignored:*/ "f" IF_NOT_FEATURE_TOUCH_SUSV3("am") |
127 | "\0" /* opt_complementary: */ | 127 | "\0" /* opt_complementary: */ |
128 | /* coreutils forbids -r and -t at once: */ IF_FEATURE_TOUCH_SUSV3("r--t:t--r") | 128 | /* at least one arg: */ "-1" |
129 | /* but allows these combinations: "r--d:d--r:t--d:d--t" */, | 129 | /* coreutils forbids -r and -t at once: */ IF_FEATURE_TOUCH_SUSV3(":r--t:t--r") |
130 | touch_longopts | 130 | /* but allows these combinations: "r--d:d--r:t--d:d--t" */ |
131 | , touch_longopts | ||
131 | #if ENABLE_FEATURE_TOUCH_SUSV3 | 132 | #if ENABLE_FEATURE_TOUCH_SUSV3 |
132 | , &reference_file | 133 | , &reference_file |
133 | , &date_str | 134 | , &date_str |
@@ -135,13 +136,7 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
135 | #endif | 136 | #endif |
136 | ); | 137 | ); |
137 | 138 | ||
138 | argv += optind; | ||
139 | if (!*argv) { | ||
140 | bb_show_usage(); | ||
141 | } | ||
142 | |||
143 | timebuf[0].tv_nsec = timebuf[1].tv_nsec = UTIME_NOW; | 139 | timebuf[0].tv_nsec = timebuf[1].tv_nsec = UTIME_NOW; |
144 | |||
145 | if (reference_file) { | 140 | if (reference_file) { |
146 | struct stat stbuf; | 141 | struct stat stbuf; |
147 | xstat(reference_file, &stbuf); | 142 | xstat(reference_file, &stbuf); |
@@ -150,7 +145,6 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
150 | timebuf[0].tv_nsec = stbuf.st_atim.tv_nsec; | 145 | timebuf[0].tv_nsec = stbuf.st_atim.tv_nsec; |
151 | timebuf[1].tv_nsec = stbuf.st_mtim.tv_nsec; | 146 | timebuf[1].tv_nsec = stbuf.st_mtim.tv_nsec; |
152 | } | 147 | } |
153 | |||
154 | if (date_str) { | 148 | if (date_str) { |
155 | struct tm tm_time; | 149 | struct tm tm_time; |
156 | time_t t; | 150 | time_t t; |
@@ -168,7 +162,6 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
168 | timebuf[1].tv_sec = timebuf[0].tv_sec = t; | 162 | timebuf[1].tv_sec = timebuf[0].tv_sec = t; |
169 | timebuf[1].tv_nsec = timebuf[0].tv_nsec = 0; | 163 | timebuf[1].tv_nsec = timebuf[0].tv_nsec = 0; |
170 | } | 164 | } |
171 | |||
172 | if (opts & OPT_a) { | 165 | if (opts & OPT_a) { |
173 | timebuf[1].tv_nsec = UTIME_OMIT; | 166 | timebuf[1].tv_nsec = UTIME_OMIT; |
174 | } | 167 | } |
@@ -176,6 +169,7 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
176 | timebuf[0].tv_nsec = UTIME_OMIT; | 169 | timebuf[0].tv_nsec = UTIME_OMIT; |
177 | } | 170 | } |
178 | 171 | ||
172 | argv += optind; | ||
179 | do { | 173 | do { |
180 | int result = utimensat(AT_FDCWD, *argv, timebuf, | 174 | int result = utimensat(AT_FDCWD, *argv, timebuf, |
181 | (opts & OPT_h) ? AT_SYMLINK_NOFOLLOW : 0); | 175 | (opts & OPT_h) ? AT_SYMLINK_NOFOLLOW : 0); |