implemented numeric sort (sort -g)
[oweals/busybox.git] / grep.c
diff --git a/grep.c b/grep.c
index 8dcff0586f4de0b6e3b3b00ee38022a2d4755aa7..a0457df9160fd7f3bfb74d02788ddfd908decb40 100644 (file)
--- a/grep.c
+++ b/grep.c
  *
  */
 
+/*
+       18-Dec-1999     Konstantin Boldyshev <konst@voshod.com>
+
+       + -q option (be quiet) 
+       + exit code depending on grep result (TRUE or FALSE)
+         (useful for scripts)
+*/
+
 #include "internal.h"
 #include "regexp.h"
 #include <stdio.h>
 #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"
+"\t-n\tprint line number with output lines\n"
+"\t-q\tbe quiet\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 regexps).\n";
+"This version of grep matches strings (not regular expresions).\n";
 #endif
 
+static int match = FALSE, beQuiet = FALSE;
 
 static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine)
 {
@@ -64,7 +75,10 @@ static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ig
            if (tellLine==TRUE)
                printf ("%ld:", line);
 
-           fputs (haystack, stdout);
+           if (beQuiet==FALSE)
+               fputs (haystack, stdout);
+
+           match = TRUE;
        }
     }
 }
@@ -108,6 +122,10 @@ extern int grep_main (int argc, char **argv)
                tellLine = TRUE;
                break;
 
+           case 'q':
+               beQuiet = TRUE;
+               break;
+
            default:
                usage(grep_usage);
            }
@@ -135,7 +153,7 @@ extern int grep_main (int argc, char **argv)
            fclose (fp);
        }
     }
-    exit( TRUE);
+    exit(match);
 }