From afe335edb5ec5cb5b15074c5f300c5055f76f73b Mon Sep 17 00:00:00 2001 From: gabor Date: Tue, 11 Oct 2011 22:27:23 +0000 Subject: - Use getprogname() instead of __progname - Allow disabling bzip2 support with WITHOUT_BZIP2 - Fix handling patterns that start with a dot - Remove superfluous semicolon Approved by: delphij (mentor) --- usr.bin/grep/Makefile | 24 ++++++++++++++++-------- usr.bin/grep/file.c | 20 +++++++++++++++----- usr.bin/grep/grep.c | 15 +++++++++------ usr.bin/grep/regex/tre-fastmatch.c | 6 +++--- 4 files changed, 43 insertions(+), 22 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/grep/Makefile b/usr.bin/grep/Makefile index 75fad49..1bd34ff 100644 --- a/usr.bin/grep/Makefile +++ b/usr.bin/grep/Makefile @@ -26,9 +26,6 @@ LINKS= ${BINDIR}/grep ${BINDIR}/egrep \ ${BINDIR}/grep ${BINDIR}/zgrep \ ${BINDIR}/grep ${BINDIR}/zegrep \ ${BINDIR}/grep ${BINDIR}/zfgrep \ - ${BINDIR}/grep ${BINDIR}/bzgrep \ - ${BINDIR}/grep ${BINDIR}/bzegrep \ - ${BINDIR}/grep ${BINDIR}/bzfgrep \ ${BINDIR}/grep ${BINDIR}/xzgrep \ ${BINDIR}/grep ${BINDIR}/xzegrep \ ${BINDIR}/grep ${BINDIR}/xzfgrep \ @@ -41,9 +38,6 @@ MLINKS= grep.1 egrep.1 \ grep.1 zgrep.1 \ grep.1 zegrep.1 \ grep.1 zfgrep.1 \ - grep.1 bzgrep.1 \ - grep.1 bzegrep.1 \ - grep.1 bzfgrep.1 \ grep.1 xzgrep.1 \ grep.1 xzegrep.1 \ grep.1 xzfgrep.1 \ @@ -52,8 +46,22 @@ MLINKS= grep.1 egrep.1 \ grep.1 lzfgrep.1 .endif -LDADD= -lz -lbz2 -llzma -DPADD= ${LIBZ} ${LIBBZ2} ${LIBLZMA} +LDADD= -lz -llzma +DPADD= ${LIBZ} ${LIBLZMA} + +.if !defined(WITHOUT_BZIP2) +LDADD+= -lbz2 +DPADD+= ${LIBBZ2} + +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 +.else +CFLAGS+= -DWITHOUT_BZIP2 +.endif .if !defined(WITHOUT_GNU_COMPAT) CFLAGS+= -I/usr/include/gnu diff --git a/usr.bin/grep/file.c b/usr.bin/grep/file.c index 644c788..8cee2c0 100644 --- a/usr.bin/grep/file.c +++ b/usr.bin/grep/file.c @@ -38,7 +38,6 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include @@ -51,14 +50,20 @@ __FBSDID("$FreeBSD$"); #include #include +#ifndef WITHOUT_BZIP2 +#include +#endif + #include "grep.h" #define MAXBUFSIZ (32 * 1024) #define LNBUFBUMP 80 static gzFile gzbufdesc; -static BZFILE* bzbufdesc; static lzma_stream lstrm = LZMA_STREAM_INIT; +#ifndef WITHOUT_BZIP2 +static BZFILE* bzbufdesc; +#endif static unsigned char *buffer; static unsigned char *bufpos; @@ -72,7 +77,6 @@ static inline int grep_refill(struct file *f) { ssize_t nr; - int bzerr; if (filebehave == FILE_MMAP) return (0); @@ -80,9 +84,12 @@ grep_refill(struct file *f) bufpos = buffer; bufrem = 0; - if (filebehave == FILE_GZIP) + if (filebehave == FILE_GZIP) { nr = gzread(gzbufdesc, buffer, MAXBUFSIZ); - else if (filebehave == FILE_BZIP && bzbufdesc != NULL) { +#ifndef WITHOUT_BZIP2 + } else if (filebehave == FILE_BZIP && bzbufdesc != NULL) { + int bzerr; + nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ); switch (bzerr) { case BZ_OK: @@ -108,6 +115,7 @@ grep_refill(struct file *f) /* Make sure we exit with an error */ nr = -1; } +#endif } else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) { lzma_action action = LZMA_RUN; uint8_t in_buf[MAXBUFSIZ]; @@ -271,9 +279,11 @@ grep_open(const char *path) (gzbufdesc = gzdopen(f->fd, "r")) == NULL) goto error2; +#ifndef WITHOUT_BZIP2 if (filebehave == FILE_BZIP && (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL) goto error2; +#endif /* Fill read buffer, also catches errors early */ if (bufrem == 0 && grep_refill(f) != 0) diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index e0f1d71..288df90 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -150,15 +150,13 @@ bool prev; /* flag whether or not the previous line matched */ int tail; /* lines left to print */ bool notfound; /* file not found */ -extern char *__progname; - /* * Prints usage information and returns 2. */ static void usage(void) { - fprintf(stderr, getstr(4), __progname); + fprintf(stderr, getstr(4), getprogname()); fprintf(stderr, "%s", getstr(5)); fprintf(stderr, "%s", getstr(5)); fprintf(stderr, "%s", getstr(6)); @@ -332,7 +330,8 @@ int main(int argc, char *argv[]) { char **aargv, **eargv, *eopts; - char *pn, *ep; + char *ep; + const char *pn; unsigned long long l; unsigned int aargc, eargc, i; int c, lastc, needpattern, newarg, prevoptind; @@ -346,7 +345,7 @@ main(int argc, char *argv[]) /* Check what is the program name of the binary. In this way we can have all the funcionalities in one binary without the need of scripting and using ugly hacks. */ - pn = __progname; + pn = getprogname(); if (pn[0] == 'b' && pn[1] == 'z') { filebehave = FILE_BZIP; pn += 2; @@ -508,6 +507,10 @@ main(int argc, char *argv[]) cflags |= REG_ICASE; break; case 'J': +#ifdef WITHOUT_BZIP2 + errno = EOPNOTSUPP; + err(2, "bzip2 support was disabled at compile-time"); +#endif filebehave = FILE_BZIP; break; case 'L': @@ -568,7 +571,7 @@ main(int argc, char *argv[]) filebehave = FILE_MMAP; break; case 'V': - printf(getstr(9), __progname, VERSION); + printf(getstr(9), getprogname(), VERSION); exit(0); case 'v': vflag = true; diff --git a/usr.bin/grep/regex/tre-fastmatch.c b/usr.bin/grep/regex/tre-fastmatch.c index e6a35ff..6f1aec6 100644 --- a/usr.bin/grep/regex/tre-fastmatch.c +++ b/usr.bin/grep/regex/tre-fastmatch.c @@ -1,4 +1,4 @@ -/* $FreeBSD$ */ +/* $FreeBSD$ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -548,7 +548,7 @@ tre_compile_fast(fastmatch_t *fg, const tre_char_t *pat, size_t n, int cflags) { tre_char_t *tmp; - size_t pos = 0, hasdot = 0, whasdot = 0;; + size_t pos = 0, hasdot = 0, whasdot = 0; ssize_t firstdot = -1, wfirstdot = -1; bool escaped = false; bool *_escmap = NULL; @@ -694,7 +694,7 @@ badpat: return REG_BADPAT; } - fg->hasdot = whasdot; + fg->hasdot = wfirstdot > -1; /* * The pattern has been processed and copied to tmp as a literal string -- cgit v1.1