]> git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/grep/file.c
Import macOS userland
[apple_cmds.git] / text_cmds / grep / file.c
1 /* $NetBSD: file.c,v 1.5 2011/02/16 18:35:39 joerg Exp $ */
2 /* $FreeBSD: src/usr.bin/grep/file.c,v 1.7 2011/10/11 22:27:23 gabor Exp $ */
3 /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */
4
5 /*-
6 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
7 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
8 * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/usr.bin/grep/file.c,v 1.7 2011/10/11 22:27:23 gabor Exp $");
35
36 #include <sys/param.h>
37 #include <sys/mman.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #ifndef WITHOUT_LZMA
45 #include <lzma.h>
46 #endif
47 #include <stddef.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <wchar.h>
52 #include <wctype.h>
53 #include <zlib.h>
54
55 #ifndef WITHOUT_BZIP2
56 #include <bzlib.h>
57 #endif
58
59 #include "grep.h"
60
61 #define MAXBUFSIZ (32 * 1024)
62 #define LNBUFBUMP 80
63
64 static gzFile gzbufdesc;
65 #ifndef WITHOUT_LZMA
66 static lzma_stream lstrm = LZMA_STREAM_INIT;
67 #endif
68 #ifndef WITHOUT_BZIP2
69 static BZFILE* bzbufdesc;
70 #endif
71
72 static unsigned char *buffer;
73 static unsigned char *bufpos;
74 static size_t bufrem;
75 static size_t fsiz;
76
77 static unsigned char *lnbuf;
78 static size_t lnbuflen;
79
80 static inline int
81 grep_refill(struct file *f)
82 {
83 ssize_t nr;
84
85 if (filebehave == FILE_MMAP)
86 return (0);
87
88 bufpos = buffer;
89 bufrem = 0;
90
91 if (filebehave == FILE_GZIP) {
92 nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
93 #ifndef WITHOUT_BZIP2
94 } else if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
95 int bzerr;
96
97 nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
98 switch (bzerr) {
99 case BZ_OK:
100 case BZ_STREAM_END:
101 /* No problem, nr will be okay */
102 break;
103 case BZ_DATA_ERROR_MAGIC:
104 /*
105 * As opposed to gzread(), which simply returns the
106 * plain file data, if it is not in the correct
107 * compressed format, BZ2_bzRead() instead aborts.
108 *
109 * So, just restart at the beginning of the file again,
110 * and use plain reads from now on.
111 */
112 BZ2_bzReadClose(&bzerr, bzbufdesc);
113 bzbufdesc = NULL;
114 if (lseek(f->fd, 0, SEEK_SET) == -1)
115 return (-1);
116 nr = read(f->fd, buffer, MAXBUFSIZ);
117 break;
118 default:
119 /* Make sure we exit with an error */
120 nr = -1;
121 }
122 #endif
123 #ifndef WITHOUT_LZMA
124 } else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
125 lzma_action action = LZMA_RUN;
126 uint8_t in_buf[MAXBUFSIZ];
127 lzma_ret ret;
128
129 ret = (filebehave == FILE_XZ) ?
130 lzma_stream_decoder(&lstrm, UINT64_MAX,
131 LZMA_CONCATENATED) :
132 lzma_alone_decoder(&lstrm, UINT64_MAX);
133
134 if (ret != LZMA_OK)
135 return (-1);
136
137 lstrm.next_out = buffer;
138 lstrm.avail_out = MAXBUFSIZ;
139 lstrm.next_in = in_buf;
140 nr = read(f->fd, in_buf, MAXBUFSIZ);
141
142 if (nr < 0)
143 return (-1);
144 else if (nr == 0)
145 action = LZMA_FINISH;
146
147 lstrm.avail_in = nr;
148 ret = lzma_code(&lstrm, action);
149
150 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
151 return (-1);
152 bufrem = MAXBUFSIZ - lstrm.avail_out;
153 return (0);
154 #endif
155 } else
156 nr = read(f->fd, buffer, MAXBUFSIZ);
157
158 if (nr < 0)
159 return (-1);
160
161 bufrem = nr;
162 return (0);
163 }
164
165 static inline int
166 grep_lnbufgrow(size_t newlen)
167 {
168
169 if (lnbuflen < newlen) {
170 lnbuf = grep_realloc(lnbuf, newlen);
171 lnbuflen = newlen;
172 }
173
174 return (0);
175 }
176
177 char *
178 grep_fgetln(struct file *f, size_t *lenp)
179 {
180 unsigned char *p;
181 char *ret;
182 size_t len;
183 size_t off;
184 ptrdiff_t diff;
185
186 /* Fill the buffer, if necessary */
187 if (bufrem == 0 && grep_refill(f) != 0)
188 goto error;
189
190 if (bufrem == 0) {
191 /* Return zero length to indicate EOF */
192 *lenp = 0;
193 #ifdef __APPLE__
194 return (char *)(bufpos);
195 #else
196 return (bufpos);
197 #endif
198 }
199
200 /* Look for a newline in the remaining part of the buffer */
201 if ((p = memchr(bufpos, '\n', bufrem)) != NULL) {
202 ++p; /* advance over newline */
203 #ifdef __APPLE__
204 ret = (char *)bufpos;
205 #else
206 ret = bufpos;
207 #endif
208 len = p - bufpos;
209 bufrem -= len;
210 bufpos = p;
211 *lenp = len;
212 return (ret);
213 }
214
215 /* We have to copy the current buffered data to the line buffer */
216 for (len = bufrem, off = 0; ; len += bufrem) {
217 /* Make sure there is room for more data */
218 if (grep_lnbufgrow(len + LNBUFBUMP))
219 goto error;
220 memcpy(lnbuf + off, bufpos, len - off);
221 off = len;
222 if (grep_refill(f) != 0)
223 goto error;
224 if (bufrem == 0)
225 /* EOF: return partial line */
226 break;
227 if ((p = memchr(bufpos, '\n', bufrem)) == NULL)
228 continue;
229 /* got it: finish up the line (like code above) */
230 ++p;
231 diff = p - bufpos;
232 len += diff;
233 if (grep_lnbufgrow(len))
234 goto error;
235 memcpy(lnbuf + off, bufpos, diff);
236 bufrem -= diff;
237 bufpos = p;
238 break;
239 }
240 *lenp = len;
241 #ifdef __APPLE__
242 return (char *)(lnbuf);
243 #else
244 return (lnbuf);
245 #endif
246
247 error:
248 *lenp = 0;
249 return (NULL);
250 }
251
252 /*
253 * Opens a file for processing.
254 */
255 struct file *
256 grep_open(const char *path)
257 {
258 struct file *f;
259
260 f = grep_malloc(sizeof *f);
261 memset(f, 0, sizeof *f);
262 if (path == NULL) {
263 /* Processing stdin implies --line-buffered. */
264 lbflag = true;
265 f->fd = STDIN_FILENO;
266 } else if ((f->fd = open(path, O_RDONLY)) == -1)
267 goto error1;
268
269 if (filebehave == FILE_MMAP) {
270 struct stat st;
271
272 if ((fstat(f->fd, &st) == -1) || (st.st_size > OFF_MAX) ||
273 (!S_ISREG(st.st_mode)))
274 filebehave = FILE_STDIO;
275 else {
276 #ifdef __APPLE__
277 int flags = MAP_PRIVATE | MAP_NOCACHE;
278 #else
279 int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
280 #endif
281 #ifdef MAP_PREFAULT_READ
282 flags |= MAP_PREFAULT_READ;
283 #endif
284 fsiz = st.st_size;
285 buffer = mmap(NULL, fsiz, PROT_READ, flags,
286 f->fd, (off_t)0);
287 if (buffer == MAP_FAILED)
288 filebehave = FILE_STDIO;
289 else {
290 bufrem = st.st_size;
291 bufpos = buffer;
292 madvise(buffer, st.st_size, MADV_SEQUENTIAL);
293 }
294 }
295 }
296
297 if ((buffer == NULL) || (buffer == MAP_FAILED))
298 buffer = grep_malloc(MAXBUFSIZ);
299
300 if (filebehave == FILE_GZIP &&
301 (gzbufdesc = gzdopen(f->fd, "r")) == NULL)
302 goto error2;
303
304 #ifndef WITHOUT_BZIP2
305 if (filebehave == FILE_BZIP &&
306 (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
307 goto error2;
308 #endif
309
310 /* Fill read buffer, also catches errors early */
311 if (bufrem == 0 && grep_refill(f) != 0)
312 goto error2;
313
314 /* Check for binary stuff, if necessary */
315 if (binbehave != BINFILE_TEXT && memchr(bufpos, '\0', bufrem) != NULL)
316 f->binary = true;
317
318 return (f);
319
320 error2:
321 close(f->fd);
322 error1:
323 free(f);
324 return (NULL);
325 }
326
327 /*
328 * Closes a file.
329 */
330 void
331 grep_close(struct file *f)
332 {
333
334 close(f->fd);
335
336 /* Reset read buffer and line buffer */
337 if (filebehave == FILE_MMAP) {
338 munmap(buffer, fsiz);
339 buffer = NULL;
340 }
341 bufpos = buffer;
342 bufrem = 0;
343
344 free(lnbuf);
345 lnbuf = NULL;
346 lnbuflen = 0;
347 }