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