tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / grep / file.c
blob3982f075cb90a4523e1b8fc796dda89254c27eb2
1 /* $OpenBSD: file.c,v 1.9 2006/02/09 09:54:46 otto Exp $ */
3 /*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
5 * All rights reserved.
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.
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.
29 #include <sys/param.h>
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
36 #include "grep.h"
38 static char fname[MAXPATHLEN];
39 #ifndef NOZ
40 static char *lnbuf;
41 static size_t lnbuflen;
42 #endif
44 #define FILE_STDIO 0
45 #define FILE_MMAP 1
46 #define FILE_GZIP 2
48 struct file {
49 int type;
50 int noseek;
51 FILE *f;
52 mmf_t *mmf;
53 #ifndef NOZ
54 gzFile *gzf;
55 #endif
58 #ifndef NOZ
59 static char *
60 gzfgetln(gzFile *f, size_t *len)
62 size_t n;
63 int c;
65 for (n = 0; ; ++n) {
66 c = gzgetc(f);
67 if (c == -1) {
68 const char *gzerrstr;
69 int gzerr;
71 if (gzeof(f))
72 break;
74 gzerrstr = gzerror(f, &gzerr);
75 if (gzerr == Z_ERRNO)
76 err(2, "%s", fname);
77 else
78 errx(2, "%s: %s", fname, gzerrstr);
80 if (n >= lnbuflen) {
81 lnbuflen *= 2;
82 lnbuf = grep_realloc(lnbuf, ++lnbuflen);
84 if (c == '\n')
85 break;
86 lnbuf[n] = c;
89 if (gzeof(f) && n == 0)
90 return NULL;
91 *len = n;
92 return lnbuf;
94 #endif
96 file_t *
97 grep_fdopen(int fd, char *mode)
99 file_t *f;
101 if (fd == STDIN_FILENO)
102 snprintf(fname, sizeof fname, "(standard input)");
103 else
104 snprintf(fname, sizeof fname, "(fd %d)", fd);
106 f = grep_malloc(sizeof *f);
108 #ifndef NOZ
109 if (Zflag) {
110 f->type = FILE_GZIP;
111 f->noseek = lseek(fd, 0L, SEEK_SET) == -1;
112 if ((f->gzf = gzdopen(fd, mode)) != NULL)
113 return f;
114 } else
115 #endif
117 f->type = FILE_STDIO;
118 f->noseek = isatty(fd);
119 if ((f->f = fdopen(fd, mode)) != NULL)
120 return f;
123 free(f);
124 return NULL;
127 file_t *
128 grep_open(char *path, char *mode)
130 file_t *f;
132 snprintf(fname, sizeof fname, "%s", path);
134 f = grep_malloc(sizeof *f);
135 f->noseek = 0;
137 #ifndef NOZ
138 if (Zflag) {
139 f->type = FILE_GZIP;
140 if ((f->gzf = gzopen(fname, mode)) != NULL)
141 return f;
142 } else
143 #endif
145 #ifdef FILE_MMAP
146 /* try mmap first; if it fails, try stdio */
147 if ((f->mmf = mmopen(fname, mode)) != NULL) {
148 f->type = FILE_MMAP;
149 return f;
151 #endif
152 f->type = FILE_STDIO;
153 if ((f->f = fopen(path, mode)) != NULL)
154 return f;
157 free(f);
158 return NULL;
162 grep_bin_file(file_t *f)
164 if (f->noseek)
165 return 0;
167 switch (f->type) {
168 case FILE_STDIO:
169 return bin_file(f->f);
170 #ifdef FILE_MMAP
171 case FILE_MMAP:
172 return mmbin_file(f->mmf);
173 #endif
174 #ifndef NOZ
175 case FILE_GZIP:
176 return gzbin_file(f->gzf);
177 #endif
178 default:
179 /* can't happen */
180 errx(2, "invalid file type");
184 char *
185 grep_fgetln(file_t *f, size_t *l)
187 switch (f->type) {
188 case FILE_STDIO:
189 return fgetln(f->f, l);
190 #ifdef FILE_MMAP
191 case FILE_MMAP:
192 return mmfgetln(f->mmf, l);
193 #endif
194 #ifndef NOZ
195 case FILE_GZIP:
196 return gzfgetln(f->gzf, l);
197 #endif
198 default:
199 /* can't happen */
200 errx(2, "invalid file type");
204 void
205 grep_close(file_t *f)
207 switch (f->type) {
208 case FILE_STDIO:
209 fclose(f->f);
210 break;
211 #ifdef FILE_MMAP
212 case FILE_MMAP:
213 mmclose(f->mmf);
214 break;
215 #endif
216 #ifndef NOZ
217 case FILE_GZIP:
218 gzclose(f->gzf);
219 break;
220 #endif
221 default:
222 /* can't happen */
223 errx(2, "invalid file type");
225 free(f);