diff options
author | Rob Landley <rob@landley.net> | 2005-10-04 03:34:39 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-10-04 03:34:39 +0000 |
commit | 5d3a0e8ed3c7fcb43c9298d43868672ae51a4456 (patch) | |
tree | f60cc3927c07020236912ae6cfa9f864a29f6f75 /findutils/find.c | |
parent | d128b7182082052626555331ca46b92e478a0cc5 (diff) | |
download | busybox-w32-5d3a0e8ed3c7fcb43c9298d43868672ae51a4456.tar.gz busybox-w32-5d3a0e8ed3c7fcb43c9298d43868672ae51a4456.tar.bz2 busybox-w32-5d3a0e8ed3c7fcb43c9298d43868672ae51a4456.zip |
Add find -exec support from Rob Sullivan, and convert CONFIG_ to ENABLE_
while we're in the area.
Diffstat (limited to 'findutils/find.c')
-rw-r--r-- | findutils/find.c | 103 |
1 files changed, 60 insertions, 43 deletions
diff --git a/findutils/find.c b/findutils/find.c index 334a7b56e..25f57fd4f 100644 --- a/findutils/find.c +++ b/findutils/find.c | |||
@@ -39,33 +39,39 @@ static const char msg_invalid_arg[] = "invalid argument `%s' to `%s'"; | |||
39 | 39 | ||
40 | static char *pattern; | 40 | static char *pattern; |
41 | 41 | ||
42 | #ifdef CONFIG_FEATURE_FIND_TYPE | 42 | #if ENABLE_FEATURE_FIND_TYPE |
43 | static int type_mask = 0; | 43 | static int type_mask = 0; |
44 | #endif | 44 | #endif |
45 | 45 | ||
46 | #ifdef CONFIG_FEATURE_FIND_PERM | 46 | #if ENABLE_FEATURE_FIND_PERM |
47 | static char perm_char = 0; | 47 | static char perm_char = 0; |
48 | static int perm_mask = 0; | 48 | static int perm_mask = 0; |
49 | #endif | 49 | #endif |
50 | 50 | ||
51 | #ifdef CONFIG_FEATURE_FIND_MTIME | 51 | #if ENABLE_FEATURE_FIND_MTIME |
52 | static char mtime_char; | 52 | static char mtime_char; |
53 | static int mtime_days; | 53 | static int mtime_days; |
54 | #endif | 54 | #endif |
55 | 55 | ||
56 | #ifdef CONFIG_FEATURE_FIND_XDEV | 56 | #if ENABLE_FEATURE_FIND_XDEV |
57 | static dev_t *xdev_dev; | 57 | static dev_t *xdev_dev; |
58 | static int xdev_count = 0; | 58 | static int xdev_count = 0; |
59 | #endif | 59 | #endif |
60 | 60 | ||
61 | #ifdef CONFIG_FEATURE_FIND_NEWER | 61 | #if ENABLE_FEATURE_FIND_NEWER |
62 | static time_t newer_mtime; | 62 | static time_t newer_mtime; |
63 | #endif | 63 | #endif |
64 | 64 | ||
65 | #ifdef CONFIG_FEATURE_FIND_INUM | 65 | #if ENABLE_FEATURE_FIND_INUM |
66 | static ino_t inode_num; | 66 | static ino_t inode_num; |
67 | #endif | 67 | #endif |
68 | 68 | ||
69 | #if ENABLE_FEATURE_FIND_EXEC | ||
70 | static char **exec_str; | ||
71 | static int num_matches; | ||
72 | static int exec_opt; | ||
73 | #endif | ||
74 | |||
69 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) | 75 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
70 | { | 76 | { |
71 | if (pattern != NULL) { | 77 | if (pattern != NULL) { |
@@ -78,22 +84,17 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk) | |||
78 | if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0)) | 84 | if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0)) |
79 | goto no_match; | 85 | goto no_match; |
80 | } | 86 | } |
81 | #ifdef CONFIG_FEATURE_FIND_TYPE | 87 | if (ENABLE_FEATURE_FIND_TYPE && type_mask != 0) { |
82 | if (type_mask != 0) { | ||
83 | if (!((statbuf->st_mode & S_IFMT) == type_mask)) | 88 | if (!((statbuf->st_mode & S_IFMT) == type_mask)) |
84 | goto no_match; | 89 | goto no_match; |
85 | } | 90 | } |
86 | #endif | 91 | if (ENABLE_FEATURE_FIND_PERM && perm_mask != 0) { |
87 | #ifdef CONFIG_FEATURE_FIND_PERM | ||
88 | if (perm_mask != 0) { | ||
89 | if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) || | 92 | if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) || |
90 | (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) || | 93 | (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) || |
91 | (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0))) | 94 | (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0))) |
92 | goto no_match; | 95 | goto no_match; |
93 | } | 96 | } |
94 | #endif | 97 | if (ENABLE_FEATURE_FIND_MTIME && mtime_char != 0) { |
95 | #ifdef CONFIG_FEATURE_FIND_MTIME | ||
96 | if (mtime_char != 0) { | ||
97 | time_t file_age = time(NULL) - statbuf->st_mtime; | 98 | time_t file_age = time(NULL) - statbuf->st_mtime; |
98 | time_t mtime_secs = mtime_days * 24 * 60 * 60; | 99 | time_t mtime_secs = mtime_days * 24 * 60 * 60; |
99 | if (!((isdigit(mtime_char) && file_age >= mtime_secs && | 100 | if (!((isdigit(mtime_char) && file_age >= mtime_secs && |
@@ -102,9 +103,7 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk) | |||
102 | (mtime_char == '-' && file_age < mtime_secs))) | 103 | (mtime_char == '-' && file_age < mtime_secs))) |
103 | goto no_match; | 104 | goto no_match; |
104 | } | 105 | } |
105 | #endif | 106 | if (ENABLE_FEATURE_FIND_XDEV && xdev_count) { |
106 | #ifdef CONFIG_FEATURE_FIND_XDEV | ||
107 | if (xdev_count) { | ||
108 | int i; | 107 | int i; |
109 | for (i=0; i<xdev_count; i++) { | 108 | for (i=0; i<xdev_count; i++) { |
110 | if (xdev_dev[i] == statbuf-> st_dev) | 109 | if (xdev_dev[i] == statbuf-> st_dev) |
@@ -117,26 +116,31 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk) | |||
117 | goto no_match; | 116 | goto no_match; |
118 | } | 117 | } |
119 | } | 118 | } |
120 | #endif | 119 | if (ENABLE_FEATURE_FIND_NEWER && newer_mtime != 0) { |
121 | #ifdef CONFIG_FEATURE_FIND_NEWER | ||
122 | if (newer_mtime != 0) { | ||
123 | time_t file_age = newer_mtime - statbuf->st_mtime; | 120 | time_t file_age = newer_mtime - statbuf->st_mtime; |
124 | if (file_age >= 0) | 121 | if (file_age >= 0) |
125 | goto no_match; | 122 | goto no_match; |
126 | } | 123 | } |
127 | #endif | 124 | if (ENABLE_FEATURE_FIND_INUM && inode_num != 0) { |
128 | #ifdef CONFIG_FEATURE_FIND_INUM | ||
129 | if (inode_num != 0) { | ||
130 | if (!(statbuf->st_ino == inode_num)) | 125 | if (!(statbuf->st_ino == inode_num)) |
131 | goto no_match; | 126 | goto no_match; |
132 | } | 127 | } |
133 | #endif | 128 | if (ENABLE_FEATURE_FIND_EXEC && exec_opt) { |
129 | int i; | ||
130 | char *cmd_string = ""; | ||
131 | for (i = 0; i < num_matches; i++) | ||
132 | cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName); | ||
133 | cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]); | ||
134 | system(cmd_string); | ||
135 | goto no_match; | ||
136 | } | ||
137 | |||
134 | puts(fileName); | 138 | puts(fileName); |
135 | no_match: | 139 | no_match: |
136 | return (TRUE); | 140 | return (TRUE); |
137 | } | 141 | } |
138 | 142 | ||
139 | #ifdef CONFIG_FEATURE_FIND_TYPE | 143 | #if ENABLE_FEATURE_FIND_TYPE |
140 | static int find_type(char *type) | 144 | static int find_type(char *type) |
141 | { | 145 | { |
142 | int mask = 0; | 146 | int mask = 0; |
@@ -193,14 +197,11 @@ int find_main(int argc, char **argv) | |||
193 | if (++i == argc) | 197 | if (++i == argc) |
194 | bb_error_msg_and_die(msg_req_arg, "-name"); | 198 | bb_error_msg_and_die(msg_req_arg, "-name"); |
195 | pattern = argv[i]; | 199 | pattern = argv[i]; |
196 | #ifdef CONFIG_FEATURE_FIND_TYPE | 200 | } else if (ENABLE_FEATURE_FIND_TYPE && strcmp(argv[i], "-type") == 0) { |
197 | } else if (strcmp(argv[i], "-type") == 0) { | ||
198 | if (++i == argc) | 201 | if (++i == argc) |
199 | bb_error_msg_and_die(msg_req_arg, "-type"); | 202 | bb_error_msg_and_die(msg_req_arg, "-type"); |
200 | type_mask = find_type(argv[i]); | 203 | type_mask = find_type(argv[i]); |
201 | #endif | 204 | } else if (ENABLE_FEATURE_FIND_PERM && strcmp(argv[i], "-perm") == 0) { |
202 | #ifdef CONFIG_FEATURE_FIND_PERM | ||
203 | } else if (strcmp(argv[i], "-perm") == 0) { | ||
204 | char *end; | 205 | char *end; |
205 | if (++i == argc) | 206 | if (++i == argc) |
206 | bb_error_msg_and_die(msg_req_arg, "-perm"); | 207 | bb_error_msg_and_die(msg_req_arg, "-perm"); |
@@ -209,9 +210,7 @@ int find_main(int argc, char **argv) | |||
209 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm"); | 210 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm"); |
210 | if ((perm_char = argv[i][0]) == '-') | 211 | if ((perm_char = argv[i][0]) == '-') |
211 | perm_mask = -perm_mask; | 212 | perm_mask = -perm_mask; |
212 | #endif | 213 | } else if (ENABLE_FEATURE_FIND_MTIME && strcmp(argv[i], "-mtime") == 0) { |
213 | #ifdef CONFIG_FEATURE_FIND_MTIME | ||
214 | } else if (strcmp(argv[i], "-mtime") == 0) { | ||
215 | char *end; | 214 | char *end; |
216 | if (++i == argc) | 215 | if (++i == argc) |
217 | bb_error_msg_and_die(msg_req_arg, "-mtime"); | 216 | bb_error_msg_and_die(msg_req_arg, "-mtime"); |
@@ -220,9 +219,7 @@ int find_main(int argc, char **argv) | |||
220 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime"); | 219 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime"); |
221 | if ((mtime_char = argv[i][0]) == '-') | 220 | if ((mtime_char = argv[i][0]) == '-') |
222 | mtime_days = -mtime_days; | 221 | mtime_days = -mtime_days; |
223 | #endif | 222 | } else if (ENABLE_FEATURE_FIND_XDEV && strcmp(argv[i], "-xdev") == 0) { |
224 | #ifdef CONFIG_FEATURE_FIND_XDEV | ||
225 | } else if (strcmp(argv[i], "-xdev") == 0) { | ||
226 | struct stat stbuf; | 223 | struct stat stbuf; |
227 | 224 | ||
228 | xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1; | 225 | xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1; |
@@ -241,25 +238,45 @@ int find_main(int argc, char **argv) | |||
241 | xdev_dev [i-1] = stbuf. st_dev; | 238 | xdev_dev [i-1] = stbuf. st_dev; |
242 | } | 239 | } |
243 | } | 240 | } |
244 | #endif | 241 | } else if (ENABLE_FEATURE_FIND_NEWER && strcmp(argv[i], "-newer") == 0) { |
245 | #ifdef CONFIG_FEATURE_FIND_NEWER | ||
246 | } else if (strcmp(argv[i], "-newer") == 0) { | ||
247 | struct stat stat_newer; | 242 | struct stat stat_newer; |
248 | if (++i == argc) | 243 | if (++i == argc) |
249 | bb_error_msg_and_die(msg_req_arg, "-newer"); | 244 | bb_error_msg_and_die(msg_req_arg, "-newer"); |
250 | if (stat (argv[i], &stat_newer) != 0) | 245 | if (stat (argv[i], &stat_newer) != 0) |
251 | bb_error_msg_and_die("file %s not found", argv[i]); | 246 | bb_error_msg_and_die("file %s not found", argv[i]); |
252 | newer_mtime = stat_newer.st_mtime; | 247 | newer_mtime = stat_newer.st_mtime; |
253 | #endif | 248 | } else if (ENABLE_FEATURE_FIND_INUM && strcmp(argv[i], "-inum") == 0) { |
254 | #ifdef CONFIG_FEATURE_FIND_INUM | ||
255 | } else if (strcmp(argv[i], "-inum") == 0) { | ||
256 | char *end; | 249 | char *end; |
257 | if (++i == argc) | 250 | if (++i == argc) |
258 | bb_error_msg_and_die(msg_req_arg, "-inum"); | 251 | bb_error_msg_and_die(msg_req_arg, "-inum"); |
259 | inode_num = strtol(argv[i], &end, 10); | 252 | inode_num = strtol(argv[i], &end, 10); |
260 | if (end[0] != '\0') | 253 | if (end[0] != '\0') |
261 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum"); | 254 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum"); |
262 | #endif | 255 | } else if (ENABLE_FEATURE_FIND_EXEC && strcmp(argv[i], "-exec") == 0) { |
256 | int b_pos; | ||
257 | char *cmd_string = ""; | ||
258 | |||
259 | while (i++) { | ||
260 | if (i == argc) | ||
261 | bb_error_msg_and_die(msg_req_arg, "-exec"); | ||
262 | if (*argv[i] == ';') | ||
263 | break; | ||
264 | cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]); | ||
265 | } | ||
266 | |||
267 | if (*cmd_string == 0) | ||
268 | bb_error_msg_and_die(msg_req_arg, "-exec"); | ||
269 | cmd_string++; | ||
270 | exec_str = xmalloc(sizeof(char *)); | ||
271 | |||
272 | while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) { | ||
273 | num_matches++; | ||
274 | exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *)); | ||
275 | exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos); | ||
276 | cmd_string += b_pos + 2; | ||
277 | } | ||
278 | exec_str[num_matches] = bb_xstrdup(cmd_string); | ||
279 | exec_opt = 1; | ||
263 | } else | 280 | } else |
264 | bb_show_usage(); | 281 | bb_show_usage(); |
265 | } | 282 | } |