1 /* $NetBSD: file.c,v 1.10 2018/08/12 09:03:21 christos Exp $ */
2 /* $FreeBSD: head/usr.bin/grep/file.c 211496 2010-08-19 09:28:59Z des $ */
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 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
35 #endif
36
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: file.c,v 1.10 2018/08/12 09:03:21 christos Exp $");
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
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
54 #include "grep.h"
55
56 #define MAXBUFSIZ (32 * 1024)
57 #define LNBUFBUMP 80
58
59 #ifndef WITHOUT_GZIP
60 static gzFile gzbufdesc;
61 #endif
62 #ifndef WITHOUT_BZ2
63 static BZFILE* bzbufdesc;
64 #endif
65
66 static unsigned char buffer[MAXBUFSIZ + 1];
67 static unsigned char *bufpos;
68 static size_t bufrem;
69
70 static unsigned char *lnbuf;
71 static size_t lnbuflen;
72
73 static inline int
grep_refill(struct file * f)74 grep_refill(struct file *f)
75 {
76 ssize_t nr = -1;
77 int bzerr;
78
79 bufpos = buffer;
80 bufrem = 0;
81
82 #ifndef WITHOUT_GZIP
83 if (filebehave == FILE_GZIP) {
84 nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
85 if (nr == -1)
86 return -1;
87 }
88 #endif
89 #ifndef WITHOUT_BZ2
90 if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
91 nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
92 switch (bzerr) {
93 case BZ_OK:
94 case BZ_STREAM_END:
95 /* No problem, nr will be okay */
96 break;
97 case BZ_DATA_ERROR_MAGIC:
98 /*
99 * As opposed to gzread(), which simply returns the
100 * plain file data, if it is not in the correct
101 * compressed format, BZ2_bzRead() instead aborts.
102 *
103 * So, just restart at the beginning of the file again,
104 * and use plain reads from now on.
105 */
106 BZ2_bzReadClose(&bzerr, bzbufdesc);
107 bzbufdesc = NULL;
108 if (lseek(f->fd, 0, SEEK_SET) == -1)
109 return (-1);
110 nr = read(f->fd, buffer, MAXBUFSIZ);
111 break;
112 default:
113 /* Make sure we exit with an error */
114 nr = -1;
115 }
116 if (nr == -1)
117 return -1;
118 }
119 #endif
120 if (nr == -1) {
121 nr = read(f->fd, buffer, MAXBUFSIZ);
122 }
123
124 if (nr < 0)
125 return (-1);
126
127 bufrem = nr;
128 return (0);
129 }
130
131 static inline void
grep_lnbufgrow(size_t newlen)132 grep_lnbufgrow(size_t newlen)
133 {
134
135 if (lnbuflen < newlen) {
136 lnbuf = grep_realloc(lnbuf, newlen);
137 lnbuflen = newlen;
138 }
139 }
140
141 char *
grep_fgetln(struct file * f,size_t * lenp)142 grep_fgetln(struct file *f, size_t *lenp)
143 {
144 unsigned char *p;
145 char *ret;
146 size_t len;
147 size_t off;
148 ptrdiff_t diff;
149
150 /* Fill the buffer, if necessary */
151 if (bufrem == 0 && grep_refill(f) != 0)
152 goto error;
153
154 if (bufrem == 0) {
155 /* Return zero length to indicate EOF */
156 *lenp = 0;
157 return ((char *)bufpos);
158 }
159
160 /* Look for a newline in the remaining part of the buffer */
161 if ((p = memchr(bufpos, line_sep, bufrem)) != NULL) {
162 ++p; /* advance over newline */
163 len = p - bufpos;
164 grep_lnbufgrow(len + 1);
165 memcpy(lnbuf, bufpos, len);
166 lnbuf[len] = '\0';
167 *lenp = len;
168 bufrem -= len;
169 bufpos = p;
170 return ((char *)lnbuf);
171 }
172
173 /* We have to copy the current buffered data to the line buffer */
174 for (len = bufrem, off = 0; ; len += bufrem) {
175 /* Make sure there is room for more data */
176 grep_lnbufgrow(len + LNBUFBUMP);
177 memcpy(lnbuf + off, bufpos, len - off);
178 lnbuf[len] = '\0';
179 off = len;
180 if (grep_refill(f) != 0)
181 goto error;
182 if (bufrem == 0)
183 /* EOF: return partial line */
184 break;
185 if ((p = memchr(bufpos, line_sep, bufrem)) == NULL)
186 continue;
187 /* got it: finish up the line (like code above) */
188 ++p;
189 diff = p - bufpos;
190 len += diff;
191 grep_lnbufgrow(len + 1);
192 memcpy(lnbuf + off, bufpos, diff);
193 lnbuf[off + diff] = '\0';
194 bufrem -= diff;
195 bufpos = p;
196 break;
197 }
198 *lenp = len;
199 return ((char *)lnbuf);
200
201 error:
202 *lenp = 0;
203 return (NULL);
204 }
205
206 static inline struct file *
grep_file_init(struct file * f)207 grep_file_init(struct file *f)
208 {
209
210 #ifndef WITHOUT_GZIP
211 if (filebehave == FILE_GZIP &&
212 (gzbufdesc = gzdopen(f->fd, "r")) == NULL)
213 goto error;
214 #endif
215
216 #ifndef WITHOUT_BZ2
217 if (filebehave == FILE_BZIP &&
218 (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
219 goto error;
220 #endif
221
222 /* Fill read buffer, also catches errors early */
223 if (grep_refill(f) != 0)
224 goto error;
225
226 /* Check for binary stuff, if necessary */
227 if (!nulldataflag && binbehave != BINFILE_TEXT &&
228 memchr(bufpos, '\0', bufrem) != NULL)
229 f->binary = true;
230
231 return (f);
232 error:
233 close(f->fd);
234 free(f);
235 return (NULL);
236 }
237
238 /*
239 * Opens a file for processing.
240 */
241 struct file *
grep_open(const char * path)242 grep_open(const char *path)
243 {
244 struct file *f;
245
246 f = grep_malloc(sizeof *f);
247 memset(f, 0, sizeof *f);
248 if (path == NULL) {
249 /* Processing stdin implies --line-buffered. */
250 lbflag = true;
251 f->fd = STDIN_FILENO;
252 } else if ((f->fd = open(path, O_RDONLY)) == -1) {
253 free(f);
254 return (NULL);
255 }
256
257 return (grep_file_init(f));
258 }
259
260 /*
261 * Closes a file.
262 */
263 void
grep_close(struct file * f)264 grep_close(struct file *f)
265 {
266
267 close(f->fd);
268
269 /* Reset read buffer and line buffer */
270 bufpos = buffer;
271 bufrem = 0;
272
273 free(lnbuf);
274 lnbuf = NULL;
275 lnbuflen = 0;
276 }
277