Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini find implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 7 | * Reworked by David Douthitt <n9ubh@callsign.net> and |
| 8 | * Matt Kraai <kraai@alumni.carnegiemellon.edu>. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 9 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 14 | #include <fnmatch.h> |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 15 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 16 | static char *pattern; |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 17 | #ifdef CONFIG_FEATURE_FIND_PRINT0 |
| 18 | static char printsep = '\n'; |
| 19 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 20 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 22 | static int type_mask = 0; |
| 23 | #endif |
| 24 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 25 | #ifdef CONFIG_FEATURE_FIND_PERM |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 26 | static char perm_char = 0; |
| 27 | static int perm_mask = 0; |
| 28 | #endif |
| 29 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 30 | #ifdef CONFIG_FEATURE_FIND_MTIME |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 31 | static char mtime_char; |
| 32 | static int mtime_days; |
| 33 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 34 | |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 35 | #ifdef CONFIG_FEATURE_FIND_MMIN |
| 36 | static char mmin_char; |
| 37 | static int mmin_mins; |
| 38 | #endif |
| 39 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 40 | #ifdef CONFIG_FEATURE_FIND_XDEV |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 41 | static dev_t *xdev_dev; |
| 42 | static int xdev_count = 0; |
| 43 | #endif |
| 44 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_FEATURE_FIND_NEWER |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 46 | static time_t newer_mtime; |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 49 | #ifdef CONFIG_FEATURE_FIND_INUM |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 50 | static ino_t inode_num; |
| 51 | #endif |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 52 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 53 | #ifdef CONFIG_FEATURE_FIND_EXEC |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 54 | static char **exec_str; |
| 55 | static int num_matches; |
| 56 | static int exec_opt; |
| 57 | #endif |
| 58 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 59 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 60 | { |
Paul Fox | 8416a2d | 2006-03-27 16:42:33 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 62 | if (S_ISDIR(statbuf->st_mode) && xdev_count) { |
| 63 | int i; |
| 64 | for (i=0; i<xdev_count; i++) { |
| 65 | if (xdev_dev[i] != statbuf->st_dev) |
| 66 | return SKIP; |
| 67 | } |
| 68 | } |
| 69 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 70 | if (pattern != NULL) { |
| 71 | const char *tmp = strrchr(fileName, '/'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | |
| 73 | if (tmp == NULL) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 74 | tmp = fileName; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | else |
| 76 | tmp++; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 77 | if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0)) |
| 78 | goto no_match; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 80 | #ifdef CONFIG_FEATURE_FIND_TYPE |
| 81 | if (type_mask != 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 82 | if (!((statbuf->st_mode & S_IFMT) == type_mask)) |
| 83 | goto no_match; |
| 84 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 85 | #endif |
| 86 | #ifdef CONFIG_FEATURE_FIND_PERM |
| 87 | if (perm_mask != 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 88 | if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) || |
| 89 | (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) || |
| 90 | (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0))) |
| 91 | goto no_match; |
| 92 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 93 | #endif |
| 94 | #ifdef CONFIG_FEATURE_FIND_MTIME |
| 95 | if (mtime_char != 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 96 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 97 | time_t mtime_secs = mtime_days * 24 * 60 * 60; |
Glenn L McGrath | 49b0f86 | 2002-12-11 21:22:21 +0000 | [diff] [blame] | 98 | if (!((isdigit(mtime_char) && file_age >= mtime_secs && |
| 99 | file_age < mtime_secs + 24 * 60 * 60) || |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 100 | (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) || |
Glenn L McGrath | 49b0f86 | 2002-12-11 21:22:21 +0000 | [diff] [blame] | 101 | (mtime_char == '-' && file_age < mtime_secs))) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 102 | goto no_match; |
| 103 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 104 | #endif |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 105 | #ifdef CONFIG_FEATURE_FIND_MMIN |
| 106 | if (mmin_char != 0) { |
| 107 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 108 | time_t mmin_secs = mmin_mins * 60; |
| 109 | if (!((isdigit(mmin_char) && file_age >= mmin_secs && |
| 110 | file_age < mmin_secs + 60) || |
| 111 | (mmin_char == '+' && file_age >= mmin_secs + 60) || |
| 112 | (mmin_char == '-' && file_age < mmin_secs))) |
| 113 | goto no_match; |
| 114 | } |
| 115 | #endif |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 116 | #ifdef CONFIG_FEATURE_FIND_NEWER |
| 117 | if (newer_mtime != 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 118 | time_t file_age = newer_mtime - statbuf->st_mtime; |
| 119 | if (file_age >= 0) |
| 120 | goto no_match; |
| 121 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 122 | #endif |
| 123 | #ifdef CONFIG_FEATURE_FIND_INUM |
| 124 | if (inode_num != 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 125 | if (!(statbuf->st_ino == inode_num)) |
| 126 | goto no_match; |
| 127 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 128 | #endif |
| 129 | #ifdef CONFIG_FEATURE_FIND_EXEC |
| 130 | if (exec_opt) { |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 131 | int i; |
| 132 | char *cmd_string = ""; |
| 133 | for (i = 0; i < num_matches; i++) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 134 | cmd_string = xasprintf("%s%s%s", cmd_string, exec_str[i], fileName); |
| 135 | cmd_string = xasprintf("%s%s", cmd_string, exec_str[num_matches]); |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 136 | system(cmd_string); |
| 137 | goto no_match; |
| 138 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 139 | #endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 140 | |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 141 | #ifdef CONFIG_FEATURE_FIND_PRINT0 |
| 142 | printf("%s%c", fileName, printsep); |
| 143 | #else |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 144 | puts(fileName); |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 145 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 146 | no_match: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | return (TRUE); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 150 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 151 | static int find_type(char *type) |
| 152 | { |
| 153 | int mask = 0; |
| 154 | |
| 155 | switch (type[0]) { |
| 156 | case 'b': |
| 157 | mask = S_IFBLK; |
| 158 | break; |
| 159 | case 'c': |
| 160 | mask = S_IFCHR; |
| 161 | break; |
| 162 | case 'd': |
| 163 | mask = S_IFDIR; |
| 164 | break; |
| 165 | case 'p': |
| 166 | mask = S_IFIFO; |
| 167 | break; |
| 168 | case 'f': |
| 169 | mask = S_IFREG; |
| 170 | break; |
| 171 | case 'l': |
| 172 | mask = S_IFLNK; |
| 173 | break; |
| 174 | case 's': |
| 175 | mask = S_IFSOCK; |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | if (mask == 0 || type[1] != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 180 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 181 | |
| 182 | return mask; |
| 183 | } |
| 184 | #endif |
| 185 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 186 | int find_main(int argc, char **argv) |
| 187 | { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 188 | int dereference = FALSE; |
| 189 | int i, firstopt, status = EXIT_SUCCESS; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 190 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 191 | for (firstopt = 1; firstopt < argc; firstopt++) { |
| 192 | if (argv[firstopt][0] == '-') |
| 193 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 194 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 195 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | /* Parse any options */ |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 197 | for (i = firstopt; i < argc; i++) { |
| 198 | if (strcmp(argv[i], "-follow") == 0) |
| 199 | dereference = TRUE; |
Mark Whitley | e0a7f91 | 2001-03-28 22:04:42 +0000 | [diff] [blame] | 200 | else if (strcmp(argv[i], "-print") == 0) { |
| 201 | ; |
| 202 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 203 | #ifdef CONFIG_FEATURE_FIND_PRINT0 |
| 204 | else if (strcmp(argv[i], "-print0") == 0) |
| 205 | printsep = '\0'; |
| 206 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 207 | else if (strcmp(argv[i], "-name") == 0) { |
| 208 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 209 | bb_error_msg_and_die(bb_msg_requires_arg, "-name"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 210 | pattern = argv[i]; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 211 | #ifdef CONFIG_FEATURE_FIND_TYPE |
| 212 | } else if (strcmp(argv[i], "-type") == 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 213 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 214 | bb_error_msg_and_die(bb_msg_requires_arg, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 215 | type_mask = find_type(argv[i]); |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 216 | #endif |
| 217 | #ifdef CONFIG_FEATURE_FIND_PERM |
| 218 | } else if (strcmp(argv[i], "-perm") == 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 219 | char *end; |
| 220 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 221 | bb_error_msg_and_die(bb_msg_requires_arg, "-perm"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 222 | perm_mask = strtol(argv[i], &end, 8); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 223 | if ((end[0] != '\0') || (perm_mask > 07777)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 224 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-perm"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 225 | if ((perm_char = argv[i][0]) == '-') |
| 226 | perm_mask = -perm_mask; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifdef CONFIG_FEATURE_FIND_MTIME |
| 229 | } else if (strcmp(argv[i], "-mtime") == 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 230 | char *end; |
| 231 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 232 | bb_error_msg_and_die(bb_msg_requires_arg, "-mtime"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 233 | mtime_days = strtol(argv[i], &end, 10); |
| 234 | if (end[0] != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 235 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mtime"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 236 | if ((mtime_char = argv[i][0]) == '-') |
| 237 | mtime_days = -mtime_days; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 238 | #endif |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 239 | #ifdef CONFIG_FEATURE_FIND_MMIN |
| 240 | } else if (strcmp(argv[i], "-mmin") == 0) { |
| 241 | char *end; |
| 242 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 243 | bb_error_msg_and_die(bb_msg_requires_arg, "-mmin"); |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 244 | mmin_mins = strtol(argv[i], &end, 10); |
| 245 | if (end[0] != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 246 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mmin"); |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 247 | if ((mmin_char = argv[i][0]) == '-') |
| 248 | mmin_mins = -mmin_mins; |
| 249 | #endif |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 250 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 251 | } else if (strcmp(argv[i], "-xdev") == 0) { |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 252 | struct stat stbuf; |
| 253 | |
| 254 | xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1; |
| 255 | xdev_dev = xmalloc ( xdev_count * sizeof( dev_t )); |
| 256 | |
| 257 | if ( firstopt == 1 ) { |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 258 | xstat ( ".", &stbuf ); |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 259 | xdev_dev [0] = stbuf. st_dev; |
| 260 | } |
| 261 | else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 262 | |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 263 | for (i = 1; i < firstopt; i++) { |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 264 | xstat ( argv [i], &stbuf ); |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 265 | xdev_dev [i-1] = stbuf. st_dev; |
| 266 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 267 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #ifdef CONFIG_FEATURE_FIND_NEWER |
| 270 | } else if (strcmp(argv[i], "-newer") == 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 271 | struct stat stat_newer; |
| 272 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 273 | bb_error_msg_and_die(bb_msg_requires_arg, "-newer"); |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 274 | xstat (argv[i], &stat_newer); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 275 | newer_mtime = stat_newer.st_mtime; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 276 | #endif |
| 277 | #ifdef CONFIG_FEATURE_FIND_INUM |
| 278 | } else if (strcmp(argv[i], "-inum") == 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 279 | char *end; |
| 280 | if (++i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 281 | bb_error_msg_and_die(bb_msg_requires_arg, "-inum"); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 282 | inode_num = strtol(argv[i], &end, 10); |
| 283 | if (end[0] != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 284 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-inum"); |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifdef CONFIG_FEATURE_FIND_EXEC |
| 287 | } else if (strcmp(argv[i], "-exec") == 0) { |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 288 | int b_pos; |
| 289 | char *cmd_string = ""; |
| 290 | |
| 291 | while (i++) { |
| 292 | if (i == argc) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 293 | bb_error_msg_and_die(bb_msg_requires_arg, "-exec"); |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 294 | if (*argv[i] == ';') |
| 295 | break; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 296 | cmd_string = xasprintf("%s %s", cmd_string, argv[i]); |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 297 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 298 | |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 299 | if (*cmd_string == 0) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 300 | bb_error_msg_and_die(bb_msg_requires_arg, "-exec"); |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 301 | cmd_string++; |
| 302 | exec_str = xmalloc(sizeof(char *)); |
| 303 | |
| 304 | while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) { |
| 305 | num_matches++; |
| 306 | exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *)); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 307 | exec_str[num_matches - 1] = xstrndup(cmd_string, b_pos); |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 308 | cmd_string += b_pos + 2; |
| 309 | } |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 310 | exec_str[num_matches] = xstrdup(cmd_string); |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 311 | exec_opt = 1; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 312 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 313 | } else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 314 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 317 | if (firstopt == 1) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 318 | if (! recursive_action(".", TRUE, dereference, FALSE, fileAction, |
| 319 | fileAction, NULL)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 320 | status = EXIT_FAILURE; |
| 321 | } else { |
| 322 | for (i = 1; i < firstopt; i++) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 323 | if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction, |
| 324 | fileAction, NULL)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 325 | status = EXIT_FAILURE; |
| 326 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 329 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 330 | } |