+ minor typo fix
[oweals/busybox.git] / grep.c
diff --git a/grep.c b/grep.c
index bb1a1462242617667028c0945e00b53c003d9b05..e29606405c1a50c768637b59097ad072117803ec 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -39,6 +39,9 @@
 #include <signal.h>
 #include <time.h>
 #include <ctype.h>
+#define BB_DECLARE_EXTERN
+#define bb_need_too_few_args
+#include "messages.c"
 
 static const char grep_usage[] =
        "grep [OPTIONS]... PATTERN [FILE]...\n"
@@ -63,15 +66,12 @@ static int match = FALSE, beQuiet = FALSE;
 static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
                                        int ignoreCase, int tellLine, int invertSearch)
 {
-       char *cp;
        long line = 0;
        char *haystack;
        int  truth = !invertSearch;
 
        while ((haystack = cstring_lineFromFile(fp))) {
                line++;
-               cp = &haystack[strlen(haystack) - 1];
-
                if (find_match(haystack, needle, ignoreCase) == truth) {
                        if (tellName == TRUE)
                                printf("%s:", fileName);
@@ -92,7 +92,6 @@ static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
 extern int grep_main(int argc, char **argv)
 {
        FILE *fp;
-       char *cp;
        char *needle;
        char *fileName;
        int tellName     = TRUE;
@@ -100,18 +99,14 @@ extern int grep_main(int argc, char **argv)
        int tellLine     = FALSE;
        int invertSearch = FALSE;
 
-       argc--;
-       argv++;
        if (argc < 1) {
                usage(grep_usage);
        }
+       argv++;
 
-       if (**argv == '-') {
-               argc--;
-               cp = *argv++;
-
-               while (*++cp)
-                       switch (*cp) {
+       while (--argc >= 0 && *argv && (**argv == '-')) {
+               while (*++(*argv)) {
+                       switch (**argv) {
                        case 'i':
                                ignoreCase = TRUE;
                                break;
@@ -135,6 +130,12 @@ extern int grep_main(int argc, char **argv)
                        default:
                                usage(grep_usage);
                        }
+               }
+               argv++;
+       }
+
+       if (argc == 0 || *argv == NULL) {
+               fatalError(too_few_args, "grep");
        }
 
        needle = *argv++;