xref: /freebsd/usr.bin/grep/file.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
1b66a823bSGabor Kovesdan /*	$NetBSD: file.c,v 1.5 2011/02/16 18:35:39 joerg Exp $	*/
24dc88ebeSGabor Kovesdan /*	$OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $	*/
34dc88ebeSGabor Kovesdan 
44dc88ebeSGabor Kovesdan /*-
54d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
61de7b4b8SPedro F. Giffuni  *
7e738085bSDag-Erling Smørgrav  * Copyright (c) 1999 James Howard and Dag-Erling Smørgrav
83ed1008bSGabor Kovesdan  * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
93ed1008bSGabor Kovesdan  * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
104dc88ebeSGabor Kovesdan  * All rights reserved.
114dc88ebeSGabor Kovesdan  *
124dc88ebeSGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
134dc88ebeSGabor Kovesdan  * modification, are permitted provided that the following conditions
144dc88ebeSGabor Kovesdan  * are met:
154dc88ebeSGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
164dc88ebeSGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
174dc88ebeSGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
184dc88ebeSGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
194dc88ebeSGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
204dc88ebeSGabor Kovesdan  *
214dc88ebeSGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
224dc88ebeSGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234dc88ebeSGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244dc88ebeSGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
254dc88ebeSGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264dc88ebeSGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274dc88ebeSGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284dc88ebeSGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294dc88ebeSGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304dc88ebeSGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314dc88ebeSGabor Kovesdan  * SUCH DAMAGE.
324dc88ebeSGabor Kovesdan  */
334dc88ebeSGabor Kovesdan 
344dc88ebeSGabor Kovesdan #include <sys/param.h>
35f20f6f3fSGabor Kovesdan #include <sys/mman.h>
364dc88ebeSGabor Kovesdan #include <sys/stat.h>
37f20f6f3fSGabor Kovesdan #include <sys/types.h>
384dc88ebeSGabor Kovesdan 
394dc88ebeSGabor Kovesdan #include <err.h>
404dc88ebeSGabor Kovesdan #include <errno.h>
413ed1008bSGabor Kovesdan #include <fcntl.h>
423ed1008bSGabor Kovesdan #include <stddef.h>
434dc88ebeSGabor Kovesdan #include <stdlib.h>
444dc88ebeSGabor Kovesdan #include <string.h>
454dc88ebeSGabor Kovesdan #include <unistd.h>
464dc88ebeSGabor Kovesdan #include <wchar.h>
474dc88ebeSGabor Kovesdan #include <wctype.h>
48afbbd357SGabor Kovesdan 
494dc88ebeSGabor Kovesdan #include "grep.h"
504dc88ebeSGabor Kovesdan 
513ed1008bSGabor Kovesdan #define	MAXBUFSIZ	(32 * 1024)
523ed1008bSGabor Kovesdan #define	LNBUFBUMP	80
534dc88ebeSGabor Kovesdan 
5486ce5365SAlex Richardson static char *buffer;
5586ce5365SAlex Richardson static char *bufpos;
563ed1008bSGabor Kovesdan static size_t bufrem;
57f20f6f3fSGabor Kovesdan static size_t fsiz;
583ed1008bSGabor Kovesdan 
5986ce5365SAlex Richardson static char *lnbuf;
604dc88ebeSGabor Kovesdan static size_t lnbuflen;
614dc88ebeSGabor Kovesdan 
6259218eb7SGabor Kovesdan static inline int
grep_refill(struct file * f)633ed1008bSGabor Kovesdan grep_refill(struct file *f)
644dc88ebeSGabor Kovesdan {
653ed1008bSGabor Kovesdan 	ssize_t nr;
664dc88ebeSGabor Kovesdan 
67f20f6f3fSGabor Kovesdan 	if (filebehave == FILE_MMAP)
68f20f6f3fSGabor Kovesdan 		return (0);
69f20f6f3fSGabor Kovesdan 
703ed1008bSGabor Kovesdan 	bufpos = buffer;
713ed1008bSGabor Kovesdan 	bufrem = 0;
724dc88ebeSGabor Kovesdan 
733ed1008bSGabor Kovesdan 	nr = read(f->fd, buffer, MAXBUFSIZ);
74*caf5283fSDag-Erling Smørgrav 	if (nr < 0 && errno == EISDIR)
75*caf5283fSDag-Erling Smørgrav 		nr = 0;
763ed1008bSGabor Kovesdan 	if (nr < 0)
773ed1008bSGabor Kovesdan 		return (-1);
783ed1008bSGabor Kovesdan 
793ed1008bSGabor Kovesdan 	bufrem = nr;
803ed1008bSGabor Kovesdan 	return (0);
814dc88ebeSGabor Kovesdan }
824dc88ebeSGabor Kovesdan 
833ed1008bSGabor Kovesdan static inline int
grep_lnbufgrow(size_t newlen)843ed1008bSGabor Kovesdan grep_lnbufgrow(size_t newlen)
854dc88ebeSGabor Kovesdan {
864dc88ebeSGabor Kovesdan 
873ed1008bSGabor Kovesdan 	if (lnbuflen < newlen) {
883ed1008bSGabor Kovesdan 		lnbuf = grep_realloc(lnbuf, newlen);
893ed1008bSGabor Kovesdan 		lnbuflen = newlen;
904dc88ebeSGabor Kovesdan 	}
914dc88ebeSGabor Kovesdan 
923ed1008bSGabor Kovesdan 	return (0);
933ed1008bSGabor Kovesdan }
943ed1008bSGabor Kovesdan 
953ed1008bSGabor Kovesdan char *
grep_fgetln(struct file * f,struct parsec * pc)96bd60b9b4SKyle Evans grep_fgetln(struct file *f, struct parsec *pc)
973ed1008bSGabor Kovesdan {
9886ce5365SAlex Richardson 	char *p;
993ed1008bSGabor Kovesdan 	size_t len;
1003ed1008bSGabor Kovesdan 	size_t off;
1013ed1008bSGabor Kovesdan 	ptrdiff_t diff;
1023ed1008bSGabor Kovesdan 
1033ed1008bSGabor Kovesdan 	/* Fill the buffer, if necessary */
1043ed1008bSGabor Kovesdan 	if (bufrem == 0 && grep_refill(f) != 0)
1053ed1008bSGabor Kovesdan 		goto error;
1063ed1008bSGabor Kovesdan 
1073ed1008bSGabor Kovesdan 	if (bufrem == 0) {
1083ed1008bSGabor Kovesdan 		/* Return zero length to indicate EOF */
109bd60b9b4SKyle Evans 		pc->ln.len= 0;
1103ed1008bSGabor Kovesdan 		return (bufpos);
1113ed1008bSGabor Kovesdan 	}
1123ed1008bSGabor Kovesdan 
113d1a920b4SKyle Evans 	/* Look for a newline in the remaining part of the buffer */
1145ee1ea02SEd Maste 	if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) {
1153ed1008bSGabor Kovesdan 		++p; /* advance over newline */
1163ed1008bSGabor Kovesdan 		len = p - bufpos;
11781c3f641SAlex Richardson 		if (grep_lnbufgrow(len + 1))
11881c3f641SAlex Richardson 			goto error;
11981c3f641SAlex Richardson 		memcpy(lnbuf, bufpos, len);
1203ed1008bSGabor Kovesdan 		bufrem -= len;
1213ed1008bSGabor Kovesdan 		bufpos = p;
122bd60b9b4SKyle Evans 		pc->ln.len = len;
12381c3f641SAlex Richardson 		lnbuf[len] = '\0';
12481c3f641SAlex Richardson 		return (lnbuf);
1253ed1008bSGabor Kovesdan 	}
1263ed1008bSGabor Kovesdan 
1273ed1008bSGabor Kovesdan 	/* We have to copy the current buffered data to the line buffer */
1283ed1008bSGabor Kovesdan 	for (len = bufrem, off = 0; ; len += bufrem) {
1293ed1008bSGabor Kovesdan 		/* Make sure there is room for more data */
1303ed1008bSGabor Kovesdan 		if (grep_lnbufgrow(len + LNBUFBUMP))
1313ed1008bSGabor Kovesdan 			goto error;
1323ed1008bSGabor Kovesdan 		memcpy(lnbuf + off, bufpos, len - off);
1339a145202SEd Maste 		/* With FILE_MMAP, this is EOF; there's no more to refill */
1349a145202SEd Maste 		if (filebehave == FILE_MMAP) {
1359a145202SEd Maste 			bufrem -= len;
1369a145202SEd Maste 			break;
1379a145202SEd Maste 		}
1383ed1008bSGabor Kovesdan 		off = len;
1399a145202SEd Maste 		/* Fetch more to try and find EOL/EOF */
1403ed1008bSGabor Kovesdan 		if (grep_refill(f) != 0)
1413ed1008bSGabor Kovesdan 			goto error;
1423ed1008bSGabor Kovesdan 		if (bufrem == 0)
1433ed1008bSGabor Kovesdan 			/* EOF: return partial line */
1443ed1008bSGabor Kovesdan 			break;
1459a145202SEd Maste 		if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
1463ed1008bSGabor Kovesdan 			continue;
1473ed1008bSGabor Kovesdan 		/* got it: finish up the line (like code above) */
1483ed1008bSGabor Kovesdan 		++p;
1493ed1008bSGabor Kovesdan 		diff = p - bufpos;
1503ed1008bSGabor Kovesdan 		len += diff;
15181c3f641SAlex Richardson 		if (grep_lnbufgrow(len + 1))
1523ed1008bSGabor Kovesdan 		    goto error;
1533ed1008bSGabor Kovesdan 		memcpy(lnbuf + off, bufpos, diff);
1543ed1008bSGabor Kovesdan 		bufrem -= diff;
1553ed1008bSGabor Kovesdan 		bufpos = p;
1563ed1008bSGabor Kovesdan 		break;
1573ed1008bSGabor Kovesdan 	}
158bd60b9b4SKyle Evans 	pc->ln.len = len;
15981c3f641SAlex Richardson 	lnbuf[len] = '\0';
1603ed1008bSGabor Kovesdan 	return (lnbuf);
1613ed1008bSGabor Kovesdan 
1623ed1008bSGabor Kovesdan error:
163bd60b9b4SKyle Evans 	pc->ln.len = 0;
1643ed1008bSGabor Kovesdan 	return (NULL);
1653ed1008bSGabor Kovesdan }
1663ed1008bSGabor Kovesdan 
1674dc88ebeSGabor Kovesdan /*
1683ed1008bSGabor Kovesdan  * Opens a file for processing.
1694dc88ebeSGabor Kovesdan  */
1704dc88ebeSGabor Kovesdan struct file *
grep_open(const char * path)1714dc88ebeSGabor Kovesdan grep_open(const char *path)
1724dc88ebeSGabor Kovesdan {
1734dc88ebeSGabor Kovesdan 	struct file *f;
1744dc88ebeSGabor Kovesdan 
1754dc88ebeSGabor Kovesdan 	f = grep_malloc(sizeof *f);
1763ed1008bSGabor Kovesdan 	memset(f, 0, sizeof *f);
1773ed1008bSGabor Kovesdan 	if (path == NULL) {
1783ed1008bSGabor Kovesdan 		/* Processing stdin implies --line-buffered. */
1793ed1008bSGabor Kovesdan 		lbflag = true;
1803ed1008bSGabor Kovesdan 		f->fd = STDIN_FILENO;
181f20f6f3fSGabor Kovesdan 	} else if ((f->fd = open(path, O_RDONLY)) == -1)
182f20f6f3fSGabor Kovesdan 		goto error1;
183f20f6f3fSGabor Kovesdan 
184f20f6f3fSGabor Kovesdan 	if (filebehave == FILE_MMAP) {
185f20f6f3fSGabor Kovesdan 		struct stat st;
186f20f6f3fSGabor Kovesdan 
187b6175849SKyle Evans 		if (fstat(f->fd, &st) == -1 || !S_ISREG(st.st_mode))
188f20f6f3fSGabor Kovesdan 			filebehave = FILE_STDIO;
189f20f6f3fSGabor Kovesdan 		else {
190f20f6f3fSGabor Kovesdan 			int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC;
191f20f6f3fSGabor Kovesdan #ifdef MAP_PREFAULT_READ
192f20f6f3fSGabor Kovesdan 			flags |= MAP_PREFAULT_READ;
193f20f6f3fSGabor Kovesdan #endif
194f20f6f3fSGabor Kovesdan 			fsiz = st.st_size;
195f20f6f3fSGabor Kovesdan 			buffer = mmap(NULL, fsiz, PROT_READ, flags,
196f20f6f3fSGabor Kovesdan 			     f->fd, (off_t)0);
197f20f6f3fSGabor Kovesdan 			if (buffer == MAP_FAILED)
198f20f6f3fSGabor Kovesdan 				filebehave = FILE_STDIO;
199f20f6f3fSGabor Kovesdan 			else {
200f20f6f3fSGabor Kovesdan 				bufrem = st.st_size;
201f20f6f3fSGabor Kovesdan 				bufpos = buffer;
202f20f6f3fSGabor Kovesdan 				madvise(buffer, st.st_size, MADV_SEQUENTIAL);
203f20f6f3fSGabor Kovesdan 			}
204f20f6f3fSGabor Kovesdan 		}
2054dc88ebeSGabor Kovesdan 	}
2064dc88ebeSGabor Kovesdan 
207f20f6f3fSGabor Kovesdan 	if ((buffer == NULL) || (buffer == MAP_FAILED))
208f20f6f3fSGabor Kovesdan 		buffer = grep_malloc(MAXBUFSIZ);
209f20f6f3fSGabor Kovesdan 
210f20f6f3fSGabor Kovesdan 	/* Fill read buffer, also catches errors early */
211f20f6f3fSGabor Kovesdan 	if (bufrem == 0 && grep_refill(f) != 0)
212f20f6f3fSGabor Kovesdan 		goto error2;
213f20f6f3fSGabor Kovesdan 
214f20f6f3fSGabor Kovesdan 	/* Check for binary stuff, if necessary */
2155ee1ea02SEd Maste 	if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
2165ee1ea02SEd Maste 	    memchr(bufpos, '\0', bufrem) != NULL)
217f20f6f3fSGabor Kovesdan 		f->binary = true;
218f20f6f3fSGabor Kovesdan 
219f20f6f3fSGabor Kovesdan 	return (f);
220f20f6f3fSGabor Kovesdan 
221f20f6f3fSGabor Kovesdan error2:
222f20f6f3fSGabor Kovesdan 	close(f->fd);
223f20f6f3fSGabor Kovesdan error1:
224f20f6f3fSGabor Kovesdan 	free(f);
225f20f6f3fSGabor Kovesdan 	return (NULL);
2263ed1008bSGabor Kovesdan }
2273ed1008bSGabor Kovesdan 
2284dc88ebeSGabor Kovesdan /*
2293ed1008bSGabor Kovesdan  * Closes a file.
2304dc88ebeSGabor Kovesdan  */
2314dc88ebeSGabor Kovesdan void
grep_close(struct file * f)2324dc88ebeSGabor Kovesdan grep_close(struct file *f)
2334dc88ebeSGabor Kovesdan {
2344dc88ebeSGabor Kovesdan 
2353ed1008bSGabor Kovesdan 	close(f->fd);
2364dc88ebeSGabor Kovesdan 
2373ed1008bSGabor Kovesdan 	/* Reset read buffer and line buffer */
238f20f6f3fSGabor Kovesdan 	if (filebehave == FILE_MMAP) {
239f20f6f3fSGabor Kovesdan 		munmap(buffer, fsiz);
240f20f6f3fSGabor Kovesdan 		buffer = NULL;
241f20f6f3fSGabor Kovesdan 	}
2423ed1008bSGabor Kovesdan 	bufpos = buffer;
2433ed1008bSGabor Kovesdan 	bufrem = 0;
2443ed1008bSGabor Kovesdan 
2453ed1008bSGabor Kovesdan 	free(lnbuf);
2463ed1008bSGabor Kovesdan 	lnbuf = NULL;
2473ed1008bSGabor Kovesdan 	lnbuflen = 0;
2484dc88ebeSGabor Kovesdan }
249