From 089f14a27a48df98af31ba267389df9c6a780d56 Mon Sep 17 00:00:00 2001 From: "David E. O'Brien" Date: Mon, 16 Sep 2002 04:27:29 +0000 Subject: [PATCH] Create bzgrep. --- gnu/usr.bin/grep/Makefile | 7 +++++++ gnu/usr.bin/grep/grep.1 | 3 +++ gnu/usr.bin/grep/grep.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/gnu/usr.bin/grep/Makefile b/gnu/usr.bin/grep/Makefile index b1875fedabe5..5f6ecdd443f9 100644 --- a/gnu/usr.bin/grep/Makefile +++ b/gnu/usr.bin/grep/Makefile @@ -13,7 +13,14 @@ LINKS+= ${BINDIR}/grep ${BINDIR}/egrep \ MLINKS= grep.1 egrep.1 grep.1 fgrep.1 DPADD+= ${LIBGNUREGEX} +DPADD+= ${LIBBZ2} LDADD+= -lgnuregex +LDADD+= -lbz2 + +LINKS+= ${BINDIR}/grep ${BINDIR}/bzgrep \ + ${BINDIR}/grep ${BINDIR}/bzegrep \ + ${BINDIR}/grep ${BINDIR}/bzfgrep +MLINKS+=grep.1 bzgrep.1 grep.1 bzegrep.1 grep.1 bzfgrep.1 .if defined(GREP_LIBZ) && !empty(GREP_LIBZ) LDADD+= -lz diff --git a/gnu/usr.bin/grep/grep.1 b/gnu/usr.bin/grep/grep.1 index 4045217db7bb..2bd98cb011a5 100644 --- a/gnu/usr.bin/grep/grep.1 +++ b/gnu/usr.bin/grep/grep.1 @@ -351,6 +351,9 @@ even those that contain newline characters. .BR \-Z ", " \-\^\-decompress Decompress the input data before searching. This option is only available if compiled with zlib(3) library. +.BR \-J ", " \-\^\-bz2decompress +Decompress the bzip2ed input data before searching. +This option is only available if compiled with zlib(3) library. .SH "REGULAR EXPRESSIONS" A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic diff --git a/gnu/usr.bin/grep/grep.c b/gnu/usr.bin/grep/grep.c index e021824531f1..f34259938fce 100644 --- a/gnu/usr.bin/grep/grep.c +++ b/gnu/usr.bin/grep/grep.c @@ -98,6 +98,7 @@ static struct option long_options[] = {"mmap", no_argument, &mmap_option, 1}, {"no-filename", no_argument, NULL, 'h'}, {"no-messages", no_argument, NULL, 's'}, + {"bz2decompress", no_argument, NULL, 'J'}, #if HAVE_LIBZ > 0 {"decompress", no_argument, NULL, 'Z'}, {"null", no_argument, &filename_mask, 0}, @@ -248,6 +249,9 @@ static int bufmapped; /* True if buffer is memory-mapped. */ static off_t initial_bufoffset; /* Initial value of bufoffset. */ #endif +#include +static BZFILE* bzbufdesc; /* libbz2 file handle. */ +static int BZflag; /* uncompress before searching. */ #if HAVE_LIBZ > 0 #include static gzFile gzbufdesc; /* zlib file descriptor. */ @@ -310,6 +314,12 @@ reset (int fd, char const *file, struct stats *stats) || ! (buffer = page_alloc (bufalloc + 1, &ubuffer))) fatal (_("memory exhausted"), 0); } + if (BZflag) + { + bzbufdesc = BZ2_bzdopen(fd, "r"); + if (bzbufdesc == NULL) + fatal(_("memory exhausted"), 0); + } #if HAVE_LIBZ > 0 if (Zflag) { @@ -330,6 +340,7 @@ reset (int fd, char const *file, struct stats *stats) if (directories == SKIP_DIRECTORIES && S_ISDIR (stats->stat.st_mode)) return 0; if ( + BZflag || #if HAVE_LIBZ > 0 Zflag || #endif @@ -479,6 +490,16 @@ fillbuf (size_t save, struct stats *stats) { ssize_t bytesread; do + if (BZflag) + { + bytesread = BZ2_bzread (bzbufdesc, buffer + bufsalloc, readsize); + /* gzread() will return "non-error" when given input that isn't + its type of compression. So we need to mimic that behavor + for the bzgrep case. */ + if (bytesread == -1) + bytesread = 0; + } + else #if HAVE_LIBZ > 0 if (Zflag) bytesread = gzread (gzbufdesc, buffer + bufsalloc, readsize); @@ -726,6 +747,9 @@ grep (int fd, char const *file, struct stats *stats) { /* Close fd now, so that we don't open a lot of file descriptors when we recurse deeply. */ + if (BZflag) + BZ2_bzclose(bzbufdesc); + else #if HAVE_LIBZ > 0 if (Zflag) gzclose(gzbufdesc); @@ -899,6 +923,9 @@ grepfile (char const *file, struct stats *stats) if (list_files == 1 - 2 * status) printf ("%s%c", filename, '\n' & filename_mask); + if (BZflag) + BZ2_bzclose(bzbufdesc); + else #if HAVE_LIBZ > 0 if (Zflag) gzclose(gzbufdesc); @@ -1008,6 +1035,7 @@ Miscellaneous:\n\ -v, --invert-match select non-matching lines\n\ -V, --version print version information and exit\n\ --help display this help and exit\n\ + -J, --bz2decompress decompress bzip2'ed input before searching\n\ -Z, --decompress decompress input before searching (HAVE_LIBZ=1)\n\ --mmap use memory-mapped input if possible\n")); printf (_("\ @@ -1180,6 +1208,10 @@ main (int argc, char **argv) ++prog; } #endif + if (prog[0] == 'b') { + BZflag = 1; + ++prog; + } #if defined(__MSDOS__) || defined(_WIN32) /* DOS and MS-Windows use backslashes as directory separators, and usually @@ -1288,6 +1320,9 @@ main (int argc, char **argv) case 'I': binary_files = WITHOUT_MATCH_BINARY_FILES; break; + case 'J': + BZflag = 1; + break; case 'U': #if O_BINARY dos_use_file_type = DOS_BINARY;