summaryrefslogtreecommitdiff
path: root/findutils/grep.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-07 08:30:23 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-07 08:30:23 +0000
commit596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe (patch)
tree4d48d109e66c5197bb27c8215062aab28d385509 /findutils/grep.c
parent5c3199e0a519695c367b773e179b5458670f452b (diff)
downloadbusybox-w32-596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe.tar.gz
busybox-w32-596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe.tar.bz2
busybox-w32-596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe.zip
more stuff
Diffstat (limited to 'findutils/grep.c')
-rw-r--r--findutils/grep.c251
1 files changed, 119 insertions, 132 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index a07feda03..11198b851 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -23,187 +23,174 @@
23 23
24 24
25const char grep_usage[] = 25const char grep_usage[] =
26"Search the input file(s) for lines matching the given pattern.\n" 26 "Search the input file(s) for lines matching the given pattern.\n"
27"\tI search stdin if no files are given.\n" 27 "\tI search stdin if no files are given.\n"
28"\tI can't grok full regular expressions.\n" 28 "\tI can't grok full regular expressions.\n"
29"usage: grep [in] PATTERN [FILES]...\n" 29 "usage: grep [in] PATTERN [FILES]...\n"
30"\ti=ignore case, n=list line numbers\n"; 30 "\ti=ignore case, n=list line numbers\n";
31 31
32 32
33 33
34static BOOL search 34static BOOL search (const char *string, const char *word, BOOL ignoreCase);
35 (const char * string, const char * word, BOOL ignoreCase);
36 35
37 36
38extern int 37extern int grep_main (int argc, char **argv)
39grep_main(int argc, char ** argv)
40{ 38{
41 FILE * fp; 39 FILE *fp;
42 const char * word; 40 const char *word;
43 const char * name; 41 const char *name;
44 const char * cp; 42 const char *cp;
45 BOOL tellName; 43 BOOL tellName;
46 BOOL ignoreCase; 44 BOOL ignoreCase;
47 BOOL tellLine; 45 BOOL tellLine;
48 long line; 46 long line;
49 char buf[BUF_SIZE]; 47 char buf[BUF_SIZE];
50 48
51 ignoreCase = FALSE; 49 ignoreCase = FALSE;
52 tellLine = FALSE; 50 tellLine = FALSE;
53 51
52 argc--;
53 argv++;
54 if (argc < 1) {
55 fprintf (stderr, "%s", grep_usage);
56 return 1;
57 }
58
59 if (**argv == '-') {
54 argc--; 60 argc--;
55 argv++; 61 cp = *argv++;
56 if (argc < 1)
57 {
58 fprintf(stderr, "%s", grep_usage);
59 return 1;
60 }
61 62
62 if (**argv == '-') 63 while (*++cp)
63 { 64 switch (*cp) {
64 argc--; 65 case 'i':
65 cp = *argv++; 66 ignoreCase = TRUE;
66 67 break;
67 while (*++cp) switch (*cp)
68 {
69 case 'i':
70 ignoreCase = TRUE;
71 break;
72
73 case 'n':
74 tellLine = TRUE;
75 break;
76
77 default:
78 fprintf(stderr, "Unknown option\n");
79 return 1;
80 }
81 }
82 68
83 word = *argv++; 69 case 'n':
84 argc--; 70 tellLine = TRUE;
71 break;
85 72
86 tellName = (argc > 1); 73 default:
74 fprintf (stderr, "Unknown option\n");
75 return 1;
76 }
77 }
87 78
88 while (argc-- > 0) 79 word = *argv++;
89 { 80 argc--;
90 name = *argv++;
91 81
92 fp = fopen(name, "r"); 82 tellName = (argc > 1);
93 83
94 if (fp == NULL) 84 while (argc-- > 0) {
95 { 85 name = *argv++;
96 perror(name);
97 86
98 continue; 87 fp = fopen (name, "r");
99 }
100 88
101 line = 0; 89 if (fp == NULL) {
90 perror (name);
102 91
103 while (fgets(buf, sizeof(buf), fp)) 92 continue;
104 { 93 }
105 line++;
106 94
107 cp = &buf[strlen(buf) - 1]; 95 line = 0;
108 96
109 if (*cp != '\n') 97 while (fgets (buf, sizeof (buf), fp)) {
110 fprintf(stderr, "%s: Line too long\n", name); 98 line++;
111 99
112 if (search(buf, word, ignoreCase)) 100 cp = &buf[strlen (buf) - 1];
113 {
114 if (tellName)
115 printf("%s: ", name);
116 101
117 if (tellLine) 102 if (*cp != '\n')
118 printf("%ld: ", line); 103 fprintf (stderr, "%s: Line too long\n", name);
119 104
120 fputs(buf, stdout); 105 if (search (buf, word, ignoreCase)) {
121 } 106 if (tellName)
122 } 107 printf ("%s: ", name);
123 108
124 if (ferror(fp)) 109 if (tellLine)
125 perror(name); 110 printf ("%ld: ", line);
126 111
127 fclose(fp); 112 fputs (buf, stdout);
113 }
128 } 114 }
129 return 0; 115
116 if (ferror (fp))
117 perror (name);
118
119 fclose (fp);
120 }
121 return 0;
130} 122}
131 123
132 124
133/* 125/*
134 * See if the specified word is found in the specified string. 126 * See if the specified word is found in the specified string.
135 */ 127 */
136static BOOL 128static BOOL search (const char *string, const char *word, BOOL ignoreCase)
137search(const char * string, const char * word, BOOL ignoreCase)
138{ 129{
139 const char * cp1; 130 const char *cp1;
140 const char * cp2; 131 const char *cp2;
141 int len; 132 int len;
142 int lowFirst; 133 int lowFirst;
143 int ch1; 134 int ch1;
144 int ch2; 135 int ch2;
145 136
146 len = strlen(word); 137 len = strlen (word);
147 138
148 if (!ignoreCase) 139 if (!ignoreCase) {
149 { 140 while (TRUE) {
150 while (TRUE) 141 string = strchr (string, word[0]);
151 {
152 string = strchr(string, word[0]);
153 142
154 if (string == NULL) 143 if (string == NULL)
155 return FALSE; 144 return FALSE;
156 145
157 if (memcmp(string, word, len) == 0) 146 if (memcmp (string, word, len) == 0)
158 return TRUE; 147 return TRUE;
159 148
160 string++; 149 string++;
161 }
162 } 150 }
151 }
163 152
164 /* 153 /*
165 * Here if we need to check case independence. 154 * Here if we need to check case independence.
166 * Do the search by lower casing both strings. 155 * Do the search by lower casing both strings.
167 */ 156 */
168 lowFirst = *word; 157 lowFirst = *word;
169 158
170 if (isupper(lowFirst)) 159 if (isupper (lowFirst))
171 lowFirst = tolower(lowFirst); 160 lowFirst = tolower (lowFirst);
172 161
173 while (TRUE) 162 while (TRUE) {
174 { 163 while (*string && (*string != lowFirst) &&
175 while (*string && (*string != lowFirst) && 164 (!isupper (*string) || (tolower (*string) != lowFirst))) {
176 (!isupper(*string) || (tolower(*string) != lowFirst))) 165 string++;
177 { 166 }
178 string++;
179 }
180
181 if (*string == '\0')
182 return FALSE;
183 167
184 cp1 = string; 168 if (*string == '\0')
185 cp2 = word; 169 return FALSE;
186 170
187 do 171 cp1 = string;
188 { 172 cp2 = word;
189 if (*cp2 == '\0')
190 return TRUE;
191 173
192 ch1 = *cp1++; 174 do {
175 if (*cp2 == '\0')
176 return TRUE;
193 177
194 if (isupper(ch1)) 178 ch1 = *cp1++;
195 ch1 = tolower(ch1);
196 179
197 ch2 = *cp2++; 180 if (isupper (ch1))
181 ch1 = tolower (ch1);
198 182
199 if (isupper(ch2)) 183 ch2 = *cp2++;
200 ch2 = tolower(ch2);
201 184
202 } 185 if (isupper (ch2))
203 while (ch1 == ch2); 186 ch2 = tolower (ch2);
204 187
205 string++;
206 } 188 }
189 while (ch1 == ch2);
190
191 string++;
192 }
193 return (TRUE);
207} 194}
208 195
209#endif 196#endif