Date: Tue, 24 Jun 2008 20:05:05 GMT From: Gabor Kovesdan <gabor@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 144045 for review Message-ID: <200806242005.m5OK55RF029095@repoman.freebsd.org>
index | next in thread | raw e-mail
http://perforce.freebsd.org/chv.cgi?CH=144045 Change 144045 by gabor@gabor_server on 2008/06/24 20:04:34 - Implement pre-extract of gzip and bzip2 files Affected files ... .. //depot/projects/soc2008/gabor_textproc/grep/file.c#10 edit Differences ... ==== //depot/projects/soc2008/gabor_textproc/grep/file.c#10 (text+ko) ==== @@ -40,6 +40,7 @@ #include <err.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <zlib.h> @@ -49,8 +50,6 @@ #define FILE_STDIO 0 #define FILE_MMAP 1 -#define FILE_GZIP 2 -#define FILE_BZIP 3 struct file { int type; @@ -81,13 +80,61 @@ grep_open(char *path) { struct file *f; + char *templ; + int tempfd; snprintf(fname, sizeof fname, "%s", path); f = grep_malloc(sizeof *f); f->noseek = 0; -/* XXX: pre-extract gzip and bzip2 files */ + 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; + + 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); + f->type = FILE_STDIO; + 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); + f->type = FILE_STDIO; + if ((f->f = fdopen(tempfd, "r")) != NULL) + return (f); + else + return (NULL); + } + } /* try mmap first; if it fails, try stdio */ if ((f->mmf = mmopen(fname, "r")) != NULL) {help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200806242005.m5OK55RF029095>
