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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 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 | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 29 | |
| 30 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 31 | static char *pattern = NULL; |
| 32 | static char *directory = "."; |
| 33 | static int dereferenceFlag = FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 34 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 35 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 36 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | if (pattern == NULL) |
| 38 | fprintf(stdout, "%s\n", fileName); |
| 39 | else { |
| 40 | char *tmp = strrchr(fileName, '/'); |
| 41 | |
| 42 | if (tmp == NULL) |
| 43 | tmp = (char *) fileName; |
| 44 | else |
| 45 | tmp++; |
| 46 | if (check_wildcard_match(tmp, pattern) == TRUE) |
| 47 | fprintf(stdout, "%s\n", fileName); |
| 48 | } |
| 49 | return (TRUE); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 52 | int find_main(int argc, char **argv) |
| 53 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | /* peel off the "find" */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 55 | argc--; |
| 56 | argv++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 57 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | if (argc > 0 && **argv != '-') { |
| 59 | directory = *argv; |
| 60 | argc--; |
| 61 | argv++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 62 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 63 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | /* Parse any options */ |
| 65 | while (argc > 0 && **argv == '-') { |
| 66 | int stopit = FALSE; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 67 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | while (*++(*argv) && stopit == FALSE) |
| 69 | switch (**argv) { |
| 70 | case 'f': |
| 71 | if (strcmp(*argv, "follow") == 0) { |
| 72 | argc--; |
| 73 | argv++; |
| 74 | dereferenceFlag = TRUE; |
| 75 | } |
| 76 | break; |
| 77 | case 'n': |
| 78 | if (strcmp(*argv, "name") == 0) { |
| 79 | if (argc-- > 1) { |
| 80 | pattern = *(++argv); |
| 81 | stopit = TRUE; |
| 82 | } else { |
| 83 | usage(find_usage); |
| 84 | } |
| 85 | } |
| 86 | break; |
| 87 | case '-': |
| 88 | /* Ignore all long options */ |
| 89 | break; |
| 90 | default: |
| 91 | usage(find_usage); |
| 92 | } |
| 93 | if (argc-- > 1) |
| 94 | argv++; |
| 95 | if (**argv != '-') |
| 96 | break; |
| 97 | else |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | if (recursiveAction(directory, TRUE, FALSE, FALSE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 102 | fileAction, fileAction, NULL) == FALSE) { |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame^] | 103 | return EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame^] | 106 | return EXIT_SUCCESS; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 107 | } |