1/* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $ */ 2 3/*- 4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav 5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 14 unchanged lines hidden (view full) --- 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> |
31__FBSDID("$FreeBSD: head/usr.bin/grep/file.c 211364 2010-08-15 22:15:04Z gabor $"); |
32 33#include <sys/param.h> 34#include <sys/types.h> 35#include <sys/stat.h> 36 37#include <bzlib.h> 38#include <err.h> 39#include <errno.h> --- 22 unchanged lines hidden (view full) --- 62 63#define iswbinary(ch) (!iswspace((ch)) && iswcntrl((ch)) && \ 64 (ch != L'\b') && (ch != L'\0')) 65 66/* 67 * Returns a single character according to the file type. 68 * Returns -1 on failure. 69 */ |
70static inline int |
71grep_fgetc(struct file *f) 72{ 73 unsigned char c; 74 75 switch (filebehave) { 76 case FILE_STDIO: |
77 return (getc_unlocked(f->f)); |
78 case FILE_GZIP: 79 return (gzgetc(f->gzf)); 80 case FILE_BZIP: 81 BZ2_bzRead(&bzerr, f->bzf, &c, 1); 82 if (bzerr == BZ_STREAM_END) 83 return (-1); 84 else if (bzerr != BZ_SEQUENCE_ERROR && bzerr != BZ_OK) 85 errx(2, "%s", getstr(2)); 86 return (c); 87 } 88 return (-1); 89} 90 91/* 92 * Returns true if the file position is a EOF, returns false 93 * otherwise. 94 */ |
95static inline int |
96grep_feof(struct file *f) 97{ 98 99 switch (filebehave) { 100 case FILE_STDIO: |
101 return (feof_unlocked(f->f)); |
102 case FILE_GZIP: 103 return (gzeof(f->gzf)); 104 case FILE_BZIP: 105 return (bzerr == BZ_STREAM_END); 106 } 107 return (1); 108} 109 --- 16 unchanged lines hidden (view full) --- 126 if (binbufptr == NULL) { 127 128 /* Only pre-read to the buffer if we need the binary check. */ 129 if (binbehave != BINFILE_TEXT) { 130 if (f->stdin) 131 st.st_size = MAXBUFSIZ; 132 else if (stat(fname, &st) != 0) 133 err(2, NULL); |
134 /* no need to allocate buffer. */ 135 if (st.st_size == 0) 136 return (NULL); |
137 138 bufsiz = (MAXBUFSIZ > (st.st_size * PREREAD_M)) ? 139 (st.st_size / 2) : MAXBUFSIZ; 140 141 binbuf = grep_malloc(sizeof(char) * bufsiz); 142 143 while (i < bufsiz) { 144 ch = grep_fgetc(f); 145 if (ch == EOF) 146 break; 147 binbuf[i++] = ch; |
148 if ((ch == '\n') && lbflag) 149 break; |
150 } 151 152 f->binary = memchr(binbuf, (filebehave != FILE_GZIP) ? 153 '\0' : '\200', i - 1) != NULL; 154 } 155 binbufsiz = i; 156 binbufptr = binbuf; 157 } --- 26 unchanged lines hidden (view full) --- 184/* 185 * Opens the standard input for processing. 186 */ 187struct file * 188grep_stdin_open(void) 189{ 190 struct file *f; 191 |
192 /* Processing stdin implies --line-buffered for tail -f to work. */ 193 lbflag = true; 194 |
195 snprintf(fname, sizeof fname, "%s", getstr(1)); 196 197 f = grep_malloc(sizeof *f); 198 |
199 binbuf = NULL; |
200 if ((f->f = fdopen(STDIN_FILENO, "r")) != NULL) { |
201 flockfile(f->f); |
202 f->stdin = true; 203 return (f); 204 } 205 206 free(f); 207 return (NULL); 208} 209 --- 4 unchanged lines hidden (view full) --- 214grep_open(const char *path) 215{ 216 struct file *f; 217 218 snprintf(fname, sizeof fname, "%s", path); 219 220 f = grep_malloc(sizeof *f); 221 |
222 binbuf = NULL; |
223 f->stdin = false; 224 switch (filebehave) { 225 case FILE_STDIO: |
226 if ((f->f = fopen(path, "r")) != NULL) { 227 flockfile(f->f); |
228 return (f); |
229 } |
230 break; 231 case FILE_GZIP: 232 if ((f->gzf = gzopen(fname, "r")) != NULL) 233 return (f); 234 break; 235 case FILE_BZIP: 236 if ((f->bzf = BZ2_bzopen(fname, "r")) != NULL) 237 return (f); --- 8 unchanged lines hidden (view full) --- 246 * Closes a normal, a gzipped or a bzip2 compressed file. 247 */ 248void 249grep_close(struct file *f) 250{ 251 252 switch (filebehave) { 253 case FILE_STDIO: |
254 funlockfile(f->f); |
255 fclose(f->f); 256 break; 257 case FILE_GZIP: 258 gzclose(f->gzf); 259 break; 260 case FILE_BZIP: 261 BZ2_bzclose(f->bzf); 262 break; 263 } 264 265 /* Reset read buffer for the file we are closing */ 266 binbufptr = NULL; 267 free(binbuf); |
268} |