diff options
author | 2019-01-23 23:00:54 +0000 | |
---|---|---|
committer | 2019-01-23 23:00:54 +0000 | |
commit | 3a21e4794751456cc453dc37b1a11d23c52ae4a0 (patch) | |
tree | 235cf9f62c4d1432cfaac7c07d10660fb5702bdc | |
parent | eliminate a ?: in witness mtx initializer by pushing the default one (diff) | |
download | wireguard-openbsd-3a21e4794751456cc453dc37b1a11d23c52ae4a0.tar.xz wireguard-openbsd-3a21e4794751456cc453dc37b1a11d23c52ae4a0.zip |
rework grep_open to be more careful about directories.
cleaner, but should be no functional change.
from Lauri Tirkkonen
-rw-r--r-- | usr.bin/grep/file.c | 69 | ||||
-rw-r--r-- | usr.bin/grep/grep.c | 4 | ||||
-rw-r--r-- | usr.bin/grep/grep.h | 9 | ||||
-rw-r--r-- | usr.bin/grep/mmfile.c | 26 | ||||
-rw-r--r-- | usr.bin/grep/util.c | 9 |
5 files changed, 53 insertions, 64 deletions
diff --git a/usr.bin/grep/file.c b/usr.bin/grep/file.c index 4b3c689e4ab..3a23b91df04 100644 --- a/usr.bin/grep/file.c +++ b/usr.bin/grep/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.13 2015/03/16 13:27:59 millert Exp $ */ +/* $OpenBSD: file.c,v 1.14 2019/01/23 23:00:54 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -26,7 +26,10 @@ * SUCH DAMAGE. */ +#include <sys/stat.h> #include <err.h> +#include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <zlib.h> @@ -90,68 +93,64 @@ gzfgetln(gzFile *f, size_t *len) #endif file_t * -grep_fdopen(int fd, char *mode) +grep_fdopen(int fd) { file_t *f; + struct stat sb; if (fd == STDIN_FILENO) snprintf(fname, sizeof fname, "(standard input)"); - else + else if (fname[0] == '\0') snprintf(fname, sizeof fname, "(fd %d)", fd); + if (fstat(fd, &sb) == -1) + return NULL; + if (S_ISDIR(sb.st_mode)) { + errno = EISDIR; + return NULL; + } + 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) + if ((f->gzf = gzdopen(fd, "r")) != NULL) return f; - } else + } #endif - { - f->type = FILE_STDIO; - f->noseek = isatty(fd); - if ((f->f = fdopen(fd, mode)) != NULL) - return f; + f->noseek = isatty(fd); +#ifndef SMALL + /* try mmap first; if it fails, try stdio */ + if (!f->noseek && (f->mmf = mmopen(fd, &sb)) != NULL) { + f->type = FILE_MMAP; + return f; } +#endif + f->type = FILE_STDIO; + if ((f->f = fdopen(fd, "r")) != NULL) + return f; free(f); return NULL; } file_t * -grep_open(char *path, char *mode) +grep_open(char *path) { file_t *f; + int fd; 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 -#endif - { -#ifndef SMALL - /* try mmap first; if it fails, try stdio */ - if ((f->mmf = mmopen(fname, mode)) != NULL) { - f->type = FILE_MMAP; - return f; - } -#endif - f->type = FILE_STDIO; - if ((f->f = fopen(path, mode)) != NULL) - return f; - } + if ((fd = open(fname, O_RDONLY)) == -1) + return NULL; - free(f); - return NULL; + f = grep_fdopen(fd); + if (f == NULL) + close(fd); + return f; } int diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 913cc97a0f3..fc0b7dc01c4 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.57 2017/12/10 09:17:24 jmc Exp $ */ +/* $OpenBSD: grep.c,v 1.58 2019/01/23 23:00:54 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -63,9 +63,7 @@ int Fflag; /* -F: interpret pattern as list of fixed strings */ int Hflag; /* -H: always print filename header */ int Lflag; /* -L: only show names of files with no matches */ int Rflag; /* -R: recursively search directory trees */ -#ifndef NOZ int Zflag; /* -Z: decompress input before processing */ -#endif int bflag; /* -b: show block numbers for each match */ int cflag; /* -c: only show a count of matching lines */ int hflag; /* -h: don't print filename headers */ diff --git a/usr.bin/grep/grep.h b/usr.bin/grep/grep.h index bbf7f8cd8c8..8deacded308 100644 --- a/usr.bin/grep/grep.h +++ b/usr.bin/grep/grep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.h,v 1.25 2017/12/09 18:38:37 pirofti Exp $ */ +/* $OpenBSD: grep.h,v 1.26 2019/01/23 23:00:54 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -27,6 +27,7 @@ */ #include <sys/types.h> +#include <sys/stat.h> #include <limits.h> #include <regex.h> @@ -106,7 +107,7 @@ typedef struct mmfile { char *base, *end, *ptr; } mmf_t; -mmf_t *mmopen(char *fn, char *mode); +mmf_t *mmopen(int fd, struct stat *sb); void mmclose(mmf_t *mmf); char *mmfgetln(mmf_t *mmf, size_t *l); @@ -114,8 +115,8 @@ char *mmfgetln(mmf_t *mmf, size_t *l); struct file; typedef struct file file_t; -file_t *grep_fdopen(int fd, char *mode); -file_t *grep_open(char *path, char *mode); +file_t *grep_fdopen(int fd); +file_t *grep_open(char *path); int grep_bin_file(file_t *f); char *grep_fgetln(file_t *f, size_t *l); void grep_close(file_t *f); diff --git a/usr.bin/grep/mmfile.c b/usr.bin/grep/mmfile.c index d122453429f..fa5a0ea7752 100644 --- a/usr.bin/grep/mmfile.c +++ b/usr.bin/grep/mmfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mmfile.c,v 1.17 2015/02/06 23:21:59 millert Exp $ */ +/* $OpenBSD: mmfile.c,v 1.18 2019/01/23 23:00:54 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -41,35 +41,23 @@ #define MAX_MAP_LEN 1048576 mmf_t * -mmopen(char *fn, char *mode) +mmopen(int fd, struct stat *st) { mmf_t *mmf; - struct stat st; - - if (*mode != 'r') - return NULL; mmf = grep_malloc(sizeof *mmf); - if ((mmf->fd = open(fn, O_RDONLY)) == -1) - goto ouch1; - if (fstat(mmf->fd, &st) == -1) - goto ouch2; - if (st.st_size > SIZE_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; + if (st->st_size > SIZE_MAX) /* too big to mmap */ + goto ouch; + mmf->len = (size_t)st->st_size; mmf->base = mmap(NULL, mmf->len, PROT_READ, MAP_PRIVATE, mmf->fd, (off_t)0); if (mmf->base == MAP_FAILED) - goto ouch2; + goto ouch; mmf->ptr = mmf->base; mmf->end = mmf->base + mmf->len; madvise(mmf->base, mmf->len, MADV_SEQUENTIAL); return mmf; -ouch2: - close(mmf->fd); -ouch1: +ouch: free(mmf); return NULL; } diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index f7c5407d3e0..bfd2bc3cef0 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.58 2017/12/09 18:38:37 pirofti Exp $ */ +/* $OpenBSD: util.c,v 1.59 2019/01/23 23:00:54 tedu Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -77,6 +77,7 @@ grep_tree(char **argv) if(!sflag) warnc(p->fts_errno, "%s", p->fts_path); break; + case FTS_D: case FTS_DP: break; default: @@ -101,11 +102,13 @@ procfile(char *fn) if (fn == NULL) { fn = "(standard input)"; - f = grep_fdopen(STDIN_FILENO, "r"); + f = grep_fdopen(STDIN_FILENO); } else { - f = grep_open(fn, "r"); + f = grep_open(fn); } if (f == NULL) { + if (errno == EISDIR) + return 0; file_err = 1; if (!sflag) warn("%s", fn); |