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 | |
| 37 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 38 | static char *pattern; |
| 39 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 40 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 41 | static int type_mask = 0; |
| 42 | #endif |
| 43 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_FEATURE_FIND_PERM |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 45 | static char perm_char = 0; |
| 46 | static int perm_mask = 0; |
| 47 | #endif |
| 48 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 49 | #ifdef CONFIG_FEATURE_FIND_MTIME |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 50 | static char mtime_char; |
| 51 | static int mtime_days; |
| 52 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 53 | |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 55 | static dev_t *xdev_dev; |
| 56 | static int xdev_count = 0; |
| 57 | #endif |
| 58 | |
| 59 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 60 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 61 | { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 62 | if (pattern != NULL) { |
| 63 | const char *tmp = strrchr(fileName, '/'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | |
| 65 | if (tmp == NULL) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 66 | tmp = fileName; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | else |
| 68 | tmp++; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 69 | if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0)) |
| 70 | goto no_match; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 72 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 73 | if (type_mask != 0) { |
| 74 | if (!((statbuf->st_mode & S_IFMT) == type_mask)) |
| 75 | goto no_match; |
| 76 | } |
| 77 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_FEATURE_FIND_PERM |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 79 | if (perm_mask != 0) { |
| 80 | if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) || |
| 81 | (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) || |
| 82 | (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0))) |
| 83 | goto no_match; |
| 84 | } |
| 85 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_FEATURE_FIND_MTIME |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 87 | if (mtime_days != 0) { |
| 88 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 89 | time_t mtime_secs = mtime_days * 24 * 60 * 60; |
| 90 | if (!((isdigit(mtime_char) && mtime_secs >= file_age && |
| 91 | mtime_secs < file_age + 24 * 60 * 60) || |
| 92 | (mtime_char == '+' && mtime_secs >= file_age) || |
| 93 | (mtime_char == '-' && mtime_secs < file_age))) |
| 94 | goto no_match; |
| 95 | } |
| 96 | #endif |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 97 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 98 | if (xdev_count) { |
| 99 | int i; |
| 100 | for (i=0; i<xdev_count; i++) { |
| 101 | if (xdev_dev[i] == statbuf-> st_dev) |
| 102 | break; |
| 103 | } |
| 104 | if (i == xdev_count) { |
| 105 | if(S_ISDIR(statbuf->st_mode)) |
| 106 | return SKIP; |
| 107 | else |
| 108 | goto no_match; |
| 109 | } |
| 110 | } |
| 111 | #endif |
| 112 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 113 | puts(fileName); |
| 114 | no_match: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | return (TRUE); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 118 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 119 | static int find_type(char *type) |
| 120 | { |
| 121 | int mask = 0; |
| 122 | |
| 123 | switch (type[0]) { |
| 124 | case 'b': |
| 125 | mask = S_IFBLK; |
| 126 | break; |
| 127 | case 'c': |
| 128 | mask = S_IFCHR; |
| 129 | break; |
| 130 | case 'd': |
| 131 | mask = S_IFDIR; |
| 132 | break; |
| 133 | case 'p': |
| 134 | mask = S_IFIFO; |
| 135 | break; |
| 136 | case 'f': |
| 137 | mask = S_IFREG; |
| 138 | break; |
| 139 | case 'l': |
| 140 | mask = S_IFLNK; |
| 141 | break; |
| 142 | case 's': |
| 143 | mask = S_IFSOCK; |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | if (mask == 0 || type[1] != '\0') |
| 148 | error_msg_and_die("invalid argument `%s' to `-type'", type); |
| 149 | |
| 150 | return mask; |
| 151 | } |
| 152 | #endif |
| 153 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 154 | int find_main(int argc, char **argv) |
| 155 | { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 156 | int dereference = FALSE; |
| 157 | int i, firstopt, status = EXIT_SUCCESS; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 158 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 159 | for (firstopt = 1; firstopt < argc; firstopt++) { |
| 160 | if (argv[firstopt][0] == '-') |
| 161 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 162 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 163 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 164 | /* Parse any options */ |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 165 | for (i = firstopt; i < argc; i++) { |
| 166 | if (strcmp(argv[i], "-follow") == 0) |
| 167 | dereference = TRUE; |
Mark Whitley | e0a7f91 | 2001-03-28 22:04:42 +0000 | [diff] [blame] | 168 | else if (strcmp(argv[i], "-print") == 0) { |
| 169 | ; |
| 170 | } |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 171 | else if (strcmp(argv[i], "-name") == 0) { |
| 172 | if (++i == argc) |
| 173 | error_msg_and_die("option `-name' requires an argument"); |
| 174 | pattern = argv[i]; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 175 | #ifdef CONFIG_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 176 | } else if (strcmp(argv[i], "-type") == 0) { |
| 177 | if (++i == argc) |
| 178 | error_msg_and_die("option `-type' requires an argument"); |
| 179 | type_mask = find_type(argv[i]); |
| 180 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 181 | #ifdef CONFIG_FEATURE_FIND_PERM |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 182 | } else if (strcmp(argv[i], "-perm") == 0) { |
| 183 | char *end; |
| 184 | if (++i == argc) |
| 185 | error_msg_and_die("option `-perm' requires an argument"); |
| 186 | perm_mask = strtol(argv[i], &end, 8); |
| 187 | if (end[0] != '\0') |
| 188 | error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]); |
| 189 | if (perm_mask > 07777) |
| 190 | error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]); |
| 191 | if ((perm_char = argv[i][0]) == '-') |
| 192 | perm_mask = -perm_mask; |
| 193 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 194 | #ifdef CONFIG_FEATURE_FIND_MTIME |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 195 | } else if (strcmp(argv[i], "-mtime") == 0) { |
| 196 | char *end; |
| 197 | if (++i == argc) |
| 198 | error_msg_and_die("option `-mtime' requires an argument"); |
| 199 | mtime_days = strtol(argv[i], &end, 10); |
| 200 | if (end[0] != '\0') |
| 201 | error_msg_and_die("invalid argument `%s' to `-mtime'", argv[i]); |
| 202 | if ((mtime_char = argv[i][0]) == '-') |
| 203 | mtime_days = -mtime_days; |
| 204 | #endif |
Robert Griebl | 41369af | 2002-07-24 00:34:48 +0000 | [diff] [blame] | 205 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 206 | } else if (strcmp(argv[i], "-xdev") == 0) { |
| 207 | struct stat stbuf; |
| 208 | |
| 209 | xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1; |
| 210 | xdev_dev = xmalloc ( xdev_count * sizeof( dev_t )); |
| 211 | |
| 212 | if ( firstopt == 1 ) { |
| 213 | if ( stat ( ".", &stbuf ) < 0 ) |
| 214 | error_msg_and_die("could not stat '.'" ); |
| 215 | xdev_dev [0] = stbuf. st_dev; |
| 216 | } |
| 217 | else { |
| 218 | |
| 219 | for (i = 1; i < firstopt; i++) { |
| 220 | if ( stat ( argv [i], &stbuf ) < 0 ) |
| 221 | error_msg_and_die("could not stat '%s'", argv [i] ); |
| 222 | xdev_dev [i-1] = stbuf. st_dev; |
| 223 | } |
| 224 | } |
| 225 | #endif |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 226 | } else |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 227 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 230 | if (firstopt == 1) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 231 | if (! recursive_action(".", TRUE, dereference, FALSE, fileAction, |
| 232 | fileAction, NULL)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 233 | status = EXIT_FAILURE; |
| 234 | } else { |
| 235 | for (i = 1; i < firstopt; i++) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 236 | if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction, |
| 237 | fileAction, NULL)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 238 | status = EXIT_FAILURE; |
| 239 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 242 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 243 | } |