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 | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Eric Andersen | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 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> |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 31 | |
| 32 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | static char *pattern = NULL; |
| 34 | static char *directory = "."; |
| 35 | static int dereferenceFlag = FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 36 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 37 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 38 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | if (pattern == NULL) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 40 | puts(fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | else { |
| 42 | char *tmp = strrchr(fileName, '/'); |
| 43 | |
| 44 | if (tmp == NULL) |
| 45 | tmp = (char *) fileName; |
| 46 | else |
| 47 | tmp++; |
| 48 | if (check_wildcard_match(tmp, pattern) == TRUE) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 49 | puts(fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | } |
| 51 | return (TRUE); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 54 | int find_main(int argc, char **argv) |
| 55 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | /* peel off the "find" */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 57 | argc--; |
| 58 | argv++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | if (argc > 0 && **argv != '-') { |
| 61 | directory = *argv; |
| 62 | argc--; |
| 63 | argv++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 64 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 65 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | /* Parse any options */ |
| 67 | while (argc > 0 && **argv == '-') { |
| 68 | int stopit = FALSE; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 69 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | while (*++(*argv) && stopit == FALSE) |
| 71 | switch (**argv) { |
| 72 | case 'f': |
| 73 | if (strcmp(*argv, "follow") == 0) { |
| 74 | argc--; |
| 75 | argv++; |
| 76 | dereferenceFlag = TRUE; |
| 77 | } |
| 78 | break; |
| 79 | case 'n': |
| 80 | if (strcmp(*argv, "name") == 0) { |
| 81 | if (argc-- > 1) { |
| 82 | pattern = *(++argv); |
| 83 | stopit = TRUE; |
| 84 | } else { |
| 85 | usage(find_usage); |
| 86 | } |
| 87 | } |
| 88 | break; |
| 89 | case '-': |
| 90 | /* Ignore all long options */ |
| 91 | break; |
| 92 | default: |
| 93 | usage(find_usage); |
| 94 | } |
| 95 | if (argc-- > 1) |
| 96 | argv++; |
| 97 | if (**argv != '-') |
| 98 | break; |
| 99 | else |
| 100 | break; |
| 101 | } |
| 102 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 103 | if (recursive_action(directory, TRUE, FALSE, FALSE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 104 | fileAction, fileAction, NULL) == FALSE) { |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 105 | return EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 108 | return EXIT_SUCCESS; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 109 | } |