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