1 /* $NetBSD: file.c,v 1.5 2011/02/16 18:35:39 joerg Exp $ */
3 /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */
6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
9 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
10 * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
41 #include <sys/types.h>
64 #define MAXBUFSIZ (32 * 1024)
67 static gzFile gzbufdesc;
69 static lzma_stream lstrm = LZMA_STREAM_INIT;
70 static lzma_action laction;
71 static uint8_t lin_buf[MAXBUFSIZ];
74 static BZFILE* bzbufdesc;
77 static unsigned char *buffer;
78 static unsigned char *bufpos;
82 static unsigned char *lnbuf;
83 static size_t lnbuflen;
86 grep_refill(struct file *f)
90 if (filebehave == FILE_MMAP)
96 if (filebehave == FILE_GZIP) {
97 nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
99 } else if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
102 nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
106 /* No problem, nr will be okay */
108 case BZ_DATA_ERROR_MAGIC:
110 * As opposed to gzread(), which simply returns the
111 * plain file data, if it is not in the correct
112 * compressed format, BZ2_bzRead() instead aborts.
114 * So, just restart at the beginning of the file again,
115 * and use plain reads from now on.
117 BZ2_bzReadClose(&bzerr, bzbufdesc);
119 if (lseek(f->fd, 0, SEEK_SET) == -1)
121 nr = read(f->fd, buffer, MAXBUFSIZ);
124 /* Make sure we exit with an error */
129 } else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
131 lstrm.next_out = buffer;
134 if (lstrm.avail_in == 0) {
135 lstrm.next_in = lin_buf;
136 nr = read(f->fd, lin_buf, MAXBUFSIZ);
141 laction = LZMA_FINISH;
146 ret = lzma_code(&lstrm, laction);
148 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
151 if (lstrm.avail_out == 0 || ret == LZMA_STREAM_END) {
152 bufrem = MAXBUFSIZ - lstrm.avail_out;
153 lstrm.next_out = buffer;
154 lstrm.avail_out = MAXBUFSIZ;
156 } while (bufrem == 0 && ret != LZMA_STREAM_END);
159 #endif /* WIHTOUT_LZMA */
161 nr = read(f->fd, buffer, MAXBUFSIZ);
171 grep_lnbufgrow(size_t newlen)
174 if (lnbuflen < newlen) {
175 lnbuf = grep_realloc(lnbuf, newlen);
183 grep_fgetln(struct file *f, size_t *lenp)
191 /* Fill the buffer, if necessary */
192 if (bufrem == 0 && grep_refill(f) != 0)
196 /* Return zero length to indicate EOF */
201 /* Look for a newline in the remaining part of the buffer */
202 if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
203 ++p; /* advance over newline */
212 /* We have to copy the current buffered data to the line buffer */
213 for (len = bufrem, off = 0; ; len += bufrem) {
214 /* Make sure there is room for more data */
215 if (grep_lnbufgrow(len + LNBUFBUMP))
217 memcpy(lnbuf + off, bufpos, len - off);
218 /* With FILE_MMAP, this is EOF; there's no more to refill */
219 if (filebehave == FILE_MMAP) {
224 /* Fetch more to try and find EOL/EOF */
225 if (grep_refill(f) != 0)
228 /* EOF: return partial line */
230 if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
232 /* got it: finish up the line (like code above) */
236 if (grep_lnbufgrow(len))
238 memcpy(lnbuf + off, bufpos, diff);
252 * Opens a file for processing.
255 grep_open(const char *path)
259 f = grep_malloc(sizeof *f);
260 memset(f, 0, sizeof *f);
262 /* Processing stdin implies --line-buffered. */
264 f->fd = STDIN_FILENO;
265 } else if ((f->fd = open(path, O_RDONLY)) == -1)
268 if (filebehave == FILE_MMAP) {
271 if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) ||
272 (!S_ISREG(st.st_mode)))
273 filebehave = FILE_STDIO;
275 int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
276 #ifdef MAP_PREFAULT_READ
277 flags |= MAP_PREFAULT_READ;
280 buffer = mmap(NULL, fsiz, PROT_READ, flags,
282 if (buffer == MAP_FAILED)
283 filebehave = FILE_STDIO;
287 madvise(buffer, st.st_size, MADV_SEQUENTIAL);
292 if ((buffer == NULL) || (buffer == MAP_FAILED))
293 buffer = grep_malloc(MAXBUFSIZ);
295 if (filebehave == FILE_GZIP &&
296 (gzbufdesc = gzdopen(f->fd, "r")) == NULL)
299 #ifndef WITHOUT_BZIP2
300 if (filebehave == FILE_BZIP &&
301 (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
305 else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
308 ret = (filebehave == FILE_XZ) ?
309 lzma_stream_decoder(&lstrm, UINT64_MAX,
311 lzma_alone_decoder(&lstrm, UINT64_MAX);
317 lstrm.avail_out = MAXBUFSIZ;
322 /* Fill read buffer, also catches errors early */
323 if (bufrem == 0 && grep_refill(f) != 0)
326 /* Check for binary stuff, if necessary */
327 if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
328 memchr(bufpos, '\0', bufrem) != NULL)
344 grep_close(struct file *f)
349 /* Reset read buffer and line buffer */
350 if (filebehave == FILE_MMAP) {
351 munmap(buffer, fsiz);