PERFORCE change 144045 for review

Gabor Kovesdan gabor at FreeBSD.org
Tue Jun 24 20:05:06 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=144045

Change 144045 by gabor at 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) {


More information about the p4-projects mailing list