1 /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */
4 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
5 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
6 * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/types.h>
52 #define MAXBUFSIZ (32 * 1024)
55 static gzFile gzbufdesc;
56 static BZFILE* bzbufdesc;
58 static unsigned char buffer[MAXBUFSIZ];
59 static unsigned char *bufpos;
62 static unsigned char *lnbuf;
63 static size_t lnbuflen;
66 grep_refill(struct file *f)
74 if (filebehave == FILE_GZIP)
75 nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
76 else if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
77 nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
81 /* No problem, nr will be okay */
83 case BZ_DATA_ERROR_MAGIC:
85 * As opposed to gzread(), which simply returns the
86 * plain file data, if it is not in the correct
87 * compressed format, BZ2_bzRead() instead aborts.
89 * So, just restart at the beginning of the file again,
90 * and use plain reads from now on.
92 BZ2_bzReadClose(&bzerr, bzbufdesc);
94 if (lseek(f->fd, 0, SEEK_SET) == -1)
96 nr = read(f->fd, buffer, MAXBUFSIZ);
99 /* Make sure we exit with an error */
103 nr = read(f->fd, buffer, MAXBUFSIZ);
113 grep_lnbufgrow(size_t newlen)
116 if (lnbuflen < newlen) {
117 lnbuf = grep_realloc(lnbuf, newlen);
125 grep_fgetln(struct file *f, size_t *lenp)
133 /* Fill the buffer, if necessary */
134 if (bufrem == 0 && grep_refill(f) != 0)
138 /* Return zero length to indicate EOF */
143 /* Look for a newline in the remaining part of the buffer */
144 if ((p = memchr(bufpos, '\n', bufrem)) != NULL) {
145 ++p; /* advance over newline */
154 /* We have to copy the current buffered data to the line buffer */
155 for (len = bufrem, off = 0; ; len += bufrem) {
156 /* Make sure there is room for more data */
157 if (grep_lnbufgrow(len + LNBUFBUMP))
159 memcpy(lnbuf + off, bufpos, len - off);
161 if (grep_refill(f) != 0)
164 /* EOF: return partial line */
166 if ((p = memchr(bufpos, '\n', bufrem)) == NULL)
168 /* got it: finish up the line (like code above) */
172 if (grep_lnbufgrow(len))
174 memcpy(lnbuf + off, bufpos, diff);
187 static inline struct file *
188 grep_file_init(struct file *f)
191 if (filebehave == FILE_GZIP &&
192 (gzbufdesc = gzdopen(f->fd, "r")) == NULL)
195 if (filebehave == FILE_BZIP &&
196 (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
199 /* Fill read buffer, also catches errors early */
200 if (grep_refill(f) != 0)
203 /* Check for binary stuff, if necessary */
204 if (binbehave != BINFILE_TEXT && memchr(bufpos, '\0', bufrem) != NULL)
215 * Opens a file for processing.
218 grep_open(const char *path)
222 f = grep_malloc(sizeof *f);
223 memset(f, 0, sizeof *f);
225 /* Processing stdin implies --line-buffered. */
227 f->fd = STDIN_FILENO;
228 } else if ((f->fd = open(path, O_RDONLY)) == -1) {
233 return (grep_file_init(f));
240 grep_close(struct file *f)
245 /* Reset read buffer and line buffer */