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 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <dirent.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 31 | #include <fnmatch.h> |
| 32 | #include <time.h> |
| 33 | #include <ctype.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 34 | #include "busybox.h" |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 36 | //XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz |
"Vladimir N. Oleynik" | 007a011 | 2005-09-22 11:11:11 +0000 | [diff] [blame] | 37 | static const char msg_req_arg[] = "option `%s' requires an argument"; |
| 38 | static const char msg_invalid_arg[] = "invalid argument `%s' to `%s'"; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 39 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 40 | static char *pattern; |
| 41 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 42 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 43 | static int type_mask = 0; |
| 44 | #endif |
| 45 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 46 | #ifdef CONFIG_FEATURE_FIND_PERM |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 47 | static char perm_char = 0; |
| 48 | static int perm_mask = 0; |
| 49 | #endif |
| 50 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_FEATURE_FIND_MTIME |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 52 | static char mtime_char; |
| 53 | static int mtime_days; |
| 54 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 55 | |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 56 | #ifdef CONFIG_FEATURE_FIND_MMIN |
| 57 | static char mmin_char; |
| 58 | static int mmin_mins; |
| 59 | #endif |
| 60 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_FEATURE_FIND_XDEV |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 62 | static dev_t *xdev_dev; |
| 63 | static int xdev_count = 0; |
| 64 | #endif |
| 65 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 66 | #ifdef CONFIG_FEATURE_FIND_NEWER |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 67 | static time_t newer_mtime; |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 70 | #ifdef CONFIG_FEATURE_FIND_INUM |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 71 | static ino_t inode_num; |
| 72 | #endif |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 73 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 74 | #ifdef CONFIG_FEATURE_FIND_EXEC |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 75 | static char **exec_str; |
| 76 | static int num_matches; |
| 77 | static int exec_opt; |
| 78 | #endif |
| 79 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 80 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 81 | { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 82 | if (pattern != NULL) { |
| 83 | const char *tmp = strrchr(fileName, '/'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | |
| 85 | if (tmp == NULL) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 86 | tmp = fileName; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | else |
| 88 | tmp++; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 89 | if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0)) |
| 90 | goto no_match; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 92 | #ifdef CONFIG_FEATURE_FIND_TYPE |
| 93 | if (type_mask != 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 94 | if (!((statbuf->st_mode & S_IFMT) == type_mask)) |
| 95 | goto no_match; |
| 96 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 97 | #endif |
| 98 | #ifdef CONFIG_FEATURE_FIND_PERM |
| 99 | if (perm_mask != 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 100 | if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) || |
| 101 | (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) || |
| 102 | (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0))) |
| 103 | goto no_match; |
| 104 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 105 | #endif |
| 106 | #ifdef CONFIG_FEATURE_FIND_MTIME |
| 107 | if (mtime_char != 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 108 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 109 | time_t mtime_secs = mtime_days * 24 * 60 * 60; |
Glenn L McGrath | 49b0f86 | 2002-12-11 21:22:21 +0000 | [diff] [blame] | 110 | if (!((isdigit(mtime_char) && file_age >= mtime_secs && |
| 111 | file_age < mtime_secs + 24 * 60 * 60) || |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 112 | (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) || |
Glenn L McGrath | 49b0f86 | 2002-12-11 21:22:21 +0000 | [diff] [blame] | 113 | (mtime_char == '-' && file_age < mtime_secs))) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 114 | goto no_match; |
| 115 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 116 | #endif |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 117 | #ifdef CONFIG_FEATURE_FIND_MMIN |
| 118 | if (mmin_char != 0) { |
| 119 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 120 | time_t mmin_secs = mmin_mins * 60; |
| 121 | if (!((isdigit(mmin_char) && file_age >= mmin_secs && |
| 122 | file_age < mmin_secs + 60) || |
| 123 | (mmin_char == '+' && file_age >= mmin_secs + 60) || |
| 124 | (mmin_char == '-' && file_age < mmin_secs))) |
| 125 | goto no_match; |
| 126 | } |
| 127 | #endif |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 128 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 129 | if (xdev_count) { |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 130 | int i; |
| 131 | for (i=0; i<xdev_count; i++) { |
| 132 | if (xdev_dev[i] == statbuf-> st_dev) |
| 133 | break; |
| 134 | } |
| 135 | if (i == xdev_count) { |
| 136 | if(S_ISDIR(statbuf->st_mode)) |
| 137 | return SKIP; |
| 138 | else |
| 139 | goto no_match; |
| 140 | } |
| 141 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 142 | #endif |
| 143 | #ifdef CONFIG_FEATURE_FIND_NEWER |
| 144 | if (newer_mtime != 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 145 | time_t file_age = newer_mtime - statbuf->st_mtime; |
| 146 | if (file_age >= 0) |
| 147 | goto no_match; |
| 148 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 149 | #endif |
| 150 | #ifdef CONFIG_FEATURE_FIND_INUM |
| 151 | if (inode_num != 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 152 | if (!(statbuf->st_ino == inode_num)) |
| 153 | goto no_match; |
| 154 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 155 | #endif |
| 156 | #ifdef CONFIG_FEATURE_FIND_EXEC |
| 157 | if (exec_opt) { |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 158 | int i; |
| 159 | char *cmd_string = ""; |
| 160 | for (i = 0; i < num_matches; i++) |
| 161 | cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName); |
| 162 | cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]); |
| 163 | system(cmd_string); |
| 164 | goto no_match; |
| 165 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 166 | #endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame^] | 167 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 168 | puts(fileName); |
| 169 | no_match: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 170 | return (TRUE); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 173 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 174 | static int find_type(char *type) |
| 175 | { |
| 176 | int mask = 0; |
| 177 | |
| 178 | switch (type[0]) { |
| 179 | case 'b': |
| 180 | mask = S_IFBLK; |
| 181 | break; |
| 182 | case 'c': |
| 183 | mask = S_IFCHR; |
| 184 | break; |
| 185 | case 'd': |
| 186 | mask = S_IFDIR; |
| 187 | break; |
| 188 | case 'p': |
| 189 | mask = S_IFIFO; |
| 190 | break; |
| 191 | case 'f': |
| 192 | mask = S_IFREG; |
| 193 | break; |
| 194 | case 'l': |
| 195 | mask = S_IFLNK; |
| 196 | break; |
| 197 | case 's': |
| 198 | mask = S_IFSOCK; |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | if (mask == 0 || type[1] != '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 203 | bb_error_msg_and_die(msg_invalid_arg, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 204 | |
| 205 | return mask; |
| 206 | } |
| 207 | #endif |
| 208 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 209 | int find_main(int argc, char **argv) |
| 210 | { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 211 | int dereference = FALSE; |
| 212 | int i, firstopt, status = EXIT_SUCCESS; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 213 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 214 | for (firstopt = 1; firstopt < argc; firstopt++) { |
| 215 | if (argv[firstopt][0] == '-') |
| 216 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 217 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 218 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 219 | /* Parse any options */ |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 220 | for (i = firstopt; i < argc; i++) { |
| 221 | if (strcmp(argv[i], "-follow") == 0) |
| 222 | dereference = TRUE; |
Mark Whitley | e0a7f91 | 2001-03-28 22:04:42 +0000 | [diff] [blame] | 223 | else if (strcmp(argv[i], "-print") == 0) { |
| 224 | ; |
| 225 | } |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 226 | else if (strcmp(argv[i], "-name") == 0) { |
| 227 | if (++i == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 228 | bb_error_msg_and_die(msg_req_arg, "-name"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 229 | pattern = argv[i]; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 230 | #ifdef CONFIG_FEATURE_FIND_TYPE |
| 231 | } else if (strcmp(argv[i], "-type") == 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 232 | if (++i == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | bb_error_msg_and_die(msg_req_arg, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 234 | type_mask = find_type(argv[i]); |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 235 | #endif |
| 236 | #ifdef CONFIG_FEATURE_FIND_PERM |
| 237 | } else if (strcmp(argv[i], "-perm") == 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 238 | char *end; |
| 239 | if (++i == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 240 | bb_error_msg_and_die(msg_req_arg, "-perm"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 241 | perm_mask = strtol(argv[i], &end, 8); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 242 | if ((end[0] != '\0') || (perm_mask > 07777)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 243 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 244 | if ((perm_char = argv[i][0]) == '-') |
| 245 | perm_mask = -perm_mask; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #ifdef CONFIG_FEATURE_FIND_MTIME |
| 248 | } else if (strcmp(argv[i], "-mtime") == 0) { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 249 | char *end; |
| 250 | if (++i == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 251 | bb_error_msg_and_die(msg_req_arg, "-mtime"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 252 | mtime_days = strtol(argv[i], &end, 10); |
| 253 | if (end[0] != '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 254 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 255 | if ((mtime_char = argv[i][0]) == '-') |
| 256 | mtime_days = -mtime_days; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 257 | #endif |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 258 | #ifdef CONFIG_FEATURE_FIND_MMIN |
| 259 | } else if (strcmp(argv[i], "-mmin") == 0) { |
| 260 | char *end; |
| 261 | if (++i == argc) |
| 262 | bb_error_msg_and_die(msg_req_arg, "-mmin"); |
| 263 | mmin_mins = strtol(argv[i], &end, 10); |
| 264 | if (end[0] != '\0') |
| 265 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mmin"); |
| 266 | if ((mmin_char = argv[i][0]) == '-') |
| 267 | mmin_mins = -mmin_mins; |
| 268 | #endif |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 269 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 270 | } else if (strcmp(argv[i], "-xdev") == 0) { |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 271 | struct stat stbuf; |
| 272 | |
| 273 | xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1; |
| 274 | xdev_dev = xmalloc ( xdev_count * sizeof( dev_t )); |
| 275 | |
| 276 | if ( firstopt == 1 ) { |
| 277 | if ( stat ( ".", &stbuf ) < 0 ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 278 | bb_error_msg_and_die("could not stat '.'" ); |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 279 | xdev_dev [0] = stbuf. st_dev; |
| 280 | } |
| 281 | else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 282 | |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 283 | for (i = 1; i < firstopt; i++) { |
| 284 | if ( stat ( argv [i], &stbuf ) < 0 ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 285 | bb_error_msg_and_die("could not stat '%s'", argv [i] ); |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 286 | xdev_dev [i-1] = stbuf. st_dev; |
| 287 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 288 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 289 | #endif |
| 290 | #ifdef CONFIG_FEATURE_FIND_NEWER |
| 291 | } else if (strcmp(argv[i], "-newer") == 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 292 | struct stat stat_newer; |
| 293 | if (++i == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 294 | bb_error_msg_and_die(msg_req_arg, "-newer"); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 295 | if (stat (argv[i], &stat_newer) != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 296 | bb_error_msg_and_die("file %s not found", argv[i]); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 297 | newer_mtime = stat_newer.st_mtime; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 298 | #endif |
| 299 | #ifdef CONFIG_FEATURE_FIND_INUM |
| 300 | } else if (strcmp(argv[i], "-inum") == 0) { |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 301 | char *end; |
| 302 | if (++i == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 303 | bb_error_msg_and_die(msg_req_arg, "-inum"); |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 304 | inode_num = strtol(argv[i], &end, 10); |
| 305 | if (end[0] != '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 306 | bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum"); |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 307 | #endif |
| 308 | #ifdef CONFIG_FEATURE_FIND_EXEC |
| 309 | } else if (strcmp(argv[i], "-exec") == 0) { |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 310 | int b_pos; |
| 311 | char *cmd_string = ""; |
| 312 | |
| 313 | while (i++) { |
| 314 | if (i == argc) |
| 315 | bb_error_msg_and_die(msg_req_arg, "-exec"); |
| 316 | if (*argv[i] == ';') |
| 317 | break; |
| 318 | cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]); |
| 319 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame^] | 320 | |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 321 | if (*cmd_string == 0) |
| 322 | bb_error_msg_and_die(msg_req_arg, "-exec"); |
| 323 | cmd_string++; |
| 324 | exec_str = xmalloc(sizeof(char *)); |
| 325 | |
| 326 | while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) { |
| 327 | num_matches++; |
| 328 | exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *)); |
| 329 | exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos); |
| 330 | cmd_string += b_pos + 2; |
| 331 | } |
| 332 | exec_str[num_matches] = bb_xstrdup(cmd_string); |
| 333 | exec_opt = 1; |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 334 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 335 | } else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 336 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 339 | if (firstopt == 1) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 340 | if (! recursive_action(".", TRUE, dereference, FALSE, fileAction, |
| 341 | fileAction, NULL)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 342 | status = EXIT_FAILURE; |
| 343 | } else { |
| 344 | for (i = 1; i < firstopt; i++) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 345 | if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction, |
| 346 | fileAction, NULL)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 347 | status = EXIT_FAILURE; |
| 348 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 351 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 352 | } |