Added regexp support, fixed Changelog.
This commit is contained in:
@ -21,14 +21,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "internal.h"
|
||||
#include "regexp.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static char* pattern=NULL;
|
||||
static char* directory=NULL;
|
||||
static char* directory=".";
|
||||
static int dereferenceFlag=FALSE;
|
||||
|
||||
static const char find_usage[] = "find [path...] [expression]\n"
|
||||
@ -41,7 +42,7 @@ static int fileAction(const char *fileName, struct stat* statbuf)
|
||||
{
|
||||
if (pattern==NULL)
|
||||
fprintf(stdout, "%s\n", fileName);
|
||||
else if (match(fileName, pattern) == TRUE)
|
||||
else if (find_match(fileName, pattern, TRUE) == TRUE)
|
||||
fprintf(stdout, "%s\n", fileName);
|
||||
return( TRUE);
|
||||
}
|
||||
@ -53,7 +54,7 @@ static int dirAction(const char *fileName, struct stat* statbuf)
|
||||
|
||||
if (pattern==NULL)
|
||||
fprintf(stdout, "%s\n", fileName);
|
||||
else if (match(fileName, pattern) == TRUE)
|
||||
else if (find_match(fileName, pattern, TRUE) == TRUE)
|
||||
fprintf(stdout, "%s\n", fileName);
|
||||
|
||||
dir = opendir( fileName);
|
||||
@ -71,22 +72,18 @@ static int dirAction(const char *fileName, struct stat* statbuf)
|
||||
|
||||
int find_main(int argc, char **argv)
|
||||
{
|
||||
if (argc <= 1) {
|
||||
dirAction( ".", NULL);
|
||||
}
|
||||
|
||||
/* peel off the "find" */
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (**argv != '-') {
|
||||
if ( argc > 0 && **argv != '-') {
|
||||
directory=*argv;
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
/* Parse any options */
|
||||
while (**argv == '-') {
|
||||
while (argc > 0 && **argv == '-') {
|
||||
int stopit=FALSE;
|
||||
while (*++(*argv) && stopit==FALSE) switch (**argv) {
|
||||
case 'f':
|
||||
@ -120,6 +117,10 @@ int find_main(int argc, char **argv)
|
||||
break;
|
||||
}
|
||||
|
||||
dirAction( directory, NULL);
|
||||
if (recursiveAction(directory, TRUE, FALSE, FALSE,
|
||||
fileAction, fileAction) == FALSE) {
|
||||
exit( FALSE);
|
||||
}
|
||||
|
||||
exit(TRUE);
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
*/
|
||||
|
||||
#include "internal.h"
|
||||
#include "regexp.h"
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
@ -30,44 +31,17 @@
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
static const char grep_usage[] =
|
||||
"grep [-ihn]... PATTERN [FILE]...\n"
|
||||
"Search for PATTERN in each FILE or standard input.\n\n"
|
||||
"\t-h\tsuppress the prefixing filename on output\n"
|
||||
"\t-i\tignore case distinctions\n"
|
||||
"\t-n\tprint line number with output lines\n\n"
|
||||
#if defined BB_REGEXP
|
||||
"This version of grep matches full regexps.\n";
|
||||
#else
|
||||
"This version of grep matches strings (not full regexps).\n";
|
||||
|
||||
|
||||
/*
|
||||
* See if the specified needle is found in the specified haystack.
|
||||
*/
|
||||
static int search (const char *haystack, const char *needle, int ignoreCase)
|
||||
{
|
||||
|
||||
if (ignoreCase == FALSE) {
|
||||
haystack = strstr (haystack, needle);
|
||||
if (haystack == NULL)
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
} else {
|
||||
int i;
|
||||
char needle1[BUF_SIZE];
|
||||
char haystack1[BUF_SIZE];
|
||||
|
||||
strncpy( haystack1, haystack, sizeof(haystack1));
|
||||
strncpy( needle1, needle, sizeof(needle1));
|
||||
for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
|
||||
haystack1[i]=tolower( haystack1[i]);
|
||||
for( i=0; i<sizeof(needle1) && needle1[i]; i++)
|
||||
needle1[i]=tolower( needle1[i]);
|
||||
haystack = strstr (haystack1, needle1);
|
||||
if (haystack == NULL)
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
extern int grep_main (int argc, char **argv)
|
||||
@ -80,7 +54,7 @@ extern int grep_main (int argc, char **argv)
|
||||
int ignoreCase=FALSE;
|
||||
int tellLine=FALSE;
|
||||
long line;
|
||||
char buf[BUF_SIZE];
|
||||
char haystack[BUF_SIZE];
|
||||
|
||||
ignoreCase = FALSE;
|
||||
tellLine = FALSE;
|
||||
@ -128,21 +102,21 @@ extern int grep_main (int argc, char **argv)
|
||||
|
||||
line = 0;
|
||||
|
||||
while (fgets (buf, sizeof (buf), fp)) {
|
||||
while (fgets (haystack, sizeof (haystack), fp)) {
|
||||
line++;
|
||||
cp = &buf[strlen (buf) - 1];
|
||||
cp = &haystack[strlen (haystack) - 1];
|
||||
|
||||
if (*cp != '\n')
|
||||
fprintf (stderr, "%s: Line too long\n", name);
|
||||
|
||||
if (search (buf, needle, ignoreCase)==TRUE) {
|
||||
if (find_match(haystack, needle, ignoreCase) == TRUE) {
|
||||
if (tellName==TRUE)
|
||||
printf ("%s: ", name);
|
||||
|
||||
if (tellLine==TRUE)
|
||||
printf ("%ld: ", line);
|
||||
|
||||
fputs (buf, stdout);
|
||||
fputs (haystack, stdout);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user