diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-09-11 11:58:33 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-09-11 11:58:33 +0200 |
commit | b5352078a7eb16c33404a8b079aaa9c2b994546c (patch) | |
tree | f84b3969a1fc2559d639d5d55a710ccc9482420a | |
parent | a613aa1b4c26be1567a59e9216c83d21d4be122b (diff) | |
download | busybox-w32-b5352078a7eb16c33404a8b079aaa9c2b994546c.tar.gz busybox-w32-b5352078a7eb16c33404a8b079aaa9c2b994546c.tar.bz2 busybox-w32-b5352078a7eb16c33404a8b079aaa9c2b994546c.zip |
touch: add conditional support for -h
Based on a patch by Andy <andy.padavan@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/touch.c | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/coreutils/touch.c b/coreutils/touch.c index 1216ca202..293a96890 100644 --- a/coreutils/touch.c +++ b/coreutils/touch.c | |||
@@ -26,6 +26,14 @@ | |||
26 | //config: touch is used to create or change the access and/or | 26 | //config: touch is used to create or change the access and/or |
27 | //config: modification timestamp of specified files. | 27 | //config: modification timestamp of specified files. |
28 | //config: | 28 | //config: |
29 | //config:config FEATURE_TOUCH_NODEREF | ||
30 | //config: bool "Add support for -h" | ||
31 | //config: default y | ||
32 | //config: depends on TOUCH | ||
33 | //config: help | ||
34 | //config: Enable touch to have the -h option. | ||
35 | //config: This requires libc support for lutimes() function. | ||
36 | //config: | ||
29 | //config:config FEATURE_TOUCH_SUSV3 | 37 | //config:config FEATURE_TOUCH_SUSV3 |
30 | //config: bool "Add support for SUSV3 features (-d -t -r)" | 38 | //config: bool "Add support for SUSV3 features (-d -t -r)" |
31 | //config: default y | 39 | //config: default y |
@@ -42,6 +50,9 @@ | |||
42 | //usage:#define touch_full_usage "\n\n" | 50 | //usage:#define touch_full_usage "\n\n" |
43 | //usage: "Update the last-modified date on the given FILE[s]\n" | 51 | //usage: "Update the last-modified date on the given FILE[s]\n" |
44 | //usage: "\n -c Don't create files" | 52 | //usage: "\n -c Don't create files" |
53 | //usage: IF_FEATURE_TOUCH_NODEREF( | ||
54 | //usage: "\n -h Don't follow links" | ||
55 | //usage: ) | ||
45 | //usage: IF_FEATURE_TOUCH_SUSV3( | 56 | //usage: IF_FEATURE_TOUCH_SUSV3( |
46 | //usage: "\n -d DT Date/time to use" | 57 | //usage: "\n -d DT Date/time to use" |
47 | //usage: "\n -t DT Date/time to use" | 58 | //usage: "\n -t DT Date/time to use" |
@@ -65,6 +76,7 @@ | |||
65 | * parse STRING and use it instead of current time | 76 | * parse STRING and use it instead of current time |
66 | * -f (ignored, BSD compat) | 77 | * -f (ignored, BSD compat) |
67 | * -m change only the modification time | 78 | * -m change only the modification time |
79 | * -h, --no-dereference | ||
68 | * -r, --reference=FILE | 80 | * -r, --reference=FILE |
69 | * use this file's times instead of current time | 81 | * use this file's times instead of current time |
70 | * -t STAMP | 82 | * -t STAMP |
@@ -79,6 +91,13 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
79 | int fd; | 91 | int fd; |
80 | int status = EXIT_SUCCESS; | 92 | int status = EXIT_SUCCESS; |
81 | int opts; | 93 | int opts; |
94 | enum { | ||
95 | OPT_c = (1 << 0), | ||
96 | OPT_r = (1 << 1) * ENABLE_FEATURE_TOUCH_SUSV3, | ||
97 | OPT_d = (1 << 2) * ENABLE_FEATURE_TOUCH_SUSV3, | ||
98 | OPT_t = (1 << 3) * ENABLE_FEATURE_TOUCH_SUSV3, | ||
99 | OPT_h = (1 << 4) * ENABLE_FEATURE_TOUCH_NODEREF, | ||
100 | }; | ||
82 | #if ENABLE_FEATURE_TOUCH_SUSV3 | 101 | #if ENABLE_FEATURE_TOUCH_SUSV3 |
83 | # if ENABLE_LONG_OPTS | 102 | # if ENABLE_LONG_OPTS |
84 | static const char touch_longopts[] ALIGN1 = | 103 | static const char touch_longopts[] ALIGN1 = |
@@ -86,6 +105,7 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
86 | "no-create\0" No_argument "c" | 105 | "no-create\0" No_argument "c" |
87 | "reference\0" Required_argument "r" | 106 | "reference\0" Required_argument "r" |
88 | "date\0" Required_argument "d" | 107 | "date\0" Required_argument "d" |
108 | IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h") | ||
89 | ; | 109 | ; |
90 | # endif | 110 | # endif |
91 | char *reference_file = NULL; | 111 | char *reference_file = NULL; |
@@ -105,13 +125,13 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
105 | * accepted data format differs a bit between -d and -t. | 125 | * accepted data format differs a bit between -d and -t. |
106 | * We accept the same formats for both */ | 126 | * We accept the same formats for both */ |
107 | opts = getopt32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:") | 127 | opts = getopt32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:") |
128 | IF_FEATURE_TOUCH_NODEREF("h") | ||
108 | /*ignored:*/ "fma" | 129 | /*ignored:*/ "fma" |
109 | IF_FEATURE_TOUCH_SUSV3(, &reference_file) | 130 | IF_FEATURE_TOUCH_SUSV3(, &reference_file) |
110 | IF_FEATURE_TOUCH_SUSV3(, &date_str) | 131 | IF_FEATURE_TOUCH_SUSV3(, &date_str) |
111 | IF_FEATURE_TOUCH_SUSV3(, &date_str) | 132 | IF_FEATURE_TOUCH_SUSV3(, &date_str) |
112 | ); | 133 | ); |
113 | 134 | ||
114 | opts &= 1; /* only -c bit is left */ | ||
115 | argv += optind; | 135 | argv += optind; |
116 | if (!*argv) { | 136 | if (!*argv) { |
117 | bb_show_usage(); | 137 | bb_show_usage(); |
@@ -121,6 +141,10 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
121 | struct stat stbuf; | 141 | struct stat stbuf; |
122 | xstat(reference_file, &stbuf); | 142 | xstat(reference_file, &stbuf); |
123 | timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime; | 143 | timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime; |
144 | /* Can use .st_mtim.tv_nsec | ||
145 | * (or is it .st_mtimensec?? see date.c) | ||
146 | * to set microseconds too. | ||
147 | */ | ||
124 | } | 148 | } |
125 | 149 | ||
126 | if (date_str) { | 150 | if (date_str) { |
@@ -141,9 +165,16 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
141 | } | 165 | } |
142 | 166 | ||
143 | do { | 167 | do { |
144 | if (utimes(*argv, (reference_file || date_str) ? timebuf : NULL) != 0) { | 168 | int result; |
145 | if (errno == ENOENT) { /* no such file */ | 169 | result = ( |
146 | if (opts) { /* creation is disabled, so ignore */ | 170 | #if ENABLE_FEATURE_TOUCH_NODEREF |
171 | (opts & OPT_h) ? lutimes : | ||
172 | #endif | ||
173 | utimes)(*argv, (reference_file || date_str) ? timebuf : NULL); | ||
174 | if (result != 0) { | ||
175 | if (errno == ENOENT) { /* no such file? */ | ||
176 | if (opts & OPT_c) { | ||
177 | /* Creation is disabled, so ignore */ | ||
147 | continue; | 178 | continue; |
148 | } | 179 | } |
149 | /* Try to create the file */ | 180 | /* Try to create the file */ |