From owner-p4-projects@FreeBSD.ORG Sun Jul 13 15:41:22 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id A00D71065677; Sun, 13 Jul 2008 15:41:22 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 63A18106566C for ; Sun, 13 Jul 2008 15:41:22 +0000 (UTC) (envelope-from gabor@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 4C3B98FC1E for ; Sun, 13 Jul 2008 15:41:22 +0000 (UTC) (envelope-from gabor@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id m6DFfMUF004024 for ; Sun, 13 Jul 2008 15:41:22 GMT (envelope-from gabor@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.2/8.14.1/Submit) id m6DFfMpE004022 for perforce@freebsd.org; Sun, 13 Jul 2008 15:41:22 GMT (envelope-from gabor@freebsd.org) Date: Sun, 13 Jul 2008 15:41:22 GMT Message-Id: <200807131541.m6DFfMpE004022@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to gabor@freebsd.org using -f From: Gabor Kovesdan To: Perforce Change Reviews Cc: Subject: PERFORCE change 145158 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Jul 2008 15:41:22 -0000 http://perforce.freebsd.org/chv.cgi?CH=145158 Change 145158 by gabor@gabor_server on 2008/07/13 15:40:33 - Restore the old gzip and bzip2 support, which was better. Affected files ... .. //depot/projects/soc2008/gabor_textproc/grep/file.c#16 edit .. //depot/projects/soc2008/gabor_textproc/grep/grep.c#65 edit .. //depot/projects/soc2008/gabor_textproc/grep/grep.h#37 edit .. //depot/projects/soc2008/gabor_textproc/grep/util.c#59 edit Differences ... ==== //depot/projects/soc2008/gabor_textproc/grep/file.c#16 (text+ko) ==== @@ -57,8 +57,46 @@ static int binbufsiz; char *binbufptr; +static int bzerr; + #define iswbinary(ch) (!iswspace((ch)) && iswcntrl((ch))) +int +grep_fgetc(struct file *f) +{ + char c; + + switch (filebehave) { + case FILE_STDIO: + return (fgetc(f->f)); + case FILE_GZIP: + return (gzgetc(f->gzf)); + case FILE_BZIP: + BZ2_bzRead(&bzerr, f->bzf, &c, 1); + if (bzerr == BZ_STREAM_END) + return (-1); + else if (bzerr != BZ_SEQUENCE_ERROR && bzerr != BZ_OK) + errx(2, "Cannot read bzip2 compressed file"); + return (c); + } + return (-1); +} + +int +grep_feof(struct file *f) +{ + + switch (filebehave) { + case FILE_STDIO: + return (feof(f->f)); + case FILE_GZIP: + return (gzeof(f->gzf)); + case FILE_BZIP: + return (bzerr == BZ_STREAM_END); + } + return (1); +} + char * grep_fgetln(struct file *f, size_t *len) { @@ -69,9 +107,9 @@ if ((binbufptr == NULL) && (binbehave != BINFILE_TEXT)) { for (i = 0; i < (BUFSIZ * 4); i++) { - if (feof(f->f)) + if (grep_feof(f)) break; - ch = fgetc(f->f); + ch = grep_fgetc(f); binbuf[i] = ch; } binbufsiz = i; @@ -87,9 +125,9 @@ for (i = 0; ; i++) { if (binbufptr == &binbuf[binbufsiz]) { - if (feof(f->f)) + if (grep_feof(f)) break; - ch = fgetc(f->f); + ch = grep_fgetc(f); } else { ch = binbufptr[0]; binbufptr++; @@ -102,7 +140,7 @@ break; lnbuf[i] = ch; } - if (feof(f->f) && (i == 0)) + if (grep_feof(f) && (i == 0)) return NULL; *len = i; return (lnbuf); @@ -128,61 +166,44 @@ grep_open(char *path) { struct file *f; - char *templ; - int tempfd; snprintf(fname, sizeof fname, "%s", path); f = grep_malloc(sizeof *f); - if (Zflag || Jflag) { - templ = grep_malloc(sizeof(char) * 15); - strlcpy(templ, "/tmp/grep.XXXXXXXX", 14); - if ((tempfd = mkstemp(templ)) == -1) - err(2, NULL); - free(templ); - if (Zflag) { - gzFile *gzf; - char buf[BUFSIZ]; - int i; + switch (filebehave) { + case FILE_STDIO: + if ((f->f = fopen(path, "r")) != NULL) + return (f); + break; + case FILE_GZIP: + if ((f->gzf = gzopen(fname, "r")) != NULL) + return (f); + break; + case FILE_BZIP: + if ((f->bzf = BZ2_bzopen(fname, "r")) != NULL) + return (f); + break; + } - if ((gzf = gzopen(fname, "r")) == NULL) - err(2, NULL); - while ((i = gzread(gzf, buf, BUFSIZ)) > 0) { - write(tempfd, buf, BUFSIZ); - } - gzclose(gzf); - lseek(tempfd, 0L, SEEK_SET); - if ((f->f = fdopen(tempfd, "r")) != NULL) - return (f); - else - return (NULL); - } else { - BZFILE *bzf; - char buf[BUFSIZ]; - int bzerror; - FILE *file; - if ((file = fopen(fname, "r")) == NULL) - err(2, NULL); - if ((bzf = BZ2_bzReadOpen(&bzerror, file, 0, 0, NULL, 0)) == NULL) - err(2, NULL); - do { - BZ2_bzRead(&bzerror, bzf, buf, BUFSIZ); - write(tempfd, buf, BUFSIZ); - } while (bzerror == BZ_OK); - BZ2_bzReadClose(&bzerror, bzf); - fclose(file); - lseek(tempfd, 0L, SEEK_SET); - if ((f->f = fdopen(tempfd, "r")) != NULL) - return (f); - else - return (NULL); - } - } + free(f); + return (NULL); +} - if ((f->f = fopen(path, "r")) != NULL) - return (f); +void +grep_close(struct file *f) +{ + switch (filebehave) { + case FILE_STDIO: + fclose(f->f); + break; + case FILE_GZIP: + gzclose(f->gzf); + break; + case FILE_BZIP: + BZ2_bzclose(f->bzf); + break; + } free(f); - return (NULL); } ==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#65 (text+ko) ==== @@ -98,9 +98,7 @@ unsigned long long Aflag; /* -A x: print x lines trailing each match */ unsigned long long Bflag; /* -B x: print x lines leading each match */ int Hflag; /* -H: always print file name */ -int Jflag; /* -J: grep in bzipped file */ int Lflag; /* -L: only show names of files with no matches */ -int Zflag; /* -Z: grep in gzipped file */ int bflag; /* -b: show block numbers for each match */ int cflag; /* -c: only show a count of matching lines */ int hflag; /* -h: don't print filename headers */ @@ -124,6 +122,7 @@ int grepbehave = GREP_BASIC; int binbehave = BINFILE_BIN; +int filebehave = FILE_STDIO; int devbehave = DEV_GREP; int dirbehave = DIR_GREP; int linkbehave = LINK_GREP; @@ -305,7 +304,7 @@ grepbehave = GREP_BASIC; break; case 'z': - Zflag++; + filebehave = FILE_GZIP; switch(__progname[1]) { case 'e': grepbehave = GREP_EXTENDED; @@ -408,8 +407,7 @@ cflags |= REG_ICASE; break; case 'J': - Zflag = 0; - Jflag++; + filebehave = FILE_BZIP; break; case 'L': lflag = 0; @@ -474,8 +472,7 @@ xflag = 1; break; case 'Z': - Jflag = 0; - Zflag++; + filebehave = FILE_GZIP; break; case BIN_OPT: if (strcmp("binary", optarg) == 0) ==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#37 (text+ko) ==== @@ -26,8 +26,10 @@ * SUCH DAMAGE. */ +#include #include #include +#include #if defined(WITH_PCRE) #include @@ -55,6 +57,10 @@ #define BINFILE_SKIP 1 #define BINFILE_TEXT 2 +#define FILE_STDIO 0 +#define FILE_GZIP 1 +#define FILE_BZIP 2 + #define DIR_GREP 0 #define DIR_SKIP 1 #define DIR_RECURSE 2 @@ -72,6 +78,8 @@ int binary; FILE *f; struct mmfile *mmf; + gzFile *gzf; + BZFILE *bzf; }; struct str { @@ -86,13 +94,13 @@ extern int cflags, eflags; /* Command line flags */ -extern int Eflag, Fflag, Gflag, Hflag, Jflag, Lflag, Zflag, +extern int Eflag, Fflag, Gflag, Hflag, Lflag, bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag, qflag, sflag, vflag, wflag, xflag; extern int nullflag, exclflag, inclflag; extern unsigned long long Aflag, Bflag, mcount; extern char *color, *label; -extern int binbehave, devbehave, dirbehave, linkbehave; +extern int binbehave, filebehave, devbehave, dirbehave, linkbehave; extern int first, prev, matchall, patterns, epatterns, tail, notfound; extern char **pattern, **epattern; @@ -117,6 +125,9 @@ void clearqueue(void); /* file.c */ +void grep_close(struct file *f); struct file *grep_stdin_open(void); struct file *grep_open(char *path); +int grep_feof(struct file *f); +int grep_fgetc(struct file *f); char *grep_fgetln(struct file *f, size_t *len); ==== //depot/projects/soc2008/gabor_textproc/grep/util.c#59 (text+ko) ==== @@ -160,7 +160,7 @@ } if (f->binary && binbehave == BINFILE_SKIP) { - fclose(f->f); + grep_close(f); free(f); return (0); } @@ -196,7 +196,7 @@ } if (Bflag > 0) clearqueue(); - fclose(f->f); + grep_close(f); free(f); if (cflag) {