Added support for the -c (count matches) option. Made it so it works just like
[oweals/busybox.git] / grep.c
1 /*
2  * Mini grep implementation for busybox using libc regex.
3  *
4  * Copyright (C) 1999,2000 by Lineo, inc.
5  * Written by Mark Whitley <markw@lineo.com>, <markw@enol.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include <regex.h>
27 #include <string.h> /* for strerror() */
28 #include <errno.h>
29 #include "internal.h"
30
31 extern int optind; /* in unistd.h */
32 extern int errno;  /* for use with strerror() */
33
34 /* options */
35 static int ignore_case       = 0;
36 static int print_filename    = 0;
37 static int print_line_num    = 0;
38 static int print_count_only  = 0;
39 static int be_quiet          = 0;
40 static int invert_search     = 0;
41 static int suppress_err_msgs = 0;
42
43 /* globals */
44 static regex_t regex; /* storage space for compiled regular expression */
45 static int nmatches = 0; /* keeps track of the number of matches */
46 static char *cur_file = NULL; /* the current file we are reading */
47
48
49 static void print_matched_line(char *line, int linenum)
50 {
51         if (print_filename)
52                 printf("%s:", cur_file);
53         if (print_line_num)
54                 printf("%i:", linenum);
55
56         printf("%s", line);
57 }
58
59 static void grep_file(FILE *file)
60 {
61         char *line = NULL;
62         int ret;
63         int linenum = 0;
64
65         while ((line = get_line_from_file(file)) != NULL) {
66                 linenum++;
67                 ret = regexec(&regex, line, 0, NULL, 0);
68                 if (ret == 0 && !invert_search) { /* match */
69
70                         /* if we found a match but were told to be quiet, stop here and
71                          * return success */
72                         if (be_quiet) {
73                                 regfree(&regex);
74                                 exit(0);
75                         }
76
77                         nmatches++;
78
79                         if (!print_count_only)
80                                 print_matched_line(line, linenum);
81
82                 } else if (ret == REG_NOMATCH && invert_search) {
83
84                         nmatches++;
85                         
86                         if (!print_count_only)
87                                 print_matched_line(line, linenum);
88                 }
89
90                 free(line);
91         }
92
93         /* special-case post processing */
94         if (print_count_only) {
95                 if (print_filename)
96                         printf("%s:", cur_file);
97                 printf("%i\n", nmatches);
98         }
99
100         /* reset number of matches found to zero */
101         nmatches = 0;
102 }
103
104 extern int grep_main(int argc, char **argv)
105 {
106         int opt;
107         int reflags;
108
109         /* do special-case option parsing */
110         if (argv[1] && (strcmp(argv[1], "--help") == 0))
111                 usage(grep_usage);
112
113         /* do normal option parsing */
114         while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
115                 switch (opt) {
116                         case 'i':
117                                 ignore_case++;
118                                 break;
119                         case 'H':
120                                 print_filename++;
121                                 break;
122                         case 'h':
123                                 print_filename--;
124                                 break;
125                         case 'n':
126                                 print_line_num++;
127                                 break;
128                         case 'q':
129                                 be_quiet++;
130                                 break;
131                         case 'v':
132                                 invert_search++;
133                                 break;
134                         case 's':
135                                 suppress_err_msgs++;
136                                 break;
137                         case 'c':
138                                 print_count_only++;
139                                 break;
140                 }
141         }
142
143         /* argv[optind] should be the regex pattern; no pattern, no worky */
144         if (argv[optind] == NULL)
145                 usage(grep_usage);
146
147         /* compile the regular expression
148          * we're not going to mess with sub-expressions, and we need to
149          * treat newlines right. */
150         reflags = REG_NOSUB | REG_NEWLINE; 
151         if (ignore_case)
152                 reflags |= REG_ICASE;
153         xregcomp(&regex, argv[optind], reflags);
154
155         /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
156          * there is more than one file to grep, we will print the filenames */
157         if ((argc-1) - (optind+1) > 0)
158                 print_filename++;
159
160         /* If no files were specified, or '-' was specified, take input from
161          * stdin. Otherwise, we grep through all the files specified. */
162         if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
163                 grep_file(stdin);
164         } else {
165                 int i;
166                 FILE *file;
167                 for (i = optind + 1; i < argc; i++) {
168                         cur_file = argv[i];
169                         file = fopen(cur_file, "r");
170                         if (file == NULL) {
171                                 if (!suppress_err_msgs)
172                                         errorMsg("%s: %s\n", cur_file, strerror(errno));
173                         } else {
174                                 grep_file(file);
175                                 fclose(file);
176                         }
177                 }
178         }
179
180         regfree(&regex);
181
182         if (nmatches == 0)
183                 return 1;
184
185         return 0;
186 }