56 #include <limits.h>
57 #include <sys/param.h>
58
59 static const char *errstr[] = {
60 "Range endpoint too large.",
61 "Bad number.",
62 "``\\digit'' out of range.",
63 "No remembered search string.",
64 "\\( \\) imbalance.",
65 "Too many \\(.",
66 "More than 2 numbers given in \\{ \\}.",
67 "} expected after \\.",
68 "First number exceeds second in \\{ \\}.",
69 "[ ] imbalance.",
70 "Regular expression overflow.",
71 "Illegal byte sequence.",
72 "Unknown regexp error code!!",
73 NULL
74 };
75
76 #define errmsg(msg, arg) (void) fprintf(stderr, gettext(msg), arg)
77 #define BLKSIZE 512
78 #define GBUFSIZ 8192
79 #define MAX_DEPTH 1000
80
81 static int temp;
82 static long long lnum;
83 static char *linebuf;
84 static char *prntbuf = NULL;
85 static long fw_lPrntBufLen = 0;
86 static int nflag;
87 static int bflag;
88 static int lflag;
89 static int cflag;
90 static int rflag;
91 static int Rflag;
92 static int vflag;
93 static int sflag;
94 static int iflag;
95 static int wflag;
96 static int hflag;
97 static int qflag;
98 static int errflg;
99 static int nfile;
100 static long long tln;
101 static int nsucc;
102 static int outfn = 0;
103 static int nlflag;
104 static char *ptr, *ptrend;
105 static char *expbuf;
106
107 static void execute(const char *, int);
108 static void regerr(int);
109 static void prepare(const char *);
110 static int recursive(const char *, const struct stat *, int, struct FTW *);
111 static int succeed(const char *);
112
113 int
114 main(int argc, char **argv)
115 {
116 int c;
117 char *arg;
118 extern int optind;
119
120 (void) setlocale(LC_ALL, "");
121 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
122 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
123 #endif
124 (void) textdomain(TEXT_DOMAIN);
125
126 while ((c = getopt(argc, argv, "hqblcnRrsviyw")) != -1)
127 switch (c) {
128 case 'h':
129 hflag++;
130 break;
131 case 'q': /* POSIX: quiet: status only */
132 qflag++;
133 break;
134 case 'v':
135 vflag++;
136 break;
137 case 'c':
138 cflag++;
139 break;
140 case 'n':
141 nflag++;
142 break;
143 case 'R':
144 Rflag++;
145 /* FALLTHROUGH */
146 case 'r':
147 rflag++;
148 break;
149 case 'b':
150 bflag++;
151 break;
152 case 's':
153 sflag++;
154 break;
155 case 'l':
156 lflag++;
157 break;
158 case 'y':
159 case 'i':
160 iflag++;
161 break;
162 case 'w':
163 wflag++;
164 break;
165 case '?':
166 errflg++;
167 }
168
169 if (errflg || (optind >= argc)) {
170 errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
171 "pattern file . . .\n",
172 (char *)NULL);
173 exit(2);
174 }
175
176 argv = &argv[optind];
177 argc -= optind;
178 nfile = argc - 1;
179
180 if (strrchr(*argv, '\n') != NULL)
181 regerr(41);
182
183 if (iflag) {
184 for (arg = *argv; *arg != NULL; ++arg)
185 *arg = (char)tolower((int)((unsigned char)*arg));
186 }
187
188 if (wflag) {
189 unsigned int wordlen;
190 char *wordbuf;
282 execute(const char *file, int base)
283 {
284 char *lbuf, *p;
285 long count;
286 long offset = 0;
287 char *next_ptr = NULL;
288 long next_count = 0;
289
290 tln = 0;
291
292 if (prntbuf == NULL) {
293 fw_lPrntBufLen = GBUFSIZ + 1;
294 if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
295 exit(2); /* out of memory - BAIL */
296 }
297 if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
298 exit(2); /* out of memory - BAIL */
299 }
300 }
301
302 if (file == NULL)
303 temp = 0;
304 else if ((temp = open(file + base, O_RDONLY)) == -1) {
305 if (!sflag)
306 errmsg("grep: can't open %s\n", file);
307 nsucc = 2;
308 return;
309 }
310
311 /* read in first block of bytes */
312 if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
313 (void) close(temp);
314
315 if (cflag && !qflag) {
316 if (nfile > 1 && !hflag && file)
317 (void) fprintf(stdout, "%s:", file);
318 if (!rflag)
319 (void) fprintf(stdout, "%lld\n", tln);
320 }
321 return;
322 }
323
324 lnum = 0;
325 ptr = prntbuf;
326 for (;;) {
327 /* look for next newline */
328 if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
329 offset += count;
330
331 /*
332 * shift unused data to the beginning of the buffer
333 */
334 if (ptr > prntbuf) {
335 (void) memmove(prntbuf, ptr, offset);
336 ptr = prntbuf;
393 } else
394 /*
395 * Use record as is
396 */
397 lbuf = ptr;
398
399 /* lflag only once */
400 if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
401 break;
402
403 if (!nlflag)
404 break;
405
406 ptr = next_ptr;
407 count = next_count;
408 offset = 0;
409 }
410 (void) close(temp);
411
412 if (cflag && !qflag) {
413 if (!hflag && file && (nfile > 1 ||
414 (rflag && outfn)))
415 (void) fprintf(stdout, "%s:", file);
416 (void) fprintf(stdout, "%lld\n", tln);
417 }
418 }
419
420 static int
421 succeed(const char *f)
422 {
423 int nchars;
424 nsucc = (nsucc == 2) ? 2 : 1;
425
426 if (f == NULL)
427 f = "<stdin>";
428
429 if (qflag) {
430 /* no need to continue */
431 return (1);
432 }
433
434 if (cflag) {
435 tln++;
436 return (0);
437 }
438
439 if (lflag) {
440 (void) fprintf(stdout, "%s\n", f);
441 return (1);
442 }
443
444 if (!hflag && (nfile > 1 || (rflag && outfn))) {
445 /* print filename */
446 (void) fprintf(stdout, "%s:", f);
447 }
448
449 if (bflag)
450 /* print block number */
451 (void) fprintf(stdout, "%lld:", (offset_t)
452 ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
453
454 if (nflag)
455 /* print line number */
456 (void) fprintf(stdout, "%lld:", lnum);
457
458 if (nlflag) {
459 /* newline at end of line */
460 *ptrend = '\n';
461 nchars = ptrend - ptr + 1;
462 } else {
463 /* don't write sentinel \0 */
464 nchars = ptrend - ptr;
|
56 #include <limits.h>
57 #include <sys/param.h>
58
59 static const char *errstr[] = {
60 "Range endpoint too large.",
61 "Bad number.",
62 "``\\digit'' out of range.",
63 "No remembered search string.",
64 "\\( \\) imbalance.",
65 "Too many \\(.",
66 "More than 2 numbers given in \\{ \\}.",
67 "} expected after \\.",
68 "First number exceeds second in \\{ \\}.",
69 "[ ] imbalance.",
70 "Regular expression overflow.",
71 "Illegal byte sequence.",
72 "Unknown regexp error code!!",
73 NULL
74 };
75
76 #define STDIN_FILENAME gettext("(standard input)")
77
78 #define errmsg(msg, arg) (void) fprintf(stderr, gettext(msg), arg)
79 #define BLKSIZE 512
80 #define GBUFSIZ 8192
81 #define MAX_DEPTH 1000
82
83 static int temp;
84 static long long lnum;
85 static char *linebuf;
86 static char *prntbuf = NULL;
87 static long fw_lPrntBufLen = 0;
88 static int nflag;
89 static int bflag;
90 static int lflag;
91 static int cflag;
92 static int rflag;
93 static int Rflag;
94 static int vflag;
95 static int sflag;
96 static int iflag;
97 static int wflag;
98 static int hflag;
99 static int Hflag;
100 static int qflag;
101 static int errflg;
102 static int nfile;
103 static long long tln;
104 static int nsucc;
105 static int outfn = 0;
106 static int nlflag;
107 static char *ptr, *ptrend;
108 static char *expbuf;
109
110 static void execute(const char *, int);
111 static void regerr(int);
112 static void prepare(const char *);
113 static int recursive(const char *, const struct stat *, int, struct FTW *);
114 static int succeed(const char *);
115
116 int
117 main(int argc, char **argv)
118 {
119 int c;
120 char *arg;
121 extern int optind;
122
123 (void) setlocale(LC_ALL, "");
124 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
125 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
126 #endif
127 (void) textdomain(TEXT_DOMAIN);
128
129 while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
130 switch (c) {
131 /* based on options order h or H is set as in GNU grep */
132 case 'h':
133 hflag++;
134 Hflag = 0; /* h excludes H */
135 break;
136 case 'H':
137 if (!lflag) /* H is excluded by l */
138 Hflag++;
139 hflag = 0; /* H excludes h */
140 break;
141 case 'q': /* POSIX: quiet: status only */
142 qflag++;
143 break;
144 case 'v':
145 vflag++;
146 break;
147 case 'c':
148 cflag++;
149 break;
150 case 'n':
151 nflag++;
152 break;
153 case 'R':
154 Rflag++;
155 /* FALLTHROUGH */
156 case 'r':
157 rflag++;
158 break;
159 case 'b':
160 bflag++;
161 break;
162 case 's':
163 sflag++;
164 break;
165 case 'l':
166 lflag++;
167 Hflag = 0; /* l excludes H */
168 break;
169 case 'y':
170 case 'i':
171 iflag++;
172 break;
173 case 'w':
174 wflag++;
175 break;
176 case '?':
177 errflg++;
178 }
179
180 if (errflg || (optind >= argc)) {
181 errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
182 "pattern file . . .\n",
183 (char *)NULL);
184 exit(2);
185 }
186
187 argv = &argv[optind];
188 argc -= optind;
189 nfile = argc - 1;
190
191 if (strrchr(*argv, '\n') != NULL)
192 regerr(41);
193
194 if (iflag) {
195 for (arg = *argv; *arg != NULL; ++arg)
196 *arg = (char)tolower((int)((unsigned char)*arg));
197 }
198
199 if (wflag) {
200 unsigned int wordlen;
201 char *wordbuf;
293 execute(const char *file, int base)
294 {
295 char *lbuf, *p;
296 long count;
297 long offset = 0;
298 char *next_ptr = NULL;
299 long next_count = 0;
300
301 tln = 0;
302
303 if (prntbuf == NULL) {
304 fw_lPrntBufLen = GBUFSIZ + 1;
305 if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
306 exit(2); /* out of memory - BAIL */
307 }
308 if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
309 exit(2); /* out of memory - BAIL */
310 }
311 }
312
313 if (file == NULL) {
314 temp = 0;
315 file = STDIN_FILENAME;
316 } else if ((temp = open(file + base, O_RDONLY)) == -1) {
317 if (!sflag)
318 errmsg("grep: can't open %s\n", file);
319 nsucc = 2;
320 return;
321 }
322
323 /* read in first block of bytes */
324 if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
325 (void) close(temp);
326
327 if (cflag && !qflag) {
328 if (Hflag || (nfile > 1 && !hflag))
329 (void) fprintf(stdout, "%s:", file);
330 if (!rflag)
331 (void) fprintf(stdout, "%lld\n", tln);
332 }
333 return;
334 }
335
336 lnum = 0;
337 ptr = prntbuf;
338 for (;;) {
339 /* look for next newline */
340 if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
341 offset += count;
342
343 /*
344 * shift unused data to the beginning of the buffer
345 */
346 if (ptr > prntbuf) {
347 (void) memmove(prntbuf, ptr, offset);
348 ptr = prntbuf;
405 } else
406 /*
407 * Use record as is
408 */
409 lbuf = ptr;
410
411 /* lflag only once */
412 if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
413 break;
414
415 if (!nlflag)
416 break;
417
418 ptr = next_ptr;
419 count = next_count;
420 offset = 0;
421 }
422 (void) close(temp);
423
424 if (cflag && !qflag) {
425 if (Hflag || (!hflag && ((nfile > 1) ||
426 (rflag && outfn))))
427 (void) fprintf(stdout, "%s:", file);
428 (void) fprintf(stdout, "%lld\n", tln);
429 }
430 }
431
432 static int
433 succeed(const char *f)
434 {
435 int nchars;
436 nsucc = (nsucc == 2) ? 2 : 1;
437
438 if (qflag) {
439 /* no need to continue */
440 return (1);
441 }
442
443 if (cflag) {
444 tln++;
445 return (0);
446 }
447
448 if (lflag) {
449 (void) fprintf(stdout, "%s\n", f);
450 return (1);
451 }
452
453 if (Hflag || (!hflag && (nfile > 1 || (rflag && outfn)))) {
454 /* print filename */
455 (void) fprintf(stdout, "%s:", f);
456 }
457
458 if (bflag)
459 /* print block number */
460 (void) fprintf(stdout, "%lld:", (offset_t)
461 ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
462
463 if (nflag)
464 /* print line number */
465 (void) fprintf(stdout, "%lld:", lnum);
466
467 if (nlflag) {
468 /* newline at end of line */
469 *ptrend = '\n';
470 nchars = ptrend - ptr + 1;
471 } else {
472 /* don't write sentinel \0 */
473 nchars = ptrend - ptr;
|