made note of my recent changes
[oweals/busybox.git] / grep.c
diff --git a/grep.c b/grep.c
index 50a29617845259b7b9e7523d3dbf9a1328f621f3..84bb99667373720d1fcbb0108a89988d691e2ca7 100644 (file)
--- a/grep.c
+++ b/grep.c
 #include <ctype.h>
 
 static const char grep_usage[] =
-"grep [-ihn]... PATTERN [FILE]...\n"
+"grep [OPTIONS]... PATTERN [FILE]...\n\n"
 "Search for PATTERN in each FILE or standard input.\n\n"
+"OPTIONS:\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";
+"This version of grep matches full regular expresions.\n";
 #else
-"This version of grep matches strings (not full regexps).\n";
+"This version of grep matches strings (not regular expresions).\n";
 #endif
 
-int tellName=TRUE;
-int ignoreCase=FALSE;
-int tellLine=FALSE;
 
-static do_grep(char* needle, char* haystack )
+static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine)
 {
-       line = 0;
+    char *cp;
+    long line = 0;
+    char haystack[BUF_SIZE];
 
-       while (fgets (haystack, sizeof (haystack), fp)) {
-           line++;
-           cp = &haystack[strlen (haystack) - 1];
+    while (fgets (haystack, sizeof (haystack), fp)) {
+       line++;
+       cp = &haystack[strlen (haystack) - 1];
 
-           if (*cp != '\n')
-               fprintf (stderr, "%s: Line too long\n", name);
+       if (*cp != '\n')
+           fprintf (stderr, "%s: Line too long\n", fileName);
 
-           if (find_match(haystack, needle, ignoreCase) == TRUE) {
-               if (tellName==TRUE)
-                   printf ("%s: ", name);
+       if (find_match(haystack, needle, ignoreCase) == TRUE) {
+           if (tellName==TRUE)
+               printf ("%s:", fileName);
 
-               if (tellLine==TRUE)
-                   printf ("%ld: ", line);
+           if (tellLine==TRUE)
+               printf ("%ld:", line);
 
-               fputs (haystack, stdout);
-           }
+           fputs (haystack, stdout);
        }
+    }
 }
 
 
 extern int grep_main (int argc, char **argv)
 {
     FILE *fp;
-    char *needle;
-    char *name;
     char *cp;
-    long line;
-    char haystack[BUF_SIZE];
+    char *needle;
+    char *fileName;
+    int tellName=FALSE;
+    int ignoreCase=FALSE;
+    int tellLine=FALSE;
+
 
     ignoreCase = FALSE;
     tellLine = FALSE;
@@ -100,7 +102,7 @@ extern int grep_main (int argc, char **argv)
                break;
 
            case 'h':
-               tellName = FALSE;
+               tellName = TRUE;
                break;
 
            case 'n':
@@ -115,28 +117,24 @@ extern int grep_main (int argc, char **argv)
     needle = *argv++;
     argc--;
 
+    if (argc==0) {
+       do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine);
+    } else {
+       while (argc-- > 0) {
+           fileName = *argv++;
 
-    while (argc-- > 0) {
-
-       if (argc==0) {
-           file = stdin;
-       }
-       else
-           file = fopen(*argv, "r");
-
+           fp = fopen (fileName, "r");
+           if (fp == NULL) {
+               perror (fileName);
+               continue;
+           }
 
-       name = *argv++;
+           do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine);
 
-       fp = fopen (name, "r");
-       if (fp == NULL) {
-           perror (name);
-           continue;
+           if (ferror (fp))
+               perror (fileName);
+           fclose (fp);
        }
-
-       if (ferror (fp))
-           perror (name);
-
-       fclose (fp);
     }
     exit( TRUE);
 }