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>
55 #define MAXBUFSIZ (32 * 1024)
58 static unsigned char *buffer;
59 static unsigned char *bufpos;
63 static unsigned char *lnbuf;
64 static size_t lnbuflen;
67 grep_refill(struct file *f)
71 if (filebehave == FILE_MMAP)
77 nr = read(f->fd, buffer, MAXBUFSIZ);
86 grep_lnbufgrow(size_t newlen)
89 if (lnbuflen < newlen) {
90 lnbuf = grep_realloc(lnbuf, newlen);
98 grep_fgetln(struct file *f, struct parsec *pc)
106 /* Fill the buffer, if necessary */
107 if (bufrem == 0 && grep_refill(f) != 0)
111 /* Return zero length to indicate EOF */
116 /* Look for a newline in the remaining part of the buffer */
117 if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
118 ++p; /* advance over newline */
127 /* We have to copy the current buffered data to the line buffer */
128 for (len = bufrem, off = 0; ; len += bufrem) {
129 /* Make sure there is room for more data */
130 if (grep_lnbufgrow(len + LNBUFBUMP))
132 memcpy(lnbuf + off, bufpos, len - off);
133 /* With FILE_MMAP, this is EOF; there's no more to refill */
134 if (filebehave == FILE_MMAP) {
139 /* Fetch more to try and find EOL/EOF */
140 if (grep_refill(f) != 0)
143 /* EOF: return partial line */
145 if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
147 /* got it: finish up the line (like code above) */
151 if (grep_lnbufgrow(len))
153 memcpy(lnbuf + off, bufpos, diff);
167 * Opens a file for processing.
170 grep_open(const char *path)
174 f = grep_malloc(sizeof *f);
175 memset(f, 0, sizeof *f);
177 /* Processing stdin implies --line-buffered. */
179 f->fd = STDIN_FILENO;
180 } else if ((f->fd = open(path, O_RDONLY)) == -1)
183 if (filebehave == FILE_MMAP) {
186 if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) ||
187 (!S_ISREG(st.st_mode)))
188 filebehave = FILE_STDIO;
190 int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
191 #ifdef MAP_PREFAULT_READ
192 flags |= MAP_PREFAULT_READ;
195 buffer = mmap(NULL, fsiz, PROT_READ, flags,
197 if (buffer == MAP_FAILED)
198 filebehave = FILE_STDIO;
202 madvise(buffer, st.st_size, MADV_SEQUENTIAL);
207 if ((buffer == NULL) || (buffer == MAP_FAILED))
208 buffer = grep_malloc(MAXBUFSIZ);
210 /* Fill read buffer, also catches errors early */
211 if (bufrem == 0 && grep_refill(f) != 0)
214 /* Check for binary stuff, if necessary */
215 if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
216 memchr(bufpos, '\0', bufrem) != NULL)
232 grep_close(struct file *f)
237 /* Reset read buffer and line buffer */
238 if (filebehave == FILE_MMAP) {
239 munmap(buffer, fsiz);