2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-07 01:55:32 +05:30
|
|
|
/*
|
|
|
|
* Mini find implementation for busybox
|
|
|
|
*
|
1999-10-21 03:38:37 +05:30
|
|
|
*
|
2000-04-13 06:48:56 +05:30
|
|
|
* Copyright (C) 1999,2000 by Lineo, inc.
|
1999-10-21 03:38:37 +05:30
|
|
|
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
|
1999-10-07 01:55:32 +05:30
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2000-09-26 03:15:58 +05:30
|
|
|
#include "busybox.h"
|
1999-10-07 01:55:32 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
static char *pattern = NULL;
|
|
|
|
static char *directory = ".";
|
|
|
|
static int dereferenceFlag = FALSE;
|
1999-10-07 01:55:32 +05:30
|
|
|
|
2000-03-28 06:28:14 +05:30
|
|
|
static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
|
1999-10-07 01:55:32 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
if (pattern == NULL)
|
|
|
|
fprintf(stdout, "%s\n", fileName);
|
|
|
|
else {
|
|
|
|
char *tmp = strrchr(fileName, '/');
|
|
|
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
tmp = (char *) fileName;
|
|
|
|
else
|
|
|
|
tmp++;
|
|
|
|
if (check_wildcard_match(tmp, pattern) == TRUE)
|
|
|
|
fprintf(stdout, "%s\n", fileName);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1999-10-07 01:55:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int find_main(int argc, char **argv)
|
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
/* peel off the "find" */
|
1999-10-07 01:55:32 +05:30
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if (argc > 0 && **argv != '-') {
|
|
|
|
directory = *argv;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
1999-10-07 01:55:32 +05:30
|
|
|
}
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
/* Parse any options */
|
|
|
|
while (argc > 0 && **argv == '-') {
|
|
|
|
int stopit = FALSE;
|
|
|
|
|
|
|
|
while (*++(*argv) && stopit == FALSE)
|
|
|
|
switch (**argv) {
|
|
|
|
case 'f':
|
|
|
|
if (strcmp(*argv, "follow") == 0) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
dereferenceFlag = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
if (strcmp(*argv, "name") == 0) {
|
|
|
|
if (argc-- > 1) {
|
|
|
|
pattern = *(++argv);
|
|
|
|
stopit = TRUE;
|
|
|
|
} else {
|
|
|
|
usage(find_usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
/* Ignore all long options */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(find_usage);
|
|
|
|
}
|
|
|
|
if (argc-- > 1)
|
|
|
|
argv++;
|
|
|
|
if (**argv != '-')
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-12-08 01:26:48 +05:30
|
|
|
if (recursive_action(directory, TRUE, FALSE, FALSE,
|
2000-03-28 06:28:14 +05:30
|
|
|
fileAction, fileAction, NULL) == FALSE) {
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_FAILURE;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
1999-10-22 10:00:20 +05:30
|
|
|
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
1999-10-07 01:55:32 +05:30
|
|
|
}
|