Permalink
Browse files

Merge branch 'openbsd'

Conflicts:
	grep.c
	mmfile.c
  • Loading branch information...
2 parents 7bfdf54 + bfebaf1 commit 159fcd50e4a3a104cd322eb7bd62da76e3f27b1d @howardjp committed Nov 28, 2009
Showing with 1,009 additions and 414 deletions.
  1. +12 −6 Makefile
  2. +30 −21 binary.c
  3. +47 −39 file.c
  4. +249 −145 grep.1
  5. +246 −116 grep.c
  6. +31 −10 grep.h
  7. +14 −25 mmfile.c
  8. +4 −10 queue.c
  9. +376 −42 util.c
View
18 Makefile 100755 → 100644
@@ -1,16 +1,22 @@
-# $Id: Makefile,v 1.1 2002/09/14 16:30:18 howardjp Exp $
+# $OpenBSD: Makefile,v 1.5 2003/06/23 07:52:18 deraadt Exp $
PROG= grep
SRCS= binary.c file.c grep.c mmfile.c queue.c util.c
-LINKS= ${BINDIR}/grep ${BINDIR}/egrep \
+LINKS= ${BINDIR}/grep ${BINDIR}/egrep \
${BINDIR}/grep ${BINDIR}/fgrep \
- ${BINDIR}/grep ${BINDIR}/zgrep
+ ${BINDIR}/grep ${BINDIR}/zgrep \
+ ${BINDIR}/grep ${BINDIR}/zegrep \
+ ${BINDIR}/grep ${BINDIR}/zfgrep \
+
MLINKS= grep.1 egrep.1 \
grep.1 fgrep.1 \
- grep.1 zgrep.1
+ grep.1 zgrep.1 \
+ grep.1 zegrep.1 \
+ grep.1 zfgrep.1
-CFLAGS+= -I/usr/local/include -Wall -pedantic
+CFLAGS+= -Wall
-LDADD= -lz -L/usr/local/lib/ -liberty
+LDADD= -lz
+DPADD= ${LIBZ}
.include <bsd.prog.mk>
View
51 binary.c 100755 → 100644
@@ -1,3 +1,5 @@
+/* $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
* All rights reserved.
@@ -22,67 +24,74 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * $Id: binary.c,v 1.1 2002/09/14 16:30:18 howardjp Exp $
*/
#include <ctype.h>
+#include <err.h>
#include <stdio.h>
#include <zlib.h>
#include "grep.h"
-#define BUFFER_SIZE 32
+#define isbinary(ch) (!isprint((ch)) && !isspace((ch)) && (ch) != '\b')
int
bin_file(FILE *f)
{
- char buf[BUFFER_SIZE];
- int i, m;
+ char buf[BUFSIZ];
+ size_t i, m;
+ int ret = 0;
- if (fseek(f, SEEK_SET, 0) == -1)
+ if (fseek(f, 0L, SEEK_SET) == -1)
return 0;
- if ((m = (int)fread(buf, 1, BUFFER_SIZE, f)) == 0)
+ if ((m = fread(buf, 1, BUFSIZ, f)) == 0)
return 0;
for (i = 0; i < m; i++)
- if (!isprint(buf[i]) && !isspace(buf[i]))
- return 1;
+ if (isbinary(buf[i])) {
+ ret = 1;
+ break;
+ }
rewind(f);
- return 0;
+ return ret;
}
+#ifndef NOZ
int
gzbin_file(gzFile *f)
{
- char buf[BUFFER_SIZE];
+ char buf[BUFSIZ];
int i, m;
+ int ret = 0;
- if (gzseek(f, SEEK_SET, 0) == -1)
+ if (gzseek(f, (z_off_t)0, SEEK_SET) == -1)
return 0;
- if ((m = (int)gzread(f, buf, BUFFER_SIZE)) == 0)
+ if ((m = gzread(f, buf, BUFSIZ)) <= 0)
return 0;
for (i = 0; i < m; i++)
- if (!isprint(buf[i]))
- return 1;
+ if (isbinary(buf[i])) {
+ ret = 1;
+ break;
+ }
- gzrewind(f);
- return 0;
+ if (gzrewind(f) != 0)
+ err(1, "gzbin_file");
+ return ret;
}
+#endif
int
mmbin_file(mmf_t *f)
{
int i;
-
+
/* XXX knows too much about mmf internals */
- for (i = 0; i < BUFFER_SIZE && i < f->len; i++)
- if (!isprint(f->base[i]))
+ for (i = 0; i < BUFSIZ && i < f->len; i++)
+ if (isbinary(f->base[i]))
return 1;
- mmrewind(f);
return 0;
}
View
86 file.c 100755 → 100644
@@ -1,3 +1,5 @@
+/* $OpenBSD: file.c,v 1.9 2006/02/09 09:54:46 otto Exp $ */
+
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
* All rights reserved.
@@ -22,8 +24,6 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * $Id: file.c,v 1.1 2002/09/14 16:30:18 howardjp Exp $
*/
#include <sys/param.h>
@@ -36,20 +36,24 @@
#include "grep.h"
static char fname[MAXPATHLEN];
+#ifndef NOZ
static char *lnbuf;
-static int lnbuflen;
+static size_t lnbuflen;
+#endif
#define FILE_STDIO 0
#define FILE_MMAP 1
#define FILE_GZIP 2
struct file {
int type;
+ int noseek;
FILE *f;
mmf_t *mmf;
gzFile *gzf;
};
+#ifndef NOZ
static char *
gzfgetln(gzFile *f, size_t *len)
{
@@ -64,19 +68,19 @@ gzfgetln(gzFile *f, size_t *len)
if (gzeof(f))
break;
-
+
gzerrstr = gzerror(f, &gzerr);
if (gzerr == Z_ERRNO)
- err(1, "%s", fname);
+ err(2, "%s", fname);
else
- errx(1, "%s: %s", fname, gzerrstr);
+ errx(2, "%s: %s", fname, gzerrstr);
}
- if (c == '\n')
- break;
if (n >= lnbuflen) {
lnbuflen *= 2;
lnbuf = grep_realloc(lnbuf, ++lnbuflen);
}
+ if (c == '\n')
+ break;
lnbuf[n] = c;
}
@@ -85,29 +89,35 @@ gzfgetln(gzFile *f, size_t *len)
*len = n;
return lnbuf;
}
+#endif
file_t *
grep_fdopen(int fd, char *mode)
{
file_t *f;
- if (fd == 0)
- sprintf(fname, "(standard input)");
+ if (fd == STDIN_FILENO)
+ snprintf(fname, sizeof fname, "(standard input)");
else
- sprintf(fname, "(fd %d)", fd);
-
+ snprintf(fname, sizeof fname, "(fd %d)", fd);
+
f = grep_malloc(sizeof *f);
-
+
+#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 {
+ } else
+#endif
+ {
f->type = FILE_STDIO;
+ f->noseek = isatty(fd);
if ((f->f = fdopen(fd, mode)) != NULL)
return f;
}
-
+
free(f);
return NULL;
}
@@ -117,15 +127,19 @@ grep_open(char *path, char *mode)
{
file_t *f;
- snprintf(fname, MAXPATHLEN, "%s", path);
-
+ snprintf(fname, sizeof fname, "%s", path);
+
f = grep_malloc(sizeof *f);
-
+ f->noseek = 0;
+
+#ifndef NOZ
if (Zflag) {
f->type = FILE_GZIP;
if ((f->gzf = gzopen(fname, mode)) != NULL)
return f;
- } else {
+ } else
+#endif
+ {
/* try mmap first; if it fails, try stdio */
if ((f->mmf = mmopen(fname, mode)) != NULL) {
f->type = FILE_MMAP;
@@ -135,40 +149,29 @@ grep_open(char *path, char *mode)
if ((f->f = fopen(path, mode)) != NULL)
return f;
}
-
+
free(f);
return NULL;
}
int
grep_bin_file(file_t *f)
{
+ if (f->noseek)
+ return 0;
+
switch (f->type) {
case FILE_STDIO:
return bin_file(f->f);
case FILE_MMAP:
return mmbin_file(f->mmf);
+#ifndef NOZ
case FILE_GZIP:
return gzbin_file(f->gzf);
+#endif
default:
/* can't happen */
- errx(1, "invalid file type");
- }
-}
-
-long
-grep_tell(file_t *f)
-{
- switch (f->type) {
- case FILE_STDIO:
- return ftell(f->f);
- case FILE_MMAP:
- return mmtell(f->mmf);
- case FILE_GZIP:
- return gztell(f->gzf);
- default:
- /* can't happen */
- errx(1, "invalid file type");
+ errx(2, "invalid file type");
}
}
@@ -180,11 +183,13 @@ grep_fgetln(file_t *f, size_t *l)
return fgetln(f->f, l);
case FILE_MMAP:
return mmfgetln(f->mmf, l);
+#ifndef NOZ
case FILE_GZIP:
return gzfgetln(f->gzf, l);
+#endif
default:
/* can't happen */
- errx(1, "invalid file type");
+ errx(2, "invalid file type");
}
}
@@ -198,11 +203,14 @@ grep_close(file_t *f)
case FILE_MMAP:
mmclose(f->mmf);
break;
+#ifndef NOZ
case FILE_GZIP:
gzclose(f->gzf);
break;
+#endif
default:
/* can't happen */
- errx(1, "invalid file type");
+ errx(2, "invalid file type");
}
+ free(f);
}
Oops, something went wrong.

0 comments on commit 159fcd5

Please sign in to comment.