51b8707daa295f29862feabd44bc7d6086731ef6
[oweals/busybox.git] / findutils / find.c
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;
30 static int dereferenceFlag=FALSE;
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
38 static int fileAction(const char *fileName, struct stat* statbuf)
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
47 static int dirAction(const char *fileName, struct stat* statbuf)
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);
65         recursiveAction( dirName, TRUE, dereferenceFlag, FALSE, fileAction, dirAction);
66     }
67     return( TRUE);
68 }
69
70 int find_main(int argc, char **argv)
71 {
72     if (argc <= 1) {
73         dirAction( ".", NULL); 
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 {
103                         usage (find_usage);
104                     }
105                 }
106                 break;
107             case '-':
108                 /* Ignore all long options */
109                 break;
110             default:
111                 usage (find_usage);
112         }
113         if (argc-- > 1)
114             argv++;
115             if (**argv != '-')
116                 break;
117         else
118             break;
119     }
120
121     dirAction( directory, NULL); 
122     exit(TRUE);
123 }