blob: a42f9e979bfed6c2a844162ab05791e3428463c6 [file] [log] [blame]
Eric Andersen17d49ef1999-10-06 20:25:32 +00001/*
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
28static char* pattern=NULL;
29static char* directory=NULL;
30int dereferenceFlag=FALSE;
31
32static 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
38static int fileAction(const char *fileName)
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
47static int dirAction(const char *fileName)
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, fileAction, dirAction);
66 }
67 return( TRUE);
68}
69
70int find_main(int argc, char **argv)
71{
72 if (argc <= 1) {
73 dirAction( ".");
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 fprintf(stderr, "Usage: %s\n", find_usage);
104 exit( FALSE);
105 }
106 }
107 break;
108 case '-':
109 /* Ignore all long options */
110 break;
111 default:
112 fprintf(stderr, "Usage: %s\n", find_usage);
113 exit( FALSE);
114 }
115 if (argc-- > 1)
116 argv++;
117 if (**argv != '-')
118 break;
119 else
120 break;
121 }
122
123 dirAction( directory);
124 exit(TRUE);
125}
126
127
128
129#ifdef foobar
130
Eric Andersencc8ed391999-10-05 16:24:54 +0000131#include "internal.h"
132#include <errno.h>
133#include <stdio.h>
134
135const char find_usage[] = "find dir [pattern]\n"
136"\n"
137"\tFind files.\n";
138
139extern int
140find_main(struct FileInfo * i, int argc, char * * argv)
141{
142 i->recursive=1;
143 i->processDirectoriesAfterTheirContents=1;
144 return monadic_main(i, argc, argv);
145}
146
147extern int
148find_fn(const struct FileInfo * i)
149{
150 printf("%s\n",i->source);
151
152 return(0);
153}
Eric Andersen17d49ef1999-10-06 20:25:32 +0000154
155
156#endif