Another update from Larry:
[oweals/busybox.git] / grep.c
diff --git a/grep.c b/grep.c
index 8c67976470682f7e61b027d657f4d8259faeeafc..12bcead7a1c5ba828e6ff13c2bab4e752d1369ed 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -41,6 +41,7 @@ static int print_count_only  = 0;
 static int be_quiet          = 0;
 static int invert_search     = 0;
 static int suppress_err_msgs = 0;
+static int files_that_match  = 0;
 
 #ifdef BB_FEATURE_GREP_CONTEXT
 extern char *optarg; /* in getopt.h */
@@ -105,14 +106,13 @@ static void grep_file(FILE *file)
 
                        /* otherwise, keep track of matches and print the matched line */
                        nmatches++;
-                       if (!print_count_only) {
+                       if (print_count_only==0 && files_that_match==0) {
 #ifdef BB_FEATURE_GREP_CONTEXT
                                int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
 
                                /* if we were told to print 'before' lines and there is at least
                                 * one line in the circular buffer, print them */
-                               if (lines_before && before_buf[prevpos] != NULL)
-                               {
+                               if (lines_before && before_buf[prevpos] != NULL) {
                                        int first_buf_entry_line_num = linenum - lines_before;
 
                                        /* advance to the first entry in the circular buffer, and
@@ -132,7 +132,6 @@ static void grep_file(FILE *file)
                                                idx = (idx + 1) % lines_before;
                                                first_buf_entry_line_num++;
                                        }
-
                                }
 
                                /* make a note that we need to print 'after' lines */
@@ -144,8 +143,7 @@ static void grep_file(FILE *file)
 #ifdef BB_FEATURE_GREP_CONTEXT
                else { /* no match */
                        /* Add the line to the circular 'before' buffer */
-                       if(lines_before)
-                       {
+                       if(lines_before) {
                                if(before_buf[curpos])
                                        free(before_buf[curpos]);
                                before_buf[curpos] = strdup(line);
@@ -163,11 +161,18 @@ static void grep_file(FILE *file)
        }
 
        /* special-case post processing */
-       if (print_count_only) {
+       if (files_that_match) {
+           if (nmatches > 0) {
+               printf("%s", cur_file);
+               if (nmatches)
+                   printf(":%d", nmatches);
+               printf("\n");
+           }
+       } else if (print_count_only) {
                if (print_filename)
                        printf("%s:", cur_file);
                printf("%i\n", nmatches);
-       }
+       } 
 
        /* remember if we matched */
        if (nmatches != 0)
@@ -183,7 +188,7 @@ extern int grep_main(int argc, char **argv)
 #endif
 
        /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "iHhnqvsc"
+       while ((opt = getopt(argc, argv, "iHhlnqvsc"
 #ifdef BB_FEATURE_GREP_CONTEXT
 "A:B:C:"
 #endif
@@ -192,6 +197,9 @@ extern int grep_main(int argc, char **argv)
                        case 'i':
                                ignore_case++;
                                break;
+                       case 'l':
+                               files_that_match++;
+                               break;
                        case 'H':
                                print_filename++;
                                break;
@@ -242,7 +250,7 @@ extern int grep_main(int argc, char **argv)
                show_usage();
 
        /* sanity check */
-       if (print_count_only || be_quiet) {
+       if (print_count_only || be_quiet || files_that_match) {
                print_line_num = 0;
 #ifdef BB_FEATURE_GREP_CONTEXT
                lines_before = 0;