1b66a823bSGabor Kovesdan /* $NetBSD: file.c,v 1.5 2011/02/16 18:35:39 joerg Exp $ */ 2b66a823bSGabor Kovesdan /* $FreeBSD$ */ 34dc88ebeSGabor Kovesdan /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */ 44dc88ebeSGabor Kovesdan 54dc88ebeSGabor Kovesdan /*- 6a0ef9ad6SDag-Erling Smørgrav * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav 73ed1008bSGabor Kovesdan * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org> 83ed1008bSGabor Kovesdan * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com> 94dc88ebeSGabor Kovesdan * All rights reserved. 104dc88ebeSGabor Kovesdan * 114dc88ebeSGabor Kovesdan * Redistribution and use in source and binary forms, with or without 124dc88ebeSGabor Kovesdan * modification, are permitted provided that the following conditions 134dc88ebeSGabor Kovesdan * are met: 144dc88ebeSGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 154dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer. 164dc88ebeSGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 174dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 184dc88ebeSGabor Kovesdan * documentation and/or other materials provided with the distribution. 194dc88ebeSGabor Kovesdan * 204dc88ebeSGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 214dc88ebeSGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224dc88ebeSGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234dc88ebeSGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 244dc88ebeSGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254dc88ebeSGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264dc88ebeSGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274dc88ebeSGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284dc88ebeSGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294dc88ebeSGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304dc88ebeSGabor Kovesdan * SUCH DAMAGE. 314dc88ebeSGabor Kovesdan */ 324dc88ebeSGabor Kovesdan 334dc88ebeSGabor Kovesdan #include <sys/cdefs.h> 344dc88ebeSGabor Kovesdan __FBSDID("$FreeBSD$"); 354dc88ebeSGabor Kovesdan 364dc88ebeSGabor Kovesdan #include <sys/param.h> 37*f20f6f3fSGabor Kovesdan #include <sys/mman.h> 384dc88ebeSGabor Kovesdan #include <sys/stat.h> 39*f20f6f3fSGabor Kovesdan #include <sys/types.h> 404dc88ebeSGabor Kovesdan 414dc88ebeSGabor Kovesdan #include <bzlib.h> 424dc88ebeSGabor Kovesdan #include <err.h> 434dc88ebeSGabor Kovesdan #include <errno.h> 443ed1008bSGabor Kovesdan #include <fcntl.h> 45*f20f6f3fSGabor Kovesdan #include <lzma.h> 463ed1008bSGabor Kovesdan #include <stddef.h> 474dc88ebeSGabor Kovesdan #include <stdlib.h> 484dc88ebeSGabor Kovesdan #include <string.h> 494dc88ebeSGabor Kovesdan #include <unistd.h> 504dc88ebeSGabor Kovesdan #include <wchar.h> 514dc88ebeSGabor Kovesdan #include <wctype.h> 524dc88ebeSGabor Kovesdan #include <zlib.h> 534dc88ebeSGabor Kovesdan 544dc88ebeSGabor Kovesdan #include "grep.h" 554dc88ebeSGabor Kovesdan 563ed1008bSGabor Kovesdan #define MAXBUFSIZ (32 * 1024) 573ed1008bSGabor Kovesdan #define LNBUFBUMP 80 584dc88ebeSGabor Kovesdan 593ed1008bSGabor Kovesdan static gzFile gzbufdesc; 603ed1008bSGabor Kovesdan static BZFILE* bzbufdesc; 61*f20f6f3fSGabor Kovesdan static lzma_stream lstrm = LZMA_STREAM_INIT; 624dc88ebeSGabor Kovesdan 63*f20f6f3fSGabor Kovesdan static unsigned char *buffer; 643ed1008bSGabor Kovesdan static unsigned char *bufpos; 653ed1008bSGabor Kovesdan static size_t bufrem; 66*f20f6f3fSGabor Kovesdan static size_t fsiz; 673ed1008bSGabor Kovesdan 683ed1008bSGabor Kovesdan static unsigned char *lnbuf; 694dc88ebeSGabor Kovesdan static size_t lnbuflen; 704dc88ebeSGabor Kovesdan 7159218eb7SGabor Kovesdan static inline int 723ed1008bSGabor Kovesdan grep_refill(struct file *f) 734dc88ebeSGabor Kovesdan { 743ed1008bSGabor Kovesdan ssize_t nr; 753ed1008bSGabor Kovesdan int bzerr; 764dc88ebeSGabor Kovesdan 77*f20f6f3fSGabor Kovesdan if (filebehave == FILE_MMAP) 78*f20f6f3fSGabor Kovesdan return (0); 79*f20f6f3fSGabor Kovesdan 803ed1008bSGabor Kovesdan bufpos = buffer; 813ed1008bSGabor Kovesdan bufrem = 0; 824dc88ebeSGabor Kovesdan 833ed1008bSGabor Kovesdan if (filebehave == FILE_GZIP) 843ed1008bSGabor Kovesdan nr = gzread(gzbufdesc, buffer, MAXBUFSIZ); 853ed1008bSGabor Kovesdan else if (filebehave == FILE_BZIP && bzbufdesc != NULL) { 863ed1008bSGabor Kovesdan nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ); 873ed1008bSGabor Kovesdan switch (bzerr) { 883ed1008bSGabor Kovesdan case BZ_OK: 893ed1008bSGabor Kovesdan case BZ_STREAM_END: 903ed1008bSGabor Kovesdan /* No problem, nr will be okay */ 913ed1008bSGabor Kovesdan break; 923ed1008bSGabor Kovesdan case BZ_DATA_ERROR_MAGIC: 934dc88ebeSGabor Kovesdan /* 943ed1008bSGabor Kovesdan * As opposed to gzread(), which simply returns the 953ed1008bSGabor Kovesdan * plain file data, if it is not in the correct 963ed1008bSGabor Kovesdan * compressed format, BZ2_bzRead() instead aborts. 973ed1008bSGabor Kovesdan * 983ed1008bSGabor Kovesdan * So, just restart at the beginning of the file again, 993ed1008bSGabor Kovesdan * and use plain reads from now on. 1004dc88ebeSGabor Kovesdan */ 1013ed1008bSGabor Kovesdan BZ2_bzReadClose(&bzerr, bzbufdesc); 1023ed1008bSGabor Kovesdan bzbufdesc = NULL; 1033ed1008bSGabor Kovesdan if (lseek(f->fd, 0, SEEK_SET) == -1) 1043ed1008bSGabor Kovesdan return (-1); 1053ed1008bSGabor Kovesdan nr = read(f->fd, buffer, MAXBUFSIZ); 1064dc88ebeSGabor Kovesdan break; 1073ed1008bSGabor Kovesdan default: 1083ed1008bSGabor Kovesdan /* Make sure we exit with an error */ 1093ed1008bSGabor Kovesdan nr = -1; 1104dc88ebeSGabor Kovesdan } 111*f20f6f3fSGabor Kovesdan } else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) { 112*f20f6f3fSGabor Kovesdan lzma_action action = LZMA_RUN; 113*f20f6f3fSGabor Kovesdan uint8_t in_buf[MAXBUFSIZ]; 114*f20f6f3fSGabor Kovesdan lzma_ret ret; 115*f20f6f3fSGabor Kovesdan 116*f20f6f3fSGabor Kovesdan ret = (filebehave == FILE_XZ) ? 117*f20f6f3fSGabor Kovesdan lzma_stream_decoder(&lstrm, UINT64_MAX, 118*f20f6f3fSGabor Kovesdan LZMA_CONCATENATED) : 119*f20f6f3fSGabor Kovesdan lzma_alone_decoder(&lstrm, UINT64_MAX); 120*f20f6f3fSGabor Kovesdan 121*f20f6f3fSGabor Kovesdan if (ret != LZMA_OK) 122*f20f6f3fSGabor Kovesdan return (-1); 123*f20f6f3fSGabor Kovesdan 124*f20f6f3fSGabor Kovesdan lstrm.next_out = buffer; 125*f20f6f3fSGabor Kovesdan lstrm.avail_out = MAXBUFSIZ; 126*f20f6f3fSGabor Kovesdan lstrm.next_in = in_buf; 127*f20f6f3fSGabor Kovesdan nr = read(f->fd, in_buf, MAXBUFSIZ); 128*f20f6f3fSGabor Kovesdan 129*f20f6f3fSGabor Kovesdan if (nr < 0) 130*f20f6f3fSGabor Kovesdan return (-1); 131*f20f6f3fSGabor Kovesdan else if (nr == 0) 132*f20f6f3fSGabor Kovesdan action = LZMA_FINISH; 133*f20f6f3fSGabor Kovesdan 134*f20f6f3fSGabor Kovesdan lstrm.avail_in = nr; 135*f20f6f3fSGabor Kovesdan ret = lzma_code(&lstrm, action); 136*f20f6f3fSGabor Kovesdan 137*f20f6f3fSGabor Kovesdan if (ret != LZMA_OK && ret != LZMA_STREAM_END) 138*f20f6f3fSGabor Kovesdan return (-1); 139*f20f6f3fSGabor Kovesdan bufrem = MAXBUFSIZ - lstrm.avail_out; 140*f20f6f3fSGabor Kovesdan return (0); 1414dc88ebeSGabor Kovesdan } else 1423ed1008bSGabor Kovesdan nr = read(f->fd, buffer, MAXBUFSIZ); 1433ed1008bSGabor Kovesdan 1443ed1008bSGabor Kovesdan if (nr < 0) 1453ed1008bSGabor Kovesdan return (-1); 1463ed1008bSGabor Kovesdan 1473ed1008bSGabor Kovesdan bufrem = nr; 1483ed1008bSGabor Kovesdan return (0); 1494dc88ebeSGabor Kovesdan } 1504dc88ebeSGabor Kovesdan 1513ed1008bSGabor Kovesdan static inline int 1523ed1008bSGabor Kovesdan grep_lnbufgrow(size_t newlen) 1534dc88ebeSGabor Kovesdan { 1544dc88ebeSGabor Kovesdan 1553ed1008bSGabor Kovesdan if (lnbuflen < newlen) { 1563ed1008bSGabor Kovesdan lnbuf = grep_realloc(lnbuf, newlen); 1573ed1008bSGabor Kovesdan lnbuflen = newlen; 1584dc88ebeSGabor Kovesdan } 1594dc88ebeSGabor Kovesdan 1603ed1008bSGabor Kovesdan return (0); 1613ed1008bSGabor Kovesdan } 1623ed1008bSGabor Kovesdan 1633ed1008bSGabor Kovesdan char * 1643ed1008bSGabor Kovesdan grep_fgetln(struct file *f, size_t *lenp) 1653ed1008bSGabor Kovesdan { 1663ed1008bSGabor Kovesdan unsigned char *p; 1673ed1008bSGabor Kovesdan char *ret; 1683ed1008bSGabor Kovesdan size_t len; 1693ed1008bSGabor Kovesdan size_t off; 1703ed1008bSGabor Kovesdan ptrdiff_t diff; 1713ed1008bSGabor Kovesdan 1723ed1008bSGabor Kovesdan /* Fill the buffer, if necessary */ 1733ed1008bSGabor Kovesdan if (bufrem == 0 && grep_refill(f) != 0) 1743ed1008bSGabor Kovesdan goto error; 1753ed1008bSGabor Kovesdan 1763ed1008bSGabor Kovesdan if (bufrem == 0) { 1773ed1008bSGabor Kovesdan /* Return zero length to indicate EOF */ 1783ed1008bSGabor Kovesdan *lenp = 0; 1793ed1008bSGabor Kovesdan return (bufpos); 1803ed1008bSGabor Kovesdan } 1813ed1008bSGabor Kovesdan 1823ed1008bSGabor Kovesdan /* Look for a newline in the remaining part of the buffer */ 1833ed1008bSGabor Kovesdan if ((p = memchr(bufpos, '\n', bufrem)) != NULL) { 1843ed1008bSGabor Kovesdan ++p; /* advance over newline */ 1853ed1008bSGabor Kovesdan ret = bufpos; 1863ed1008bSGabor Kovesdan len = p - bufpos; 1873ed1008bSGabor Kovesdan bufrem -= len; 1883ed1008bSGabor Kovesdan bufpos = p; 1893ed1008bSGabor Kovesdan *lenp = len; 1903ed1008bSGabor Kovesdan return (ret); 1913ed1008bSGabor Kovesdan } 1923ed1008bSGabor Kovesdan 1933ed1008bSGabor Kovesdan /* We have to copy the current buffered data to the line buffer */ 1943ed1008bSGabor Kovesdan for (len = bufrem, off = 0; ; len += bufrem) { 1953ed1008bSGabor Kovesdan /* Make sure there is room for more data */ 1963ed1008bSGabor Kovesdan if (grep_lnbufgrow(len + LNBUFBUMP)) 1973ed1008bSGabor Kovesdan goto error; 1983ed1008bSGabor Kovesdan memcpy(lnbuf + off, bufpos, len - off); 1993ed1008bSGabor Kovesdan off = len; 2003ed1008bSGabor Kovesdan if (grep_refill(f) != 0) 2013ed1008bSGabor Kovesdan goto error; 2023ed1008bSGabor Kovesdan if (bufrem == 0) 2033ed1008bSGabor Kovesdan /* EOF: return partial line */ 2043ed1008bSGabor Kovesdan break; 2053ed1008bSGabor Kovesdan if ((p = memchr(bufpos, '\n', bufrem)) == NULL) 2063ed1008bSGabor Kovesdan continue; 2073ed1008bSGabor Kovesdan /* got it: finish up the line (like code above) */ 2083ed1008bSGabor Kovesdan ++p; 2093ed1008bSGabor Kovesdan diff = p - bufpos; 2103ed1008bSGabor Kovesdan len += diff; 2113ed1008bSGabor Kovesdan if (grep_lnbufgrow(len)) 2123ed1008bSGabor Kovesdan goto error; 2133ed1008bSGabor Kovesdan memcpy(lnbuf + off, bufpos, diff); 2143ed1008bSGabor Kovesdan bufrem -= diff; 2153ed1008bSGabor Kovesdan bufpos = p; 2163ed1008bSGabor Kovesdan break; 2173ed1008bSGabor Kovesdan } 2183ed1008bSGabor Kovesdan *lenp = len; 2193ed1008bSGabor Kovesdan return (lnbuf); 2203ed1008bSGabor Kovesdan 2213ed1008bSGabor Kovesdan error: 2223ed1008bSGabor Kovesdan *lenp = 0; 2233ed1008bSGabor Kovesdan return (NULL); 2243ed1008bSGabor Kovesdan } 2253ed1008bSGabor Kovesdan 2264dc88ebeSGabor Kovesdan /* 2273ed1008bSGabor Kovesdan * Opens a file for processing. 2284dc88ebeSGabor Kovesdan */ 2294dc88ebeSGabor Kovesdan struct file * 2304dc88ebeSGabor Kovesdan grep_open(const char *path) 2314dc88ebeSGabor Kovesdan { 2324dc88ebeSGabor Kovesdan struct file *f; 2334dc88ebeSGabor Kovesdan 2344dc88ebeSGabor Kovesdan f = grep_malloc(sizeof *f); 2353ed1008bSGabor Kovesdan memset(f, 0, sizeof *f); 2363ed1008bSGabor Kovesdan if (path == NULL) { 2373ed1008bSGabor Kovesdan /* Processing stdin implies --line-buffered. */ 2383ed1008bSGabor Kovesdan lbflag = true; 2393ed1008bSGabor Kovesdan f->fd = STDIN_FILENO; 240*f20f6f3fSGabor Kovesdan } else if ((f->fd = open(path, O_RDONLY)) == -1) 241*f20f6f3fSGabor Kovesdan goto error1; 242*f20f6f3fSGabor Kovesdan 243*f20f6f3fSGabor Kovesdan if (filebehave == FILE_MMAP) { 244*f20f6f3fSGabor Kovesdan struct stat st; 245*f20f6f3fSGabor Kovesdan 246*f20f6f3fSGabor Kovesdan if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) || 247*f20f6f3fSGabor Kovesdan (!S_ISREG(st.st_mode))) 248*f20f6f3fSGabor Kovesdan filebehave = FILE_STDIO; 249*f20f6f3fSGabor Kovesdan else { 250*f20f6f3fSGabor Kovesdan int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC; 251*f20f6f3fSGabor Kovesdan #ifdef MAP_PREFAULT_READ 252*f20f6f3fSGabor Kovesdan flags |= MAP_PREFAULT_READ; 253*f20f6f3fSGabor Kovesdan #endif 254*f20f6f3fSGabor Kovesdan fsiz = st.st_size; 255*f20f6f3fSGabor Kovesdan buffer = mmap(NULL, fsiz, PROT_READ, flags, 256*f20f6f3fSGabor Kovesdan f->fd, (off_t)0); 257*f20f6f3fSGabor Kovesdan if (buffer == MAP_FAILED) 258*f20f6f3fSGabor Kovesdan filebehave = FILE_STDIO; 259*f20f6f3fSGabor Kovesdan else { 260*f20f6f3fSGabor Kovesdan bufrem = st.st_size; 261*f20f6f3fSGabor Kovesdan bufpos = buffer; 262*f20f6f3fSGabor Kovesdan madvise(buffer, st.st_size, MADV_SEQUENTIAL); 263*f20f6f3fSGabor Kovesdan } 264*f20f6f3fSGabor Kovesdan } 2654dc88ebeSGabor Kovesdan } 2664dc88ebeSGabor Kovesdan 267*f20f6f3fSGabor Kovesdan if ((buffer == NULL) || (buffer == MAP_FAILED)) 268*f20f6f3fSGabor Kovesdan buffer = grep_malloc(MAXBUFSIZ); 269*f20f6f3fSGabor Kovesdan 270*f20f6f3fSGabor Kovesdan if (filebehave == FILE_GZIP && 271*f20f6f3fSGabor Kovesdan (gzbufdesc = gzdopen(f->fd, "r")) == NULL) 272*f20f6f3fSGabor Kovesdan goto error2; 273*f20f6f3fSGabor Kovesdan 274*f20f6f3fSGabor Kovesdan if (filebehave == FILE_BZIP && 275*f20f6f3fSGabor Kovesdan (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL) 276*f20f6f3fSGabor Kovesdan goto error2; 277*f20f6f3fSGabor Kovesdan 278*f20f6f3fSGabor Kovesdan /* Fill read buffer, also catches errors early */ 279*f20f6f3fSGabor Kovesdan if (bufrem == 0 && grep_refill(f) != 0) 280*f20f6f3fSGabor Kovesdan goto error2; 281*f20f6f3fSGabor Kovesdan 282*f20f6f3fSGabor Kovesdan /* Check for binary stuff, if necessary */ 283*f20f6f3fSGabor Kovesdan if (binbehave != BINFILE_TEXT && memchr(bufpos, '\0', bufrem) != NULL) 284*f20f6f3fSGabor Kovesdan f->binary = true; 285*f20f6f3fSGabor Kovesdan 286*f20f6f3fSGabor Kovesdan return (f); 287*f20f6f3fSGabor Kovesdan 288*f20f6f3fSGabor Kovesdan error2: 289*f20f6f3fSGabor Kovesdan close(f->fd); 290*f20f6f3fSGabor Kovesdan error1: 291*f20f6f3fSGabor Kovesdan free(f); 292*f20f6f3fSGabor Kovesdan return (NULL); 2933ed1008bSGabor Kovesdan } 2943ed1008bSGabor Kovesdan 2954dc88ebeSGabor Kovesdan /* 2963ed1008bSGabor Kovesdan * Closes a file. 2974dc88ebeSGabor Kovesdan */ 2984dc88ebeSGabor Kovesdan void 2994dc88ebeSGabor Kovesdan grep_close(struct file *f) 3004dc88ebeSGabor Kovesdan { 3014dc88ebeSGabor Kovesdan 3023ed1008bSGabor Kovesdan close(f->fd); 3034dc88ebeSGabor Kovesdan 3043ed1008bSGabor Kovesdan /* Reset read buffer and line buffer */ 305*f20f6f3fSGabor Kovesdan if (filebehave == FILE_MMAP) { 306*f20f6f3fSGabor Kovesdan munmap(buffer, fsiz); 307*f20f6f3fSGabor Kovesdan buffer = NULL; 308*f20f6f3fSGabor Kovesdan } 3093ed1008bSGabor Kovesdan bufpos = buffer; 3103ed1008bSGabor Kovesdan bufrem = 0; 3113ed1008bSGabor Kovesdan 3123ed1008bSGabor Kovesdan free(lnbuf); 3133ed1008bSGabor Kovesdan lnbuf = NULL; 3143ed1008bSGabor Kovesdan lnbuflen = 0; 3154dc88ebeSGabor Kovesdan } 316