summaryrefslogtreecommitdiff
path: root/findutils/grep.c
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
committerErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
commite49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /findutils/grep.c
parentc0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff)
downloadbusybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'findutils/grep.c')
-rw-r--r--findutils/grep.c182
1 files changed, 91 insertions, 91 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 287d9f80d..d8d2f1837 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini grep implementation for busybox 3 * Mini grep implementation for busybox
3 * 4 *
@@ -40,126 +41,125 @@
40#include <ctype.h> 41#include <ctype.h>
41 42
42static const char grep_usage[] = 43static const char grep_usage[] =
43"grep [OPTIONS]... PATTERN [FILE]...\n\n" 44 "grep [OPTIONS]... PATTERN [FILE]...\n\n"
44"Search for PATTERN in each FILE or standard input.\n\n" 45 "Search for PATTERN in each FILE or standard input.\n\n"
45"OPTIONS:\n" 46 "OPTIONS:\n"
46"\t-h\tsuppress the prefixing filename on output\n" 47 "\t-h\tsuppress the prefixing filename on output\n"
47"\t-i\tignore case distinctions\n" 48 "\t-i\tignore case distinctions\n"
48"\t-n\tprint line number with output lines\n" 49 "\t-n\tprint line number with output lines\n"
49"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" 50 "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n"
50#if defined BB_REGEXP 51#if defined BB_REGEXP
51"This version of grep matches full regular expresions.\n"; 52 "This version of grep matches full regular expresions.\n";
52#else 53#else
53"This version of grep matches strings (not regular expresions).\n"; 54 "This version of grep matches strings (not regular expresions).\n";
54#endif 55#endif
55 56
56static int match = FALSE, beQuiet = FALSE; 57static int match = FALSE, beQuiet = FALSE;
57 58
58static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine) 59static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
60 int ignoreCase, int tellLine)
59{ 61{
60 char *cp; 62 char *cp;
61 long line = 0; 63 long line = 0;
62 char haystack[BUF_SIZE]; 64 char haystack[BUF_SIZE];
63 65
64 while (fgets (haystack, sizeof (haystack), fp)) { 66 while (fgets(haystack, sizeof(haystack), fp)) {
65 line++; 67 line++;
66 cp = &haystack[strlen (haystack) - 1]; 68 cp = &haystack[strlen(haystack) - 1];
67 69
68 if (*cp != '\n') 70 if (*cp != '\n')
69 fprintf (stderr, "%s: Line too long\n", fileName); 71 fprintf(stderr, "%s: Line too long\n", fileName);
70 72
71 if (find_match(haystack, needle, ignoreCase) == TRUE) { 73 if (find_match(haystack, needle, ignoreCase) == TRUE) {
72 if (tellName==TRUE) 74 if (tellName == TRUE)
73 printf ("%s:", fileName); 75 printf("%s:", fileName);
74 76
75 if (tellLine==TRUE) 77 if (tellLine == TRUE)
76 printf ("%ld:", line); 78 printf("%ld:", line);
77 79
78 if (beQuiet==FALSE) 80 if (beQuiet == FALSE)
79 fputs (haystack, stdout); 81 fputs(haystack, stdout);
80 82
81 match = TRUE; 83 match = TRUE;
84 }
82 } 85 }
83 }
84} 86}
85 87
86 88
87extern int grep_main (int argc, char **argv) 89extern int grep_main(int argc, char **argv)
88{ 90{
89 FILE *fp; 91 FILE *fp;
90 char *cp; 92 char *cp;
91 char *needle; 93 char *needle;
92 char *fileName; 94 char *fileName;
93 int tellName=TRUE; 95 int tellName = TRUE;
94 int ignoreCase=TRUE; 96 int ignoreCase = TRUE;
95 int tellLine=FALSE; 97 int tellLine = FALSE;
96 98
97 99
98 ignoreCase = FALSE; 100 ignoreCase = FALSE;
99 tellLine = FALSE; 101 tellLine = FALSE;
100 102
101 argc--;
102 argv++;
103 if (argc < 1) {
104 usage(grep_usage);
105 }
106
107 if (**argv == '-') {
108 argc--; 103 argc--;
109 cp = *argv++; 104 argv++;
105 if (argc < 1) {
106 usage(grep_usage);
107 }
110 108
111 while (*++cp) 109 if (**argv == '-') {
112 switch (*cp) { 110 argc--;
113 case 'i': 111 cp = *argv++;
114 ignoreCase = TRUE;
115 break;
116 112
117 case 'h': 113 while (*++cp)
118 tellName = FALSE; 114 switch (*cp) {
119 break; 115 case 'i':
116 ignoreCase = TRUE;
117 break;
120 118
121 case 'n': 119 case 'h':
122 tellLine = TRUE; 120 tellName = FALSE;
123 break; 121 break;
124 122
125 case 'q': 123 case 'n':
126 beQuiet = TRUE; 124 tellLine = TRUE;
127 break; 125 break;
128 126
129 default: 127 case 'q':
130 usage(grep_usage); 128 beQuiet = TRUE;
131 } 129 break;
132 } 130
133 131 default:
134 needle = *argv++; 132 usage(grep_usage);
135 argc--; 133 }
136
137 if (argc==0) {
138 do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine);
139 } else {
140 /* Never print the filename for just one file */
141 if (argc==1)
142 tellName=FALSE;
143 while (argc-- > 0) {
144 fileName = *argv++;
145
146 fp = fopen (fileName, "r");
147 if (fp == NULL) {
148 perror (fileName);
149 continue;
150 }
151
152 do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine);
153
154 if (ferror (fp))
155 perror (fileName);
156 fclose (fp);
157 } 134 }
158 }
159 exit(match);
160}
161 135
136 needle = *argv++;
137 argc--;
162 138
163/* END CODE */ 139 if (argc == 0) {
140 do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine);
141 } else {
142 /* Never print the filename for just one file */
143 if (argc == 1)
144 tellName = FALSE;
145 while (argc-- > 0) {
146 fileName = *argv++;
147
148 fp = fopen(fileName, "r");
149 if (fp == NULL) {
150 perror(fileName);
151 continue;
152 }
153
154 do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine);
155
156 if (ferror(fp))
157 perror(fileName);
158 fclose(fp);
159 }
160 }
161 exit(match);
162}
164 163
165 164
165/* END CODE */