etc/services - sync with NetBSD-8
[minix.git] / minix / usr.bin / grep / file.c
bloba1065222858d4a8d0f1d1c63a2d5c5aa292cde3f
1 /* $OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm 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 <zlib.h>
35 #ifdef __minix
36 #include <unistd.h> /* isatty */
37 #endif /* __minix */
39 #include "grep.h"
41 static char fname[MAXPATHLEN];
42 #ifndef NOZ
43 static char *lnbuf;
44 static size_t lnbuflen;
45 #endif
47 #define FILE_STDIO 0
48 #define FILE_MMAP 1
49 #define FILE_GZIP 2
51 struct file {
52 int type;
53 int noseek;
54 FILE *f;
55 mmf_t *mmf;
56 gzFile *gzf;
59 #ifndef NOZ
60 static char *
61 gzfgetln(gzFile *f, size_t *len)
63 size_t n;
64 int c;
66 for (n = 0; ; ++n) {
67 c = gzgetc(f);
68 if (c == -1) {
69 const char *gzerrstr;
70 int gzerr;
72 if (gzeof(f))
73 break;
75 gzerrstr = gzerror(f, &gzerr);
76 if (gzerr == Z_ERRNO)
77 err(2, "%s", fname);
78 else
79 errx(2, "%s: %s", fname, gzerrstr);
81 if (n >= lnbuflen) {
82 lnbuflen *= 2;
83 lnbuf = grep_realloc(lnbuf, ++lnbuflen);
85 if (c == '\n')
86 break;
87 lnbuf[n] = c;
90 if (gzeof(f) && n == 0)
91 return NULL;
92 *len = n;
93 return lnbuf;
95 #endif
97 file_t *
98 grep_fdopen(int fd, const char *mode)
100 file_t *f;
102 if (fd == STDIN_FILENO)
103 snprintf(fname, sizeof fname, "(standard input)");
104 else
105 snprintf(fname, sizeof fname, "(fd %d)", fd);
107 f = grep_malloc(sizeof *f);
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
118 f->type = FILE_STDIO;
119 f->noseek = isatty(fd);
120 if ((f->f = fdopen(fd, mode)) != NULL)
121 return f;
124 free(f);
125 return NULL;
128 file_t *
129 grep_open(const char *path, const char *mode)
131 file_t *f;
133 snprintf(fname, sizeof fname, "%s", path);
135 f = grep_malloc(sizeof *f);
136 f->noseek = 0;
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
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;
152 #endif
153 f->type = FILE_STDIO;
154 if ((f->f = fopen(path, mode)) != NULL)
155 return f;
158 free(f);
159 return NULL;
163 grep_bin_file(file_t *f)
165 if (f->noseek)
166 return 0;
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");
185 char *
186 grep_fgetln(file_t *f, size_t *l)
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");
205 void
206 grep_close(file_t *f)
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");
226 free(f);