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