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 <gabor@FreeBSD.org> 9 * Copyright (C) 2010 Dimitry Andric <dimitry@andric.com> 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 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) 76 return (-1); 77 78 bufrem = nr; 79 return (0); 80 } 81 82 static inline int 83 grep_lnbufgrow(size_t newlen) 84 { 85 86 if (lnbuflen < newlen) { 87 lnbuf = grep_realloc(lnbuf, newlen); 88 lnbuflen = newlen; 89 } 90 91 return (0); 92 } 93 94 char * 95 grep_fgetln(struct file *f, struct parsec *pc) 96 { 97 char *p; 98 size_t len; 99 size_t off; 100 ptrdiff_t diff; 101 102 /* Fill the buffer, if necessary */ 103 if (bufrem == 0 && grep_refill(f) != 0) 104 goto error; 105 106 if (bufrem == 0) { 107 /* Return zero length to indicate EOF */ 108 pc->ln.len= 0; 109 return (bufpos); 110 } 111 112 /* Look for a newline in the remaining part of the buffer */ 113 if ((p = memchr(bufpos, fileeol, bufrem)) != NULL) { 114 ++p; /* advance over newline */ 115 len = p - bufpos; 116 if (grep_lnbufgrow(len + 1)) 117 goto error; 118 memcpy(lnbuf, bufpos, len); 119 bufrem -= len; 120 bufpos = p; 121 pc->ln.len = len; 122 lnbuf[len] = '\0'; 123 return (lnbuf); 124 } 125 126 /* We have to copy the current buffered data to the line buffer */ 127 for (len = bufrem, off = 0; ; len += bufrem) { 128 /* Make sure there is room for more data */ 129 if (grep_lnbufgrow(len + LNBUFBUMP)) 130 goto error; 131 memcpy(lnbuf + off, bufpos, len - off); 132 /* With FILE_MMAP, this is EOF; there's no more to refill */ 133 if (filebehave == FILE_MMAP) { 134 bufrem -= len; 135 break; 136 } 137 off = len; 138 /* Fetch more to try and find EOL/EOF */ 139 if (grep_refill(f) != 0) 140 goto error; 141 if (bufrem == 0) 142 /* EOF: return partial line */ 143 break; 144 if ((p = memchr(bufpos, fileeol, bufrem)) == NULL) 145 continue; 146 /* got it: finish up the line (like code above) */ 147 ++p; 148 diff = p - bufpos; 149 len += diff; 150 if (grep_lnbufgrow(len + 1)) 151 goto error; 152 memcpy(lnbuf + off, bufpos, diff); 153 bufrem -= diff; 154 bufpos = p; 155 break; 156 } 157 pc->ln.len = len; 158 lnbuf[len] = '\0'; 159 return (lnbuf); 160 161 error: 162 pc->ln.len = 0; 163 return (NULL); 164 } 165 166 /* 167 * Opens a file for processing. 168 */ 169 struct file * 170 grep_open(const char *path) 171 { 172 struct file *f; 173 174 f = grep_malloc(sizeof *f); 175 memset(f, 0, sizeof *f); 176 if (path == NULL) { 177 /* Processing stdin implies --line-buffered. */ 178 lbflag = true; 179 f->fd = STDIN_FILENO; 180 } else if ((f->fd = open(path, O_RDONLY)) == -1) 181 goto error1; 182 183 if (filebehave == FILE_MMAP) { 184 struct stat st; 185 186 if (fstat(f->fd, &st) == -1 || !S_ISREG(st.st_mode)) 187 filebehave = FILE_STDIO; 188 else { 189 int flags = MAP_PRIVATE | MAP_NOCORE | MAP_NOSYNC; 190 #ifdef MAP_PREFAULT_READ 191 flags |= MAP_PREFAULT_READ; 192 #endif 193 fsiz = st.st_size; 194 buffer = mmap(NULL, fsiz, PROT_READ, flags, 195 f->fd, (off_t)0); 196 if (buffer == MAP_FAILED) 197 filebehave = FILE_STDIO; 198 else { 199 bufrem = st.st_size; 200 bufpos = buffer; 201 madvise(buffer, st.st_size, MADV_SEQUENTIAL); 202 } 203 } 204 } 205 206 if ((buffer == NULL) || (buffer == MAP_FAILED)) 207 buffer = grep_malloc(MAXBUFSIZ); 208 209 /* Fill read buffer, also catches errors early */ 210 if (bufrem == 0 && grep_refill(f) != 0) 211 goto error2; 212 213 /* Check for binary stuff, if necessary */ 214 if (binbehave != BINFILE_TEXT && fileeol != '\0' && 215 memchr(bufpos, '\0', bufrem) != NULL) 216 f->binary = true; 217 218 return (f); 219 220 error2: 221 close(f->fd); 222 error1: 223 free(f); 224 return (NULL); 225 } 226 227 /* 228 * Closes a file. 229 */ 230 void 231 grep_close(struct file *f) 232 { 233 234 close(f->fd); 235 236 /* Reset read buffer and line buffer */ 237 if (filebehave == FILE_MMAP) { 238 munmap(buffer, fsiz); 239 buffer = NULL; 240 } 241 bufpos = buffer; 242 bufrem = 0; 243 244 free(lnbuf); 245 lnbuf = NULL; 246 lnbuflen = 0; 247 } 248