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