1 /* grep.c - main driver file for grep.
2 Copyright 1992, 1997-1999, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 /* Written July 1992 by Mike Haertel. */
20 /* Builtin decompression 1997 by Wolfram Schneider <wosch@FreeBSD.org>. */
27 #include <sys/types.h>
29 #if defined(HAVE_MMAP)
30 # include <sys/mman.h>
32 #if defined(HAVE_SETRLIMIT)
33 # include <sys/time.h>
34 # include <sys/resource.h>
36 #if defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H && defined HAVE_MBRTOWC
37 /* We can handle multibyte string. */
45 #include "getpagesize.h"
55 #define MAX(A,B) ((A) > (B) ? (A) : (B))
59 struct stats const *parent;
63 /* base of chain of stat buffers, used to detect directory loops */
64 static struct stats stats_base;
66 /* if non-zero, display usage information and exit */
69 /* If non-zero, print the version on standard output and exit. */
70 static int show_version;
72 /* If nonzero, suppress diagnostics for nonexistent or unreadable files. */
73 static int suppress_errors;
75 /* If nonzero, use mmap if possible. */
76 static int mmap_option;
78 /* If zero, output nulls after filenames. */
79 static int filename_mask;
81 /* If nonzero, use grep_color marker. */
82 static int color_option;
84 /* If nonzero, show only the part of a line matching the expression. */
85 static int only_matching;
87 /* The color string used. The user can overwrite it using the environment
88 variable GREP_COLOR. The default is to print red. */
89 static const char *grep_color = "01;31";
91 static struct exclude *excluded_patterns;
92 static struct exclude *included_patterns;
94 static char const short_options[] =
95 "0123456789A:B:C:D:EFGHIJPUVX:abcd:e:f:hiKLlm:noqRrsuvwxyZz";
97 /* Non-boolean long options that have no corresponding short equivalents. */
100 BINARY_FILES_OPTION = CHAR_MAX + 1,
105 LINE_BUFFERED_OPTION,
109 /* Long options equivalences. */
110 static struct option const long_options[] =
112 {"after-context", required_argument, NULL, 'A'},
113 {"basic-regexp", no_argument, NULL, 'G'},
114 {"before-context", required_argument, NULL, 'B'},
115 {"binary-files", required_argument, NULL, BINARY_FILES_OPTION},
116 {"byte-offset", no_argument, NULL, 'b'},
117 {"context", required_argument, NULL, 'C'},
118 {"color", optional_argument, NULL, COLOR_OPTION},
119 {"colour", optional_argument, NULL, COLOR_OPTION},
120 {"count", no_argument, NULL, 'c'},
121 {"devices", required_argument, NULL, 'D'},
122 {"directories", required_argument, NULL, 'd'},
123 {"extended-regexp", no_argument, NULL, 'E'},
124 {"exclude", required_argument, NULL, EXCLUDE_OPTION},
125 {"exclude-from", required_argument, NULL, EXCLUDE_FROM_OPTION},
126 {"file", required_argument, NULL, 'f'},
127 {"files-with-matches", no_argument, NULL, 'l'},
128 {"files-without-match", no_argument, NULL, 'L'},
129 {"fixed-regexp", no_argument, NULL, 'F'},
130 {"fixed-strings", no_argument, NULL, 'F'},
131 {"help", no_argument, &show_help, 1},
132 {"include", required_argument, NULL, INCLUDE_OPTION},
133 {"ignore-case", no_argument, NULL, 'i'},
134 {"label", required_argument, NULL, LABEL_OPTION},
135 {"line-buffered", no_argument, NULL, LINE_BUFFERED_OPTION},
136 {"line-number", no_argument, NULL, 'n'},
137 {"line-regexp", no_argument, NULL, 'x'},
138 {"max-count", required_argument, NULL, 'm'},
139 {"mmap", no_argument, &mmap_option, 1},
140 {"no-filename", no_argument, NULL, 'h'},
141 {"no-messages", no_argument, NULL, 's'},
142 {"bz2decompress", no_argument, NULL, 'J'},
144 {"decompress", no_argument, NULL, 'Z'},
145 {"null", no_argument, &filename_mask, 0},
147 {"null", no_argument, NULL, 'Z'},
149 {"null-data", no_argument, NULL, 'z'},
150 {"only-matching", no_argument, NULL, 'o'},
151 {"perl-regexp", no_argument, NULL, 'P'},
152 {"quiet", no_argument, NULL, 'q'},
153 {"recursive", no_argument, NULL, 'r'},
154 {"recursive", no_argument, NULL, 'R'},
155 {"regexp", required_argument, NULL, 'e'},
156 {"invert-match", no_argument, NULL, 'v'},
157 {"silent", no_argument, NULL, 'q'},
158 {"text", no_argument, NULL, 'a'},
159 {"binary", no_argument, NULL, 'U'},
160 {"unix-byte-offsets", no_argument, NULL, 'u'},
161 {"version", no_argument, NULL, 'V'},
162 {"with-filename", no_argument, NULL, 'H'},
163 {"word-regexp", no_argument, NULL, 'w'},
167 /* Define flags declared in grep.h. */
171 unsigned char eolbyte;
173 /* For error messages. */
174 /* The name the program was run with, stripped of any leading path. */
176 static char const *filename;
179 /* How to handle directories. */
185 } directories = READ_DIRECTORIES;
187 /* How to handle devices. */
192 } devices = READ_DEVICES;
194 static int grepdir PARAMS ((char const *, struct stats const *));
195 #if defined(HAVE_DOS_FILE_CONTENTS)
196 static inline int undossify_input PARAMS ((register char *, size_t));
199 /* Functions we'll use to search. */
200 static void (*compile) PARAMS ((char const *, size_t));
201 static size_t (*execute) PARAMS ((char const *, size_t, size_t *, int));
203 /* Like error, but suppress the diagnostic if requested. */
205 suppressible_error (char const *mesg, int errnum)
207 if (! suppress_errors)
208 error (0, errnum, "%s", mesg);
212 /* Convert STR to a positive integer, storing the result in *OUT.
213 STR must be a valid context length argument; report an error if it
216 context_length_arg (char const *str, int *out)
219 if (! (xstrtoumax (str, 0, 10, &value, "") == LONGINT_OK
220 && 0 <= (*out = value)
223 error (2, 0, "%s: %s\n", str, _("invalid context length argument"));
228 /* Hairy buffering mechanism for grep. The intent is to keep
229 all reads aligned on a page boundary and multiples of the
230 page size, unless a read yields a partial page. */
232 static char *buffer; /* Base of buffer. */
233 static size_t bufalloc; /* Allocated buffer size, counting slop. */
234 #define INITIAL_BUFSIZE 32768 /* Initial buffer size, not counting slop. */
235 static int bufdesc; /* File descriptor. */
236 static char *bufbeg; /* Beginning of user-visible stuff. */
237 static char *buflim; /* Limit of user-visible stuff. */
238 static size_t pagesize; /* alignment of memory pages */
239 static off_t bufoffset; /* Read offset; defined on regular files. */
240 static off_t after_last_match; /* Pointer after last matching line that
241 would have been output if we were
242 outputting characters. */
244 #if defined(HAVE_MMAP)
245 static int bufmapped; /* True if buffer is memory-mapped. */
246 static off_t initial_bufoffset; /* Initial value of bufoffset. */
252 static BZFILE* bzbufdesc; /* libbz2 file handle. */
253 static int BZflag; /* uncompress before searching. */
256 static gzFile gzbufdesc; /* zlib file descriptor. */
257 static int Zflag; /* uncompress before searching. */
260 /* Return VAL aligned to the next multiple of ALIGNMENT. VAL can be
261 an integer or a pointer. Both args must be free of side effects. */
262 #define ALIGN_TO(val, alignment) \
263 ((size_t) (val) % (alignment) == 0 \
265 : (val) + ((alignment) - (size_t) (val) % (alignment)))
267 /* Reset the buffer for a new file, returning zero if we should skip it.
268 Initialize on the first time through. */
270 reset (int fd, char const *file, struct stats *stats)
274 pagesize = getpagesize ();
275 if (pagesize == 0 || 2 * pagesize + 1 <= pagesize)
277 bufalloc = ALIGN_TO (INITIAL_BUFSIZE, pagesize) + pagesize + 1;
278 buffer = xmalloc (bufalloc);
282 bzbufdesc = BZ2_bzdopen(fd, "r");
283 if (bzbufdesc == NULL)
284 error(2, 0, _("memory exhausted"));
289 gzbufdesc = gzdopen(fd, "r");
290 if (gzbufdesc == NULL)
291 error(2, 0, _("memory exhausted"));
295 bufbeg = buflim = ALIGN_TO (buffer + 1, pagesize);
296 bufbeg[-1] = eolbyte;
299 if (fstat (fd, &stats->stat) != 0)
301 error (0, errno, "fstat");
304 if (directories == SKIP_DIRECTORIES && S_ISDIR (stats->stat.st_mode))
307 if (devices == SKIP_DEVICES && (S_ISCHR(stats->stat.st_mode) || S_ISBLK(stats->stat.st_mode) || S_ISSOCK(stats->stat.st_mode)))
309 if (devices == SKIP_DEVICES && (S_ISCHR(stats->stat.st_mode) || S_ISBLK(stats->stat.st_mode)))
317 S_ISREG (stats->stat.st_mode))
323 bufoffset = lseek (fd, 0, SEEK_CUR);
326 error (0, errno, "lseek");
330 #if defined(HAVE_MMAP)
331 initial_bufoffset = bufoffset;
332 bufmapped = mmap_option && bufoffset % pagesize == 0;
337 #if defined(HAVE_MMAP)
344 /* Read new stuff into the buffer, saving the specified
345 amount of old stuff. When we're done, 'bufbeg' points
346 to the beginning of the buffer contents, and 'buflim'
347 points just after the end. Return zero if there's an error. */
349 fillbuf (size_t save, struct stats const *stats)
356 /* Offset from start of buffer to start of old stuff
357 that we want to save. */
358 size_t saved_offset = buflim - save - buffer;
360 if (pagesize <= buffer + bufalloc - buflim)
363 bufbeg = buflim - save;
367 size_t minsize = save + pagesize;
372 /* Grow newsize until it is at least as great as minsize. */
373 for (newsize = bufalloc - pagesize - 1; newsize < minsize; newsize *= 2)
374 if (newsize * 2 < newsize || newsize * 2 + pagesize + 1 < newsize * 2)
377 /* Try not to allocate more memory than the file size indicates,
378 as that might cause unnecessary memory exhaustion if the file
379 is large. However, do not use the original file size as a
380 heuristic if we've already read past the file end, as most
381 likely the file is growing. */
382 if (S_ISREG (stats->stat.st_mode))
384 off_t to_be_read = stats->stat.st_size - bufoffset;
385 off_t maxsize_off = save + to_be_read;
386 if (0 <= to_be_read && to_be_read <= maxsize_off
387 && maxsize_off == (size_t) maxsize_off
388 && minsize <= (size_t) maxsize_off
389 && (size_t) maxsize_off < newsize)
390 newsize = maxsize_off;
393 /* Add enough room so that the buffer is aligned and has room
394 for byte sentinels fore and aft. */
395 newalloc = newsize + pagesize + 1;
397 newbuf = bufalloc < newalloc ? xmalloc (bufalloc = newalloc) : buffer;
398 readbuf = ALIGN_TO (newbuf + 1 + save, pagesize);
399 bufbeg = readbuf - save;
400 memmove (bufbeg, buffer + saved_offset, save);
401 bufbeg[-1] = eolbyte;
402 if (newbuf != buffer)
409 readsize = buffer + bufalloc - readbuf;
410 readsize -= readsize % pagesize;
412 #if defined(HAVE_MMAP)
415 size_t mmapsize = readsize;
417 /* Don't mmap past the end of the file; some hosts don't allow this.
418 Use `read' on the last page. */
419 if (stats->stat.st_size - bufoffset < mmapsize)
421 mmapsize = stats->stat.st_size - bufoffset;
422 mmapsize -= mmapsize % pagesize;
426 && (mmap ((caddr_t) readbuf, mmapsize,
427 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
431 /* Do not bother to use madvise with MADV_SEQUENTIAL or
432 MADV_WILLNEED on the mmapped memory. One might think it
433 would help, but it slows us down about 30% on SunOS 4.1. */
438 /* Stop using mmap on this file. Synchronize the file
439 offset. Do not warn about mmap failures. On some hosts
440 (e.g. Solaris 2.5) mmap can fail merely because some
441 other process has an advisory read lock on the file.
442 There's no point alarming the user about this misfeature. */
444 if (bufoffset != initial_bufoffset
445 && lseek (bufdesc, bufoffset, SEEK_SET) < 0)
447 error (0, errno, "lseek");
458 if (BZflag && bzbufdesc)
461 bytesread = BZ2_bzRead (&bzerr, bzbufdesc, readbuf, readsize);
469 case BZ_DATA_ERROR_MAGIC:
470 BZ2_bzReadClose (&bzerr, bzbufdesc); bzbufdesc = NULL;
471 lseek (bufdesc, 0, SEEK_SET);
472 bytesread = read (bufdesc, readbuf, readsize);
482 bytesread = gzread (gzbufdesc, readbuf, readsize);
485 bytesread = read (bufdesc, readbuf, readsize);
486 while (bytesread < 0 && errno == EINTR);
490 fillsize = bytesread;
493 bufoffset += fillsize;
494 #if defined(HAVE_DOS_FILE_CONTENTS)
496 fillsize = undossify_input (readbuf, fillsize);
498 buflim = readbuf + fillsize;
502 /* Flags controlling the style of output. */
507 WITHOUT_MATCH_BINARY_FILES
508 } binary_files; /* How to handle binary files. */
510 static int filename_mask; /* If zero, output nulls after filenames. */
511 static int out_quiet; /* Suppress all normal output. */
512 static int out_invert; /* Print nonmatching stuff. */
513 static int out_file; /* Print filenames. */
514 static int out_line; /* Print line numbers. */
515 static int out_byte; /* Print byte offsets. */
516 static int out_before; /* Lines of leading context. */
517 static int out_after; /* Lines of trailing context. */
518 static int count_matches; /* Count matching lines. */
519 static int list_files; /* List matching files. */
520 static int no_filenames; /* Suppress file names. */
521 static off_t max_count; /* Stop after outputting this many
522 lines from an input file. */
523 static int line_buffered; /* If nonzero, use line buffering, i.e.
524 fflush everyline out. */
525 static char *label = NULL; /* Fake filename for stdin */
528 /* Internal variables to keep track of byte count, context, etc. */
529 static uintmax_t totalcc; /* Total character count before bufbeg. */
530 static char const *lastnl; /* Pointer after last newline counted. */
531 static char const *lastout; /* Pointer after last character output;
532 NULL if no character has been output
533 or if it's conceptually before bufbeg. */
534 static uintmax_t totalnl; /* Total newline count before lastnl. */
535 static off_t outleft; /* Maximum number of lines to be output. */
536 static int pending; /* Pending lines of output.
537 Always kept 0 if out_quiet is true. */
538 static int done_on_match; /* Stop scanning file on first match. */
539 static int exit_on_match; /* Exit on first match. */
541 #if defined(HAVE_DOS_FILE_CONTENTS)
545 /* Add two numbers that count input bytes or lines, and report an
546 error if the addition overflows. */
548 add_count (uintmax_t a, uintmax_t b)
550 uintmax_t sum = a + b;
552 error (2, 0, _("input is too large to count"));
557 nlscan (char const *lim)
561 for (beg = lastnl; beg != lim; beg = memchr (beg, eolbyte, lim - beg), beg++)
563 totalnl = add_count (totalnl, newlines);
567 /* Print a byte offset, followed by a character separator. */
569 print_offset_sep (uintmax_t pos, char sep)
571 /* Do not rely on printf to print pos, since uintmax_t may be longer
572 than long, and long long is not portable. */
574 char buf[sizeof pos * CHAR_BIT];
575 char *p = buf + sizeof buf - 1;
579 *--p = '0' + pos % 10;
580 while ((pos /= 10) != 0);
582 fwrite (p, 1, buf + sizeof buf - p, stdout);
586 prline (char const *beg, char const *lim, int sep)
589 printf ("%s%c", filename, sep & filename_mask);
593 totalnl = add_count (totalnl, 1);
594 print_offset_sep (totalnl, sep);
599 uintmax_t pos = add_count (totalcc, beg - bufbeg);
600 #if defined(HAVE_DOS_FILE_CONTENTS)
601 pos = dossified_pos (pos);
603 print_offset_sep (pos, sep);
609 while ((match_offset = (*execute) (beg, lim - beg, &match_size, 1))
612 char const *b = beg + match_offset;
618 printf("\33[%sm", grep_color);
619 fwrite(b, sizeof (char), match_size, stdout);
621 fputs("\33[00m", stdout);
623 beg = b + match_size;
634 while (lim-beg && (match_offset = (*execute) (beg, lim - beg, &match_size, 1))
637 char const *b = beg + match_offset;
638 /* Avoid matching the empty line at the end of the buffer. */
641 /* Avoid hanging on grep --color "" foo */
644 fwrite (beg, sizeof (char), match_offset, stdout);
645 printf ("\33[%sm", grep_color);
646 fwrite (b, sizeof (char), match_size, stdout);
647 fputs ("\33[00m", stdout);
648 beg = b + match_size;
650 fputs ("\33[K", stdout);
652 fwrite (beg, 1, lim - beg, stdout);
654 error (0, errno, _("writing output"));
660 /* Print pending lines of trailing context prior to LIM. Trailing context ends
661 at the next matching line when OUTLEFT is 0. */
663 prpending (char const *lim)
667 while (pending > 0 && lastout < lim)
669 char const *nl = memchr (lastout, eolbyte, lim - lastout);
673 || (((*execute) (lastout, nl - lastout, &match_size, 0) == (size_t) -1)
675 prline (lastout, nl + 1, '-');
681 /* Print the lines between BEG and LIM. Deal with context crap.
682 If NLINESP is non-null, store a count of lines between BEG and LIM. */
684 prtext (char const *beg, char const *lim, int *nlinesp)
686 static int used; /* avoid printing "--" before any output */
691 if (!out_quiet && pending > 0)
698 /* Deal with leading context crap. */
700 bp = lastout ? lastout : bufbeg;
701 for (i = 0; i < out_before; ++i)
705 while (p[-1] != eol);
707 /* We only print the "--" separator if our output is
708 discontiguous from the last output in the file. */
709 if ((out_before || out_after) && used && p != lastout)
714 char const *nl = memchr (p, eol, beg - p);
723 /* Caller wants a line count. */
724 for (n = 0; p < lim && n < outleft; n++)
726 char const *nl = memchr (p, eol, lim - p);
734 /* relying on it that this function is never called when outleft = 0. */
735 after_last_match = bufoffset - (buflim - p);
739 prline (beg, lim, ':');
741 pending = out_quiet ? 0 : out_after;
745 /* Scan the specified portion of the buffer, matching lines (or
746 between matching lines if OUT_INVERT is true). Return a count of
749 grepbuf (char const *beg, char const *lim)
752 register char const *p;
758 while ((match_offset = (*execute) (p, lim - p, &match_size, 0)) != (size_t) -1)
760 char const *b = p + match_offset;
761 char const *endp = b + match_size;
762 /* Avoid matching the empty line at the end of the buffer. */
767 prtext (b, endp, (int *) 0);
770 if (!outleft || done_on_match)
774 after_last_match = bufoffset - (buflim - endp);
788 if (out_invert && p < lim)
797 /* Search a given file. Normally, return a count of lines printed;
798 but if the file is a directory and we search it recursively, then
799 return -2 if there was a match, and -1 otherwise. */
801 grep (int fd, char const *file, struct stats *stats)
805 size_t residue, save;
811 if (!reset (fd, file, stats))
814 if (file && directories == RECURSE_DIRECTORIES
815 && S_ISDIR (stats->stat.st_mode))
817 /* Close fd now, so that we don't open a lot of file descriptors
818 when we recurse deeply. */
819 if (BZflag && bzbufdesc)
820 BZ2_bzclose(bzbufdesc);
828 error (0, errno, "%s", file);
829 return grepdir (file, stats) - 2;
836 after_last_match = 0;
843 if (! fillbuf (save, stats))
845 if (! is_EISDIR (errno, file))
846 suppressible_error (filename, errno);
850 not_text = (((binary_files == BINARY_BINARY_FILES && !out_quiet)
851 || binary_files == WITHOUT_MATCH_BINARY_FILES)
852 && memchr (bufbeg, eol ? '\0' : '\200', buflim - bufbeg));
853 if (not_text && binary_files == WITHOUT_MATCH_BINARY_FILES)
855 done_on_match += not_text;
856 out_quiet += not_text;
866 /* no more data to scan (eof) except for maybe a residue -> break */
870 /* Determine new residue (the length of an incomplete line at the end of
871 the buffer, 0 means there is no incomplete last line). */
874 for (lim = buflim; lim[-1] != eol; lim--)
880 residue = buflim - lim;
885 nlines += grepbuf (beg, lim);
888 if((!outleft && !pending) || (nlines && done_on_match && !out_invert))
892 /* The last OUT_BEFORE lines at the end of the buffer will be needed as
893 leading context if there is a matching line at the begin of the
894 next data. Make beg point to their begin. */
897 while (i < out_before && beg > bufbeg && beg != lastout)
902 while (beg[-1] != eol);
905 /* detect if leading context is discontinuous from last printed line. */
909 /* Handle some details and read more data to scan. */
910 save = residue + lim - beg;
912 totalcc = add_count (totalcc, buflim - bufbeg - save);
915 if (! fillbuf (save, stats))
917 if (! is_EISDIR (errno, file))
918 suppressible_error (filename, errno);
926 nlines += grepbuf (bufbeg + save - residue, buflim);
932 done_on_match -= not_text;
933 out_quiet -= not_text;
934 if ((not_text & ~out_quiet) && nlines != 0)
935 printf (_("Binary file %s matches\n"), filename);
940 grepfile (char const *file, struct stats *stats)
949 filename = label ? label : _("(standard input)");
953 while ((desc = open (file, O_RDONLY)) < 0 && errno == EINTR)
960 if (is_EISDIR (e, file) && directories == RECURSE_DIRECTORIES)
962 if (stat (file, &stats->stat) != 0)
964 error (0, errno, "%s", file);
968 return grepdir (file, stats);
971 if (!suppress_errors)
973 if (directories == SKIP_DIRECTORIES)
981 /* When skipping directories, don't worry about
982 directories that can't be opened. */
989 suppressible_error (file, e);
996 #if defined(SET_BINARY)
997 /* Set input to binary mode. Pipes are simulated with files
998 on DOS, so this includes the case of "foo | grep bar". */
1003 count = grep (desc, file, stats);
1011 printf ("%s%c", filename, ':' & filename_mask);
1012 printf ("%d\n", count);
1016 if (list_files == 1 - 2 * status)
1017 printf ("%s%c", filename, '\n' & filename_mask);
1019 if (BZflag && bzbufdesc)
1020 BZ2_bzclose(bzbufdesc);
1029 off_t required_offset = outleft ? bufoffset : after_last_match;
1030 if ((bufmapped || required_offset != bufoffset)
1031 && lseek (desc, required_offset, SEEK_SET) < 0
1032 && S_ISREG (stats->stat.st_mode))
1033 error (0, errno, "%s", filename);
1036 while (close (desc) != 0)
1039 error (0, errno, "%s", file);
1048 grepdir (char const *dir, struct stats const *stats)
1051 struct stats const *ancestor;
1054 /* Mingw32 does not support st_ino. No known working hosts use zero
1055 for st_ino, so assume that the Mingw32 bug applies if it's zero. */
1056 if (stats->stat.st_ino)
1057 for (ancestor = stats; (ancestor = ancestor->parent) != 0; )
1058 if (ancestor->stat.st_ino == stats->stat.st_ino
1059 && ancestor->stat.st_dev == stats->stat.st_dev)
1061 if (!suppress_errors)
1062 error (0, 0, _("warning: %s: %s"), dir,
1063 _("recursive directory loop"));
1067 name_space = savedir (dir, stats->stat.st_size, included_patterns,
1073 suppressible_error (dir, errno);
1079 size_t dirlen = strlen (dir);
1080 int needs_slash = ! (dirlen == FILESYSTEM_PREFIX_LEN (dir)
1081 || IS_SLASH (dir[dirlen - 1]));
1083 char const *namep = name_space;
1085 child.parent = stats;
1086 out_file += !no_filenames;
1089 size_t namelen = strlen (namep);
1090 file = xrealloc (file, dirlen + 1 + namelen + 1);
1093 strcpy (file + dirlen + needs_slash, namep);
1094 namep += namelen + 1;
1095 status &= grepfile (file, &child);
1097 out_file -= !no_filenames;
1111 fprintf (stderr, _("Usage: %s [OPTION]... PATTERN [FILE]...\n"),
1113 fprintf (stderr, _("Try `%s --help' for more information.\n"),
1118 printf (_("Usage: %s [OPTION]... PATTERN [FILE] ...\n"), program_name);
1120 Search for PATTERN in each FILE or standard input.\n\
1121 Example: %s -i 'hello world' menu.h main.c\n\
1123 Regexp selection and interpretation:\n"), program_name);
1125 -E, --extended-regexp PATTERN is an extended regular expression\n\
1126 -F, --fixed-strings PATTERN is a set of newline-separated strings\n\
1127 -G, --basic-regexp PATTERN is a basic regular expression\n\
1128 -P, --perl-regexp PATTERN is a Perl regular expression\n"));
1130 -e, --regexp=PATTERN use PATTERN as a regular expression\n\
1131 -f, --file=FILE obtain PATTERN from FILE\n\
1132 -i, --ignore-case ignore case distinctions\n\
1133 -w, --word-regexp force PATTERN to match only whole words\n\
1134 -x, --line-regexp force PATTERN to match only whole lines\n\
1135 -z, --null-data a data line ends in 0 byte, not newline\n"));
1139 -s, --no-messages suppress error messages\n\
1140 -v, --invert-match select non-matching lines\n\
1141 -V, --version print version information and exit\n\
1142 --help display this help and exit\n\
1143 -J, --bz2decompress decompress bzip2'ed input before searching\n\
1144 -Z, --decompress decompress input before searching (HAVE_LIBZ=1)\n\
1145 --mmap use memory-mapped input if possible\n"));
1149 -m, --max-count=NUM stop after NUM matches\n\
1150 -b, --byte-offset print the byte offset with output lines\n\
1151 -n, --line-number print line number with output lines\n\
1152 --line-buffered flush output on every line\n\
1153 -H, --with-filename print the filename for each match\n\
1154 -h, --no-filename suppress the prefixing filename on output\n\
1155 --label=LABEL print LABEL as filename for standard input\n\
1156 -o, --only-matching show only the part of a line matching PATTERN\n\
1157 -q, --quiet, --silent suppress all normal output\n\
1158 --binary-files=TYPE assume that binary files are TYPE\n\
1159 TYPE is 'binary', 'text', or 'without-match'\n\
1160 -a, --text equivalent to --binary-files=text\n\
1161 -I equivalent to --binary-files=without-match\n\
1162 -d, --directories=ACTION how to handle directories\n\
1163 ACTION is 'read', 'recurse', or 'skip'\n\
1164 -D, --devices=ACTION how to handle devices, FIFOs and sockets\n\
1165 ACTION is 'read' or 'skip'\n\
1166 -R, -r, --recursive equivalent to --directories=recurse\n\
1167 --include=PATTERN files that match PATTERN will be examined\n\
1168 --exclude=PATTERN files that match PATTERN will be skipped.\n\
1169 --exclude-from=FILE files that match PATTERN in FILE will be skipped.\n\
1170 -L, --files-without-match only print FILE names containing no match\n\
1171 -l, --files-with-matches only print FILE names containing matches\n\
1172 -c, --count only print a count of matching lines per FILE\n\
1173 --null print 0 byte after FILE name\n"));
1177 -B, --before-context=NUM print NUM lines of leading context\n\
1178 -A, --after-context=NUM print NUM lines of trailing context\n\
1179 -C, --context=NUM print NUM lines of output context\n\
1180 -NUM same as --context=NUM\n\
1182 --colour[=WHEN] use markers to distinguish the matching string\n\
1183 WHEN may be `always', `never' or `auto'.\n\
1184 -U, --binary do not strip CR characters at EOL (MSDOS)\n\
1185 -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)\n\
1187 `egrep' means `grep -E'. `fgrep' means `grep -F'.\n\
1188 With no FILE, or when FILE is -, read standard input. If less than\n\
1189 two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,\n\
1190 and 2 if trouble.\n"));
1191 printf (_("\nReport bugs to <bug-gnu-utils@gnu.org>.\n"));
1196 /* Set the matcher to M, reporting any conflicts. */
1198 setmatcher (char const *m)
1200 if (matcher && strcmp (matcher, m) != 0)
1201 error (2, 0, _("conflicting matchers specified"));
1205 /* Go through the matchers vector and look for the specified matcher.
1206 If we find it, install it in compile and execute, and return 1. */
1208 install_matcher (char const *name)
1211 #if defined(HAVE_SETRLIMIT)
1215 for (i = 0; matchers[i].compile; i++)
1216 if (strcmp (name, matchers[i].name) == 0)
1218 compile = matchers[i].compile;
1219 execute = matchers[i].execute;
1220 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
1221 /* I think every platform needs to do this, so that regex.c
1222 doesn't oveflow the stack. The default value of
1223 `re_max_failures' is too large for some platforms: it needs
1224 more than 3MB-large stack.
1226 The test for HAVE_SETRLIMIT should go into `configure'. */
1227 if (!getrlimit (RLIMIT_STACK, &rlim))
1230 extern long int re_max_failures; /* from regex.c */
1232 /* Approximate the amount regex.c needs, plus some more. */
1233 newlim = re_max_failures * 2 * 20 * sizeof (char *);
1234 if (newlim > rlim.rlim_max)
1236 newlim = rlim.rlim_max;
1237 re_max_failures = newlim / (2 * 20 * sizeof (char *));
1239 if (rlim.rlim_cur < newlim)
1241 rlim.rlim_cur = newlim;
1242 setrlimit (RLIMIT_STACK, &rlim);
1251 /* Find the white-space-separated options specified by OPTIONS, and
1252 using BUF to store copies of these options, set ARGV[0], ARGV[1],
1253 etc. to the option copies. Return the number N of options found.
1254 Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0]
1255 etc. Backslash can be used to escape whitespace (and backslashes). */
1257 prepend_args (char const *options, char *buf, char **argv)
1259 char const *o = options;
1265 while (ISSPACE ((unsigned char) *o))
1274 if ((*b++ = *o++) == '\\' && *o)
1276 while (*o && ! ISSPACE ((unsigned char) *o));
1282 /* Prepend the whitespace-separated options in OPTIONS to the argument
1283 vector of a main program with argument count *PARGC and argument
1286 prepend_default_options (char const *options, int *pargc, char ***pargv)
1290 char *buf = xmalloc (strlen (options) + 1);
1291 int prepended = prepend_args (options, buf, (char **) NULL);
1293 char * const *argv = *pargv;
1294 char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
1295 *pargc = prepended + argc;
1298 pp += prepend_args (options, buf, pp);
1299 while ((*pp++ = *argv++))
1304 /* Get the next non-digit option from ARGC and ARGV.
1305 Return -1 if there are no more options.
1306 Process any digit options that were encountered on the way,
1307 and store the resulting integer into *DEFAULT_CONTEXT. */
1309 get_nondigit_option (int argc, char *const *argv, int *default_context)
1312 char buf[sizeof (uintmax_t) * CHAR_BIT + 4];
1315 /* Set buf[0] to anything but '0', for the leading-zero test below. */
1318 while (opt = getopt_long (argc, argv, short_options, long_options, NULL),
1319 '0' <= opt && opt <= '9')
1321 /* Suppress trivial leading zeros, to avoid incorrect
1322 diagnostic on strings like 00000000000. */
1326 if (p == buf + sizeof buf - 4)
1328 /* Too many digits. Append "..." to make context_length_arg
1329 complain about "X...", where X contains the digits seen
1339 context_length_arg (buf, default_context);
1346 main (int argc, char **argv)
1349 size_t cc, keycc, oldcc, keyalloc;
1352 int default_context;
1354 extern char *optarg;
1357 initialize_main (&argc, &argv);
1358 program_name = argv[0];
1359 if (program_name && strrchr (program_name, '/'))
1360 program_name = strrchr (program_name, '/') + 1;
1362 if (program_name[0] == 'b' && program_name[1] == 'z') {
1367 else if (program_name[0] == 'z') {
1373 #if defined(__MSDOS__) || defined(_WIN32)
1374 /* DOS and MS-Windows use backslashes as directory separators, and usually
1375 have an .exe suffix. They also have case-insensitive filesystems. */
1378 char *p = program_name;
1379 char *bslash = strrchr (argv[0], '\\');
1381 if (bslash && bslash >= program_name) /* for mixed forward/backslash case */
1382 program_name = bslash + 1;
1383 else if (program_name == argv[0]
1384 && argv[0][0] && argv[0][1] == ':') /* "c:progname" */
1385 program_name = argv[0] + 2;
1387 /* Collapse the letter-case, so `strcmp' could be used hence. */
1389 if (*p >= 'A' && *p <= 'Z')
1392 /* Remove the .exe extension, if any. */
1393 if ((p = strrchr (program_name, '.')) && strcmp (p, ".exe") == 0)
1404 max_count = TYPE_MAXIMUM (off_t);
1406 /* The value -1 means to use DEFAULT_CONTEXT. */
1407 out_after = out_before = -1;
1408 /* Default before/after context: chaged by -C/-NUM options */
1409 default_context = 0;
1410 /* Changed by -o option */
1413 /* Internationalization. */
1414 #if defined(HAVE_SETLOCALE)
1415 setlocale (LC_ALL, "");
1417 #if defined(ENABLE_NLS)
1418 bindtextdomain (PACKAGE, LOCALEDIR);
1419 textdomain (PACKAGE);
1422 atexit (close_stdout);
1424 prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
1426 while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
1430 context_length_arg (optarg, &out_after);
1434 context_length_arg (optarg, &out_before);
1438 /* Set output match context, but let any explicit leading or
1439 trailing amount specified with -A or -B stand. */
1440 context_length_arg (optarg, &default_context);
1444 if (strcmp (optarg, "read") == 0)
1445 devices = READ_DEVICES;
1446 else if (strcmp (optarg, "skip") == 0)
1447 devices = SKIP_DEVICES;
1449 error (2, 0, _("unknown devices method"));
1453 setmatcher ("egrep");
1457 setmatcher ("fgrep");
1461 setmatcher ("perl");
1465 setmatcher ("grep");
1473 binary_files = WITHOUT_MATCH_BINARY_FILES;
1478 printf (_("Cannot mix -Z and -J.\n"));
1485 #if defined(HAVE_DOS_FILE_CONTENTS)
1486 dos_use_file_type = DOS_BINARY;
1491 #if defined(HAVE_DOS_FILE_CONTENTS)
1492 dos_report_unix_offset = 1;
1501 setmatcher (optarg);
1505 binary_files = TEXT_BINARY_FILES;
1517 if (strcmp (optarg, "read") == 0)
1518 directories = READ_DIRECTORIES;
1519 else if (strcmp (optarg, "skip") == 0)
1520 directories = SKIP_DIRECTORIES;
1521 else if (strcmp (optarg, "recurse") == 0)
1522 directories = RECURSE_DIRECTORIES;
1524 error (2, 0, _("unknown directories method"));
1528 cc = strlen (optarg);
1529 keys = xrealloc (keys, keycc + cc + 1);
1530 strcpy (&keys[keycc], optarg);
1532 keys[keycc++] = '\n';
1536 fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin;
1538 error (2, errno, "%s", optarg);
1539 for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
1541 keys = xrealloc (keys, keyalloc);
1544 && (cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) > 0)
1547 if (keycc == keyalloc - 1)
1548 keys = xrealloc (keys, keyalloc *= 2);
1552 /* Append final newline if file ended in non-newline. */
1553 if (oldcc != keycc && keys[keycc - 1] != '\n')
1554 keys[keycc++] = '\n';
1562 case 'y': /* For old-timers . . . */
1567 /* Like -l, except list files that don't contain matches.
1568 Inspired by the same option in Hume's gre. */
1579 switch (xstrtoumax (optarg, 0, 10, &value, ""))
1583 if (0 <= max_count && max_count == value)
1586 case LONGINT_OVERFLOW:
1587 max_count = TYPE_MAXIMUM (off_t);
1591 error (2, 0, _("invalid max count"));
1606 close_stdout_set_status(0);
1611 directories = RECURSE_DIRECTORIES;
1615 suppress_errors = 1;
1634 printf (_("Cannot mix -J and -Z.\n"));
1647 case BINARY_FILES_OPTION:
1648 if (strcmp (optarg, "binary") == 0)
1649 binary_files = BINARY_BINARY_FILES;
1650 else if (strcmp (optarg, "text") == 0)
1651 binary_files = TEXT_BINARY_FILES;
1652 else if (strcmp (optarg, "without-match") == 0)
1653 binary_files = WITHOUT_MATCH_BINARY_FILES;
1655 error (2, 0, _("unknown binary-files type"));
1660 if(!strcasecmp(optarg, "always") || !strcasecmp(optarg, "yes") ||
1661 !strcasecmp(optarg, "force"))
1663 else if(!strcasecmp(optarg, "never") || !strcasecmp(optarg, "no") ||
1664 !strcasecmp(optarg, "none"))
1666 else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") ||
1667 !strcasecmp(optarg, "if-tty"))
1673 if(color_option == 2) {
1674 if(isatty(STDOUT_FILENO) && getenv("TERM") &&
1675 strcmp(getenv("TERM"), "dumb"))
1682 case EXCLUDE_OPTION:
1683 if (!excluded_patterns)
1684 excluded_patterns = new_exclude ();
1685 add_exclude (excluded_patterns, optarg);
1688 case EXCLUDE_FROM_OPTION:
1689 if (!excluded_patterns)
1690 excluded_patterns = new_exclude ();
1691 if (add_exclude_file (add_exclude, excluded_patterns, optarg, '\n')
1694 error (2, errno, "%s", optarg);
1698 case INCLUDE_OPTION:
1699 if (!included_patterns)
1700 included_patterns = new_exclude ();
1701 add_exclude (included_patterns, optarg);
1704 case LINE_BUFFERED_OPTION:
1722 /* POSIX.2 says that -q overrides -l, which in turn overrides the
1723 other output options. */
1726 if (exit_on_match | list_files)
1731 out_quiet = count_matches | done_on_match;
1734 out_after = default_context;
1736 out_before = default_context;
1740 char *userval = getenv ("GREP_COLOR");
1741 if (userval != NULL && *userval != '\0')
1742 grep_color = userval;
1746 matcher = program_name;
1750 printf (_("%s (GNU grep) %s\n"), matcher, VERSION);
1753 Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.\n"));
1755 This is free software; see the source for copying conditions. There is NO\n\
1756 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"));
1768 /* No keys were specified (e.g. -f /dev/null). Match nothing. */
1770 match_lines = match_words = 0;
1773 /* Strip trailing newline. */
1779 keys = argv[optind++];
1780 keycc = strlen (keys);
1785 if (!install_matcher (matcher) && !install_matcher ("default"))
1789 if (MB_CUR_MAX != 1 && match_icase)
1792 mbstate_t cur_state, prev_state;
1793 int i, len = strlen(keys);
1795 memset(&cur_state, 0, sizeof(mbstate_t));
1796 for (i = 0; i <= len ;)
1799 mbclen = mbrtowc(&wc, keys + i, len - i, &cur_state);
1800 if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
1802 /* An invalid sequence, or a truncated multibyte character.
1803 We treat it as a singlebyte character. */
1808 if (iswupper((wint_t)wc))
1810 wc = towlower((wint_t)wc);
1811 wcrtomb(keys + i, wc, &cur_state);
1817 #endif /* MBS_SUPPORT */
1819 (*compile)(keys, keycc);
1821 if ((argc - optind > 1 && !no_filenames) || with_filenames)
1825 /* Output is set to binary mode because we shouldn't convert
1826 NL to CR-LF pairs, especially when grepping binary files. */
1839 char *file = argv[optind];
1840 if ((included_patterns || excluded_patterns)
1843 if (included_patterns &&
1844 ! excluded_filename (included_patterns, file, 0))
1846 if (excluded_patterns &&
1847 excluded_filename (excluded_patterns, file, 0))
1850 status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
1853 while ( ++optind < argc);
1856 status = grepfile ((char *) NULL, &stats_base);
1858 /* We register via atexit() to test stdout. */
1859 exit (errseen ? 2 : status);
1861 /* vim:set shiftwidth=2: */