blob: 51b8707daa295f29862feabd44bc7d6086731ef6 [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;
Eric Andersenf811e071999-10-09 00:25:00 +000030static int dereferenceFlag=FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +000031
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
Eric Andersen9b587181999-10-17 05:43:39 +000038static int fileAction(const char *fileName, struct stat* statbuf)
Eric Andersen17d49ef1999-10-06 20:25:32 +000039{
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 Andersen9b587181999-10-17 05:43:39 +000047static int dirAction(const char *fileName, struct stat* statbuf)
Eric Andersen17d49ef1999-10-06 20:25:32 +000048{
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 Andersenbed30e91999-10-18 19:02:32 +000065 recursiveAction( dirName, TRUE, dereferenceFlag, FALSE, fileAction, dirAction);
Eric Andersen17d49ef1999-10-06 20:25:32 +000066 }
67 return( TRUE);
68}
69
70int find_main(int argc, char **argv)
71{
72 if (argc <= 1) {
Eric Andersen9b587181999-10-17 05:43:39 +000073 dirAction( ".", NULL);
Eric Andersen17d49ef1999-10-06 20:25:32 +000074 }
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 Andersenb0e9a701999-10-18 22:28:26 +0000103 usage (find_usage);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000104 }
105 }
106 break;
107 case '-':
108 /* Ignore all long options */
109 break;
110 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000111 usage (find_usage);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000112 }
113 if (argc-- > 1)
114 argv++;
115 if (**argv != '-')
116 break;
117 else
118 break;
119 }
120
Eric Andersen9b587181999-10-17 05:43:39 +0000121 dirAction( directory, NULL);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000122 exit(TRUE);
123}