Date: Mon, 29 Dec 2008 13:21:43 GMT From: Gabor Kovesdan <gabor@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 155414 for review Message-ID: <200812291321.mBTDLhAP074612@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=155414 Change 155414 by gabor@gabor_server on 2008/12/29 13:21:40 - Buffer 20% of the file size or maximum 16KB to perform binary check Affected files ... .. //depot/projects/soc2008/gabor_textproc/grep/file.c#37 edit Differences ... ==== //depot/projects/soc2008/gabor_textproc/grep/file.c#37 (text+ko) ==== @@ -36,6 +36,8 @@ #endif /* not lint */ #include <sys/param.h> +#include <sys/types.h> +#include <sys/stat.h> #include <bzlib.h> #include <err.h> @@ -52,10 +54,13 @@ static char fname[MAXPATHLEN]; /* file name */ +#define MAXBUFSIZ (16 * 1024) +#define PREREAD_M 0.2 + /* Some global variable for the buffering and reading. */ static char *lnbuf; static size_t lnbuflen; -static char binbuf[BUFSIZ * 8]; +static char *binbuf; static int binbufsiz; char *binbufptr; static int bzerr; @@ -119,12 +124,22 @@ { int i = 0; int ch = 0; + struct stat st; + size_t bufsiz; /* Fill in the buffer if it is empty. */ if (binbufptr == NULL) { + /* Only pre-read to the buffer if we need the binary check. */ if (binbehave != BINFILE_TEXT) { - for (; i < sizeof(binbuf) && !grep_feof(f); i++) + if (stat(fname, &st) != 0) + errx(2, NULL); + + bufsiz = (MAXBUFSIZ > (st.st_size * PREREAD_M)) ? (st.st_size / 2) : MAXBUFSIZ; + + binbuf = grep_malloc(sizeof(char) * bufsiz); + + for (; i < bufsiz && !grep_feof(f); i++) binbuf[i] = grep_fgetc(f); f->binary = memchr(binbuf, (filebehave != FILE_GZIP) ? '\0' : '\200', i - 1) != 0; } @@ -228,5 +243,6 @@ /* Reset read buffer for the file we are closing */ binbufptr = NULL; + free(binbuf); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200812291321.mBTDLhAP074612>