summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/grep/binary.c8
-rw-r--r--usr.bin/grep/file.c9
2 files changed, 13 insertions, 4 deletions
diff --git a/usr.bin/grep/binary.c b/usr.bin/grep/binary.c
index 1736a6bd1b8..fcc15419a02 100644
--- a/usr.bin/grep/binary.c
+++ b/usr.bin/grep/binary.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: binary.c,v 1.13 2004/09/15 22:35:36 deraadt Exp $ */
+/* $OpenBSD: binary.c,v 1.14 2005/02/07 08:47:18 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -27,6 +27,7 @@
*/
#include <ctype.h>
+#include <err.h>
#include <stdio.h>
#include <zlib.h>
@@ -41,7 +42,7 @@ bin_file(FILE *f)
int i, m;
int ret = 0;
- if (isatty(fileno(f)) || fseek(f, 0L, SEEK_SET) == -1)
+ if (fseek(f, 0L, SEEK_SET) == -1)
return 0;
if ((m = (int)fread(buf, 1, BUFSIZ, f)) == 0)
@@ -77,7 +78,8 @@ gzbin_file(gzFile *f)
break;
}
- gzrewind(f);
+ if (gzrewind(f) != 0)
+ err(1, "gzbin_file");
return ret;
}
#endif
diff --git a/usr.bin/grep/file.c b/usr.bin/grep/file.c
index 99892e6c5a8..a11fcfc259e 100644
--- a/usr.bin/grep/file.c
+++ b/usr.bin/grep/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.6 2005/02/02 06:17:17 tedu Exp $ */
+/* $OpenBSD: file.c,v 1.7 2005/02/07 08:47:18 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -45,6 +45,7 @@ static size_t lnbuflen;
struct file {
int type;
+ int noseek;
FILE *f;
mmf_t *mmf;
gzFile *gzf;
@@ -103,12 +104,14 @@ grep_fdopen(int fd, char *mode)
#ifndef NOZ
if (Zflag) {
f->type = FILE_GZIP;
+ f->noseek = lseek(fd, 0L, SEEK_SET) == -1;
if ((f->gzf = gzdopen(fd, mode)) != NULL)
return f;
} else
#endif
{
f->type = FILE_STDIO;
+ f->noseek = isatty(fd);
if ((f->f = fdopen(fd, mode)) != NULL)
return f;
}
@@ -125,6 +128,7 @@ grep_open(char *path, char *mode)
snprintf(fname, sizeof fname, "%s", path);
f = grep_malloc(sizeof *f);
+ f->noseek = 0;
#ifndef NOZ
if (Zflag) {
@@ -151,6 +155,9 @@ grep_open(char *path, char *mode)
int
grep_bin_file(file_t *f)
{
+ if (f->noseek)
+ return 0;
+
switch (f->type) {
case FILE_STDIO:
return bin_file(f->f);