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