Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini find implementation for busybox |
| 3 | * |
| 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <dirent.h> |
| 25 | #include "internal.h" |
| 26 | |
| 27 | |
| 28 | static char* pattern=NULL; |
| 29 | static char* directory=NULL; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 30 | static int dereferenceFlag=FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 31 | |
| 32 | static const char find_usage[] = "find [path...] [expression]\n" |
| 33 | "default path is the current directory; default expression is -print\n" |
| 34 | "expression may consist of:\n"; |
| 35 | |
| 36 | |
| 37 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 38 | static int fileAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 39 | { |
| 40 | if (pattern==NULL) |
| 41 | fprintf(stdout, "%s\n", fileName); |
| 42 | else if (match(fileName, pattern) == TRUE) |
| 43 | fprintf(stdout, "%s\n", fileName); |
| 44 | return( TRUE); |
| 45 | } |
| 46 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 47 | static int dirAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 48 | { |
| 49 | DIR *dir; |
| 50 | struct dirent *entry; |
| 51 | |
| 52 | if (pattern==NULL) |
| 53 | fprintf(stdout, "%s\n", fileName); |
| 54 | else if (match(fileName, pattern) == TRUE) |
| 55 | fprintf(stdout, "%s\n", fileName); |
| 56 | |
| 57 | dir = opendir( fileName); |
| 58 | if (!dir) { |
| 59 | perror("Can't open directory"); |
| 60 | exit(FALSE); |
| 61 | } |
| 62 | while ((entry = readdir(dir)) != NULL) { |
| 63 | char dirName[NAME_MAX]; |
| 64 | sprintf(dirName, "%s/%s", fileName, entry->d_name); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 65 | recursiveAction( dirName, TRUE, dereferenceFlag, FALSE, fileAction, dirAction); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 66 | } |
| 67 | return( TRUE); |
| 68 | } |
| 69 | |
| 70 | int find_main(int argc, char **argv) |
| 71 | { |
| 72 | if (argc <= 1) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 73 | dirAction( ".", NULL); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* peel off the "find" */ |
| 77 | argc--; |
| 78 | argv++; |
| 79 | |
| 80 | if (**argv != '-') { |
| 81 | directory=*argv; |
| 82 | argc--; |
| 83 | argv++; |
| 84 | } |
| 85 | |
| 86 | /* Parse any options */ |
| 87 | while (**argv == '-') { |
| 88 | int stopit=FALSE; |
| 89 | while (*++(*argv) && stopit==FALSE) switch (**argv) { |
| 90 | case 'f': |
| 91 | if (strcmp(*argv, "follow")==0) { |
| 92 | argc--; |
| 93 | argv++; |
| 94 | dereferenceFlag=TRUE; |
| 95 | } |
| 96 | break; |
| 97 | case 'n': |
| 98 | if (strcmp(*argv, "name")==0) { |
| 99 | if (argc-- > 1) { |
| 100 | pattern=*(++argv); |
| 101 | stopit=-TRUE; |
| 102 | } else { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame^] | 103 | usage (find_usage); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | break; |
| 107 | case '-': |
| 108 | /* Ignore all long options */ |
| 109 | break; |
| 110 | default: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame^] | 111 | usage (find_usage); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 112 | } |
| 113 | if (argc-- > 1) |
| 114 | argv++; |
| 115 | if (**argv != '-') |
| 116 | break; |
| 117 | else |
| 118 | break; |
| 119 | } |
| 120 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 121 | dirAction( directory, NULL); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 122 | exit(TRUE); |
| 123 | } |