aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/grep/file.c
blob: c4de7db6acc974842ae2af165b4c8f389ef08838 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*	$OpenBSD: file.c,v 1.11 2010/07/02 20:48:48 nicm Exp $	*/

/*-
 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <bzlib.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include <zlib.h>

#include "grep.h"

static char	 fname[MAXPATHLEN];	/* file name */

#define		 MAXBUFSIZ	(16 * 1024)
#define		 PREREAD_M	0.2

/* Some global variables for the buffering and reading. */
static char	*lnbuf;
static size_t	 lnbuflen;
static unsigned char *binbuf;
static int	 binbufsiz;
unsigned char	*binbufptr;
static int	 bzerr;

#define iswbinary(ch)	(!iswspace((ch)) && iswcntrl((ch)) && \
			    (ch != L'\b') && (ch != L'\0'))

/*
 * Returns a single character according to the file type.
 * Returns -1 on failure.
 */
int
grep_fgetc(struct file *f)
{
	unsigned char c;

	switch (filebehave) {
	case FILE_STDIO:
		return (fgetc(f->f));
	case FILE_GZIP:
		return (gzgetc(f->gzf));
	case FILE_BZIP:
		BZ2_bzRead(&bzerr, f->bzf, &c, 1);
		if (bzerr == BZ_STREAM_END)
			return (-1);
		else if (bzerr != BZ_SEQUENCE_ERROR && bzerr != BZ_OK)
			errx(2, "%s", getstr(2));
		return (c);
	}
	return (-1);
}

/*
 * Returns true if the file position is a EOF, returns false
 * otherwise.
 */
int
grep_feof(struct file *f)
{

	switch (filebehave) {
	case FILE_STDIO:
		return (feof(f->f));
	case FILE_GZIP:
		return (gzeof(f->gzf));
	case FILE_BZIP:
		return (bzerr == BZ_STREAM_END);
	}
	return (1);
}

/*
 * At the first call, fills in an internal buffer and checks if the given
 * file is a binary file and sets the binary flag accordingly.  Then returns
 * a single line and sets len to the length of the returned line.
 * At any other call returns a single line either from the internal buffer
 * or from the file if the buffer is exhausted and sets len to the length
 * of the line.
 */
char *
grep_fgetln(struct file *f, size_t *len)
{
	struct stat st;
	size_t bufsiz, i = 0;
	int ch = 0;

	/* Fill in the buffer if it is empty. */
	if (binbufptr == NULL) {

		/* Only pre-read to the buffer if we need the binary check. */
		if (binbehave != BINFILE_TEXT) {
			if (f->stdin)
				st.st_size = MAXBUFSIZ;
			else if (stat(fname, &st) != 0)
				err(2, NULL);

			bufsiz = (MAXBUFSIZ > (st.st_size * PREREAD_M)) ?
			    (st.st_size / 2) : MAXBUFSIZ;

			binbuf = grep_malloc(sizeof(char) * bufsiz);

			while (i < bufsiz) {
				ch = grep_fgetc(f);
				if (ch == EOF)
					break;
				binbuf[i++] = ch;
			}

			f->binary = memchr(binbuf, (filebehave != FILE_GZIP) ?
			    '\0' : '\200', i - 1) != NULL;
		}
		binbufsiz = i;
		binbufptr = binbuf;
	}

	/* Read a line whether from the buffer or from the file itself. */
	for (i = 0; !(grep_feof(f) &&
	    (binbufptr == &binbuf[binbufsiz])); i++) {
		if (binbufptr == &binbuf[binbufsiz]) {
			ch = grep_fgetc(f);
		} else {
			ch = binbufptr[0];
			binbufptr++;
		}
		if (i >= lnbuflen) {
			lnbuflen *= 2;
			lnbuf = grep_realloc(lnbuf, ++lnbuflen);
		}
		if ((ch == '\n') || (ch == EOF)) {
			lnbuf[i] = '\0';
			break;
		} else
			lnbuf[i] = ch;
	}
	if (grep_feof(f) && (i == 0) && (ch != '\n'))
		return (NULL);
	*len = i;
	return (lnbuf);
}

/*
 * Opens the standard input for processing.
 */
struct file *
grep_stdin_open(void)
{
	struct file *f;

	snprintf(fname, sizeof fname, "%s", getstr(1));

	f = grep_malloc(sizeof *f);

	if ((f->f = fdopen(STDIN_FILENO, "r")) != NULL) {
		f->stdin = true;
		return (f);
	}

	free(f);
	return (NULL);
}

/*
 * Opens a normal, a gzipped or a bzip2 compressed file for processing.
 */
struct file *
grep_open(const char *path)
{
	struct file *f;

	snprintf(fname, sizeof fname, "%s", path);

	f = grep_malloc(sizeof *f);

	f->stdin = false;
	switch (filebehave) {
	case FILE_STDIO:
		if ((f->f = fopen(path, "r")) != NULL)
			return (f);
		break;
	case FILE_GZIP:
		if ((f->gzf = gzopen(fname, "r")) != NULL)
			return (f);
		break;
	case FILE_BZIP:
		if ((f->bzf = BZ2_bzopen(fname, "r")) != NULL)
			return (f);
		break;
	}

	free(f);
	return (NULL);
}

/*
 * Closes a normal, a gzipped or a bzip2 compressed file.
 */
void
grep_close(struct file *f)
{

	switch (filebehave) {
	case FILE_STDIO:
		fclose(f->f);
		break;
	case FILE_GZIP:
		gzclose(f->gzf);
		break;
	case FILE_BZIP:
		BZ2_bzclose(f->bzf);
		break;
	}

	/* Reset read buffer for the file we are closing */
	binbufptr = NULL;
	free(binbuf);

}