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 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <zlib.h>
35 #ifdef __minix
36 #include <unistd.h> /* isatty */
37 #endif /* __minix */
38
39 #include "grep.h"
40
41 static char fname[MAXPATHLEN];
42 #ifndef NOZ
43 static char *lnbuf;
44 static size_t lnbuflen;
45 #endif
46
47 #define FILE_STDIO 0
48 #define FILE_MMAP 1
49 #define FILE_GZIP 2
50
51 struct file {
52 int type;
53 int noseek;
54 FILE *f;
55 mmf_t *mmf;
56 gzFile *gzf;
57 };
58
59 #ifndef NOZ
60 static char *
gzfgetln(gzFile * f,size_t * len)61 gzfgetln(gzFile *f, size_t *len)
62 {
63 size_t n;
64 int c;
65
66 for (n = 0; ; ++n) {
67 c = gzgetc(f);
68 if (c == -1) {
69 const char *gzerrstr;
70 int gzerr;
71
72 if (gzeof(f))
73 break;
74
75 gzerrstr = gzerror(f, &gzerr);
76 if (gzerr == Z_ERRNO)
77 err(2, "%s", fname);
78 else
79 errx(2, "%s: %s", fname, gzerrstr);
80 }
81 if (n >= lnbuflen) {
82 lnbuflen *= 2;
83 lnbuf = grep_realloc(lnbuf, ++lnbuflen);
84 }
85 if (c == '\n')
86 break;
87 lnbuf[n] = c;
88 }
89
90 if (gzeof(f) && n == 0)
91 return NULL;
92 *len = n;
93 return lnbuf;
94 }
95 #endif
96
97 file_t *
grep_fdopen(int fd,const char * mode)98 grep_fdopen(int fd, const char *mode)
99 {
100 file_t *f;
101
102 if (fd == STDIN_FILENO)
103 snprintf(fname, sizeof fname, "(standard input)");
104 else
105 snprintf(fname, sizeof fname, "(fd %d)", fd);
106
107 f = grep_malloc(sizeof *f);
108
109 #ifndef NOZ
110 if (Zflag) {
111 f->type = FILE_GZIP;
112 f->noseek = lseek(fd, 0L, SEEK_SET) == -1;
113 if ((f->gzf = gzdopen(fd, mode)) != NULL)
114 return f;
115 } else
116 #endif
117 {
118 f->type = FILE_STDIO;
119 f->noseek = isatty(fd);
120 if ((f->f = fdopen(fd, mode)) != NULL)
121 return f;
122 }
123
124 free(f);
125 return NULL;
126 }
127
128 file_t *
grep_open(const char * path,const char * mode)129 grep_open(const char *path, const char *mode)
130 {
131 file_t *f;
132
133 snprintf(fname, sizeof fname, "%s", path);
134
135 f = grep_malloc(sizeof *f);
136 f->noseek = 0;
137
138 #ifndef NOZ
139 if (Zflag) {
140 f->type = FILE_GZIP;
141 if ((f->gzf = gzopen(fname, mode)) != NULL)
142 return f;
143 } else
144 #endif
145 {
146 #ifndef SMALL
147 /* try mmap first; if it fails, try stdio */
148 if ((f->mmf = mmopen(fname, mode)) != NULL) {
149 f->type = FILE_MMAP;
150 return f;
151 }
152 #endif
153 f->type = FILE_STDIO;
154 if ((f->f = fopen(path, mode)) != NULL)
155 return f;
156 }
157
158 free(f);
159 return NULL;
160 }
161
162 int
grep_bin_file(file_t * f)163 grep_bin_file(file_t *f)
164 {
165 if (f->noseek)
166 return 0;
167
168 switch (f->type) {
169 case FILE_STDIO:
170 return bin_file(f->f);
171 #ifndef SMALL
172 case FILE_MMAP:
173 return mmbin_file(f->mmf);
174 #endif
175 #ifndef NOZ
176 case FILE_GZIP:
177 return gzbin_file(f->gzf);
178 #endif
179 default:
180 /* can't happen */
181 errx(2, "invalid file type");
182 }
183 }
184
185 char *
grep_fgetln(file_t * f,size_t * l)186 grep_fgetln(file_t *f, size_t *l)
187 {
188 switch (f->type) {
189 case FILE_STDIO:
190 return fgetln(f->f, l);
191 #ifndef SMALL
192 case FILE_MMAP:
193 return mmfgetln(f->mmf, l);
194 #endif
195 #ifndef NOZ
196 case FILE_GZIP:
197 return gzfgetln(f->gzf, l);
198 #endif
199 default:
200 /* can't happen */
201 errx(2, "invalid file type");
202 }
203 }
204
205 void
grep_close(file_t * f)206 grep_close(file_t *f)
207 {
208 switch (f->type) {
209 case FILE_STDIO:
210 fclose(f->f);
211 break;
212 #ifndef SMALL
213 case FILE_MMAP:
214 mmclose(f->mmf);
215 break;
216 #endif
217 #ifndef NOZ
218 case FILE_GZIP:
219 gzclose(f->gzf);
220 break;
221 #endif
222 default:
223 /* can't happen */
224 errx(2, "invalid file type");
225 }
226 free(f);
227 }
228