1 /* $NetBSD: file.c,v 1.5 2011/02/16 18:35:39 joerg Exp $ */
2 /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */
3
4 /*-
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * Copyright (c) 1999 James Howard and Dag-Erling Smørgrav
8 * Copyright (C) 2008-2010 Gabor Kovesdan <[email protected]>
9 * Copyright (C) 2010 Dimitry Andric <[email protected]>
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <wchar.h>
48 #include <wctype.h>
49
50 #include "grep.h"
51
52 #define MAXBUFSIZ (32 * 1024)
53 #define LNBUFBUMP 80
54
55 static char *buffer;
56 static char *bufpos;
57 static size_t bufrem;
58 static size_t fsiz;
59
60 static char *lnbuf;
61 static size_t lnbuflen;
62
63 static inline int
grep_refill(struct file * f)64 grep_refill(struct file *f)
65 {
66 ssize_t nr;
67
68 if (filebehave == FILE_MMAP)
69 return (0);
70
71 bufpos = buffer;
72 bufrem = 0;
73
74 nr = read(f->fd, buffer, MAXBUFSIZ);
75 if (nr < 0 && errno == EISDIR)
76 nr = 0;
77 if (nr < 0)
78 return (-1);
79
80 bufrem = nr;
81 return (0);
82 }
83
84 static inline int
grep_lnbufgrow(size_t newlen)85 grep_lnbufgrow(size_t newlen)
86 {
87
88 if (lnbuflen < newlen) {
89 lnbuf = grep_realloc(lnbuf, newlen);
90 lnbuflen = newlen;
91 }
92
93 return (0);
94 }
95
96 char *
grep_fgetln(struct file * f,struct parsec * pc)97 grep_fgetln(struct file *f, struct parsec *pc)
98 {
99 char *p;
100 size_t len;
101 size_t off;
102 ptrdiff_t diff;
103
104 /* Fill the buffer, if necessary */
105 if (bufrem == 0 && grep_refill(f) != 0)
106 goto error;
107
108 if (bufrem == 0) {
109 /* Return zero length to indicate EOF */
110 pc->ln.len= 0;
111 return (bufpos);
112 }
113
114 /* Look for a newline in the remaining part of the buffer */
115 if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
116 ++p; /* advance over newline */
117 len = p - bufpos;
118 if (grep_lnbufgrow(len + 1))
119 goto error;
120 memcpy(lnbuf, bufpos, len);
121 bufrem -= len;
122 bufpos = p;
123 pc->ln.len = len;
124 lnbuf[len] = '\0';
125 return (lnbuf);
126 }
127
128 /* We have to copy the current buffered data to the line buffer */
129 for (len = bufrem, off = 0; ; len += bufrem) {
130 /* Make sure there is room for more data */
131 if (grep_lnbufgrow(len + LNBUFBUMP))
132 goto error;
133 memcpy(lnbuf + off, bufpos, len - off);
134 /* With FILE_MMAP, this is EOF; there's no more to refill */
135 if (filebehave == FILE_MMAP) {
136 bufrem -= len;
137 break;
138 }
139 off = len;
140 /* Fetch more to try and find EOL/EOF */
141 if (grep_refill(f) != 0)
142 goto error;
143 if (bufrem == 0)
144 /* EOF: return partial line */
145 break;
146 if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
147 continue;
148 /* got it: finish up the line (like code above) */
149 ++p;
150 diff = p - bufpos;
151 len += diff;
152 if (grep_lnbufgrow(len + 1))
153 goto error;
154 memcpy(lnbuf + off, bufpos, diff);
155 bufrem -= diff;
156 bufpos = p;
157 break;
158 }
159 pc->ln.len = len;
160 lnbuf[len] = '\0';
161 return (lnbuf);
162
163 error:
164 pc->ln.len = 0;
165 return (NULL);
166 }
167
168 /*
169 * Opens a file for processing.
170 */
171 struct file *
grep_open(const char * path)172 grep_open(const char *path)
173 {
174 struct file *f;
175
176 f = grep_malloc(sizeof *f);
177 memset(f, 0, sizeof *f);
178 if (path == NULL) {
179 /* Processing stdin implies --line-buffered. */
180 lbflag = true;
181 f->fd = STDIN_FILENO;
182 } else if ((f->fd = open(path, O_RDONLY)) == -1)
183 goto error1;
184
185 if (filebehave == FILE_MMAP) {
186 struct stat st;
187
188 if (fstat(f->fd, &st) == -1 || !S_ISREG(st.st_mode))
189 filebehave = FILE_STDIO;
190 else {
191 int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
192 #ifdef MAP_PREFAULT_READ
193 flags |= MAP_PREFAULT_READ;
194 #endif
195 fsiz = st.st_size;
196 buffer = mmap(NULL, fsiz, PROT_READ, flags,
197 f->fd, (off_t)0);
198 if (buffer == MAP_FAILED)
199 filebehave = FILE_STDIO;
200 else {
201 bufrem = st.st_size;
202 bufpos = buffer;
203 madvise(buffer, st.st_size, MADV_SEQUENTIAL);
204 }
205 }
206 }
207
208 if ((buffer == NULL) || (buffer == MAP_FAILED))
209 buffer = grep_malloc(MAXBUFSIZ);
210
211 /* Fill read buffer, also catches errors early */
212 if (bufrem == 0 && grep_refill(f) != 0)
213 goto error2;
214
215 /* Check for binary stuff, if necessary */
216 if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
217 memchr(bufpos, '\0', bufrem) != NULL)
218 f->binary = true;
219
220 return (f);
221
222 error2:
223 close(f->fd);
224 error1:
225 free(f);
226 return (NULL);
227 }
228
229 /*
230 * Closes a file.
231 */
232 void
grep_close(struct file * f)233 grep_close(struct file *f)
234 {
235
236 close(f->fd);
237
238 /* Reset read buffer and line buffer */
239 if (filebehave == FILE_MMAP) {
240 munmap(buffer, fsiz);
241 buffer = NULL;
242 }
243 bufpos = buffer;
244 bufrem = 0;
245
246 free(lnbuf);
247 lnbuf = NULL;
248 lnbuflen = 0;
249 }
250