| From adf4f7a9bfdf3a8fe3a51ecedf358a368feb6069 Mon Sep 17 00:00:00 2001 |
| From: Avery Pennarun <apenwarr@gmail.com> |
| Date: Sat, 23 Feb 2013 00:22:01 -0500 |
| Subject: [PATCH] Make it compile on linux. |
| |
| --- |
| .gitignore | 3 +++ |
| Makefile | 17 ++++++++--------- |
| file.c | 10 +++++++++- |
| grep.c | 7 ++++--- |
| mmfile.c | 2 -- |
| util.c | 8 ++++++++ |
| 6 files changed, 32 insertions(+), 15 deletions(-) |
| create mode 100644 .gitignore |
| |
| diff --git a/.gitignore b/.gitignore |
| new file mode 100644 |
| index 0000000..f806a1a |
| --- /dev/null |
| +++ b/.gitignore |
| @@ -0,0 +1,3 @@ |
| +*~ |
| +*.o |
| +grep |
| diff --git a/Makefile b/Makefile |
| index 5c066d2..3752f46 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -15,13 +15,12 @@ MLINKS= grep.1 egrep.1 \ |
| grep.1 zegrep.1 \ |
| grep.1 zfgrep.1 |
| |
| -CFLAGS+= -Wall |
| +CFLAGS+= -Wall -DNOZ -DNOFTS -D_GNU_SOURCE -g |
| |
| -.if ${OPSYS} == "Minix" |
| -CPPFLAGS+= -DNOZ -D_POSIX_SOURCE -D_MINIX |
| -.include <minix.prog.mk> |
| -.else |
| -LDADD= -lz |
| -DPADD= ${LIBZ} |
| -.include <bsd.prog.mk> |
| -.endif |
| +all: grep |
| + |
| +grep: $(SRCS) |
| + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) |
| + |
| +clean: |
| + rm -f *~ grep *.o |
| diff --git a/file.c b/file.c |
| index 1787ac0..bdd48a7 100644 |
| --- a/file.c |
| +++ b/file.c |
| @@ -53,6 +53,8 @@ struct file { |
| #ifndef NOZ |
| gzFile *gzf; |
| #endif |
| + char *buf; |
| + size_t n; |
| }; |
| |
| #ifndef NOZ |
| @@ -104,6 +106,8 @@ grep_fdopen(int fd, char *mode) |
| snprintf(fname, sizeof fname, "(fd %d)", fd); |
| |
| f = grep_malloc(sizeof *f); |
| + f->buf = NULL; |
| + f->n = 0; |
| |
| #ifndef NOZ |
| if (Zflag) { |
| @@ -133,6 +137,8 @@ grep_open(char *path, char *mode) |
| |
| f = grep_malloc(sizeof *f); |
| f->noseek = 0; |
| + f->buf = NULL; |
| + f->n = 0; |
| |
| #ifndef NOZ |
| if (Zflag) { |
| @@ -182,7 +188,8 @@ grep_fgetln(file_t *f, size_t *l) |
| { |
| switch (f->type) { |
| case FILE_STDIO: |
| - return fgetln(f->f, l); |
| + *l = getline(&f->buf, &f->n, f->f); |
| + return (ssize_t)*l >= 0 ? f->buf : NULL; |
| case FILE_MMAP: |
| return mmfgetln(f->mmf, l); |
| #ifndef NOZ |
| @@ -198,6 +205,7 @@ grep_fgetln(file_t *f, size_t *l) |
| void |
| grep_close(file_t *f) |
| { |
| + free(f->buf); |
| switch (f->type) { |
| case FILE_STDIO: |
| fclose(f->f); |
| diff --git a/grep.c b/grep.c |
| index b316981..6b0431b 100755 |
| --- a/grep.c |
| +++ b/grep.c |
| @@ -218,13 +218,14 @@ static void |
| read_patterns(const char *fn) |
| { |
| FILE *f; |
| - char *line; |
| - size_t len; |
| + char *line = NULL; |
| + size_t n, len; |
| |
| if ((f = fopen(fn, "r")) == NULL) |
| err(2, "%s", fn); |
| - while ((line = fgetln(f, &len)) != NULL) |
| + while ((len = getline(&line, &n, f)) >= 0) |
| add_pattern(line, *line == '\n' ? 0 : len); |
| + free(line); |
| if (ferror(f)) |
| err(2, "%s", fn); |
| fclose(f); |
| diff --git a/mmfile.c b/mmfile.c |
| index aa1a28a..ddf915d 100644 |
| --- a/mmfile.c |
| +++ b/mmfile.c |
| @@ -53,8 +53,6 @@ mmopen(char *fn, char *mode) |
| goto ouch1; |
| if (fstat(mmf->fd, &st) == -1) |
| goto ouch2; |
| - if (st.st_size > SIZE_T_MAX) /* too big to mmap */ |
| - goto ouch2; |
| if (!S_ISREG(st.st_mode)) /* only mmap regular files */ |
| goto ouch2; |
| mmf->len = (size_t)st.st_size; |
| diff --git a/util.c b/util.c |
| index 028ddc6..fb9e18e 100644 |
| --- a/util.c |
| +++ b/util.c |
| @@ -32,7 +32,9 @@ |
| #include <ctype.h> |
| #include <err.h> |
| #include <errno.h> |
| +#ifndef NOFTS |
| #include <fts.h> |
| +#endif |
| #include <regex.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| @@ -54,6 +56,7 @@ static void grep_revstr(unsigned char *, int); |
| int |
| grep_tree(char **argv) |
| { |
| +#ifndef NOFTS |
| FTS *fts; |
| FTSENT *p; |
| int c, fts_flags; |
| @@ -90,6 +93,11 @@ grep_tree(char **argv) |
| err(2, "fts_read"); |
| |
| return c; |
| +#else |
| + errno = ENOTSUP; |
| + warn("recursive mode"); |
| + return 0; |
| +#endif |
| } |
| |
| int |
| -- |
| 1.8.1.5-x20-1 |
| |