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)
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)
105 /* Fill the buffer, if necessary */
106 if (bufrem == 0 && grep_refill(f) != 0)
110 /* Return zero length to indicate EOF */
115 /* Look for a newline in the remaining part of the buffer */
116 if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
117 ++p; /* advance over newline */
119 if (grep_lnbufgrow(len + 1))
121 memcpy(lnbuf, bufpos, len);
129 /* We have to copy the current buffered data to the line buffer */
130 for (len = bufrem, off = 0; ; len += bufrem) {
131 /* Make sure there is room for more data */
132 if (grep_lnbufgrow(len + LNBUFBUMP))
134 memcpy(lnbuf + off, bufpos, len - off);
135 /* With FILE_MMAP, this is EOF; there's no more to refill */
136 if (filebehave == FILE_MMAP) {
141 /* Fetch more to try and find EOL/EOF */
142 if (grep_refill(f) != 0)
145 /* EOF: return partial line */
147 if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
149 /* got it: finish up the line (like code above) */
153 if (grep_lnbufgrow(len + 1))
155 memcpy(lnbuf + off, bufpos, diff);
170 * Opens a file for processing.
173 grep_open(const char *path)
177 f = grep_malloc(sizeof *f);
178 memset(f, 0, sizeof *f);
180 /* Processing stdin implies --line-buffered. */
182 f->fd = STDIN_FILENO;
183 } else if ((f->fd = open(path, O_RDONLY)) == -1)
186 if (filebehave == FILE_MMAP) {
189 if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) ||
190 (!S_ISREG(st.st_mode)))
191 filebehave = FILE_STDIO;
193 int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
194 #ifdef MAP_PREFAULT_READ
195 flags |= MAP_PREFAULT_READ;
198 buffer = mmap(NULL, fsiz, PROT_READ, flags,
200 if (buffer == MAP_FAILED)
201 filebehave = FILE_STDIO;
205 madvise(buffer, st.st_size, MADV_SEQUENTIAL);
210 if ((buffer == NULL) || (buffer == MAP_FAILED))
211 buffer = grep_malloc(MAXBUFSIZ);
213 /* Fill read buffer, also catches errors early */
214 if (bufrem == 0 && grep_refill(f) != 0)
217 /* Check for binary stuff, if necessary */
218 if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
219 memchr(bufpos, '\0', bufrem) != NULL)
235 grep_close(struct file *f)
240 /* Reset read buffer and line buffer */
241 if (filebehave == FILE_MMAP) {
242 munmap(buffer, fsiz);