GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tail/read.c Lines: 37 83 44.6 %
Date: 2017-11-07 Branches: 32 116 27.6 %

Line Branch Exec Source
1
/*	$OpenBSD: read.c,v 1.20 2017/03/26 19:55:07 martijn Exp $	*/
2
/*	$NetBSD: read.c,v 1.4 1994/11/23 07:42:07 jtc Exp $	*/
3
4
/*-
5
 * Copyright (c) 1991, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Edward Sze-Tyan Wang.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 */
35
36
#include <sys/types.h>
37
#include <sys/stat.h>
38
39
#include <err.h>
40
#include <stdio.h>
41
#include <stdint.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <unistd.h>
45
46
#include "extern.h"
47
48
/*
49
 * bytes -- read bytes to an offset from the end and display.
50
 *
51
 * This is the function that reads to a byte offset from the end of the input,
52
 * storing the data in a wrap-around buffer which is then displayed.  If the
53
 * rflag is set, the data is displayed in lines in reverse order, and this
54
 * routine has the usual nastiness of trying to find the newlines.  Otherwise,
55
 * it is displayed from the character closest to the beginning of the input to
56
 * the end.
57
 *
58
 * A non-zero return means an (non-fatal) error occurred.
59
 *
60
 */
61
int
62
bytes(struct tailfile *tf, off_t off)
63
{
64
	int ch;
65
	size_t len, tlen;
66
	char *ep, *p, *t;
67
	int wrap;
68
	char *sp;
69
70
	if (off > SIZE_MAX)
71
		errx(1, "offset too large");
72
73
	if ((sp = p = malloc(off)) == NULL)
74
		err(1, NULL);
75
76
	for (wrap = 0, ep = p + off; (ch = getc(tf->fp)) != EOF;) {
77
		*p = ch;
78
		if (++p == ep) {
79
			wrap = 1;
80
			p = sp;
81
		}
82
	}
83
	if (ferror(tf->fp)) {
84
		ierr(tf->fname);
85
		free(sp);
86
		return(1);
87
	}
88
89
	if (rflag) {
90
		for (t = p - 1, len = 0; t >= sp; --t, ++len)
91
			if (*t == '\n' && len) {
92
				WR(t + 1, len);
93
				len = 0;
94
			}
95
		if (wrap) {
96
			tlen = len;
97
			for (t = ep - 1, len = 0; t >= p; --t, ++len)
98
				if (*t == '\n') {
99
					if (len) {
100
						WR(t + 1, len);
101
						len = 0;
102
					}
103
					if (tlen) {
104
						WR(sp, tlen);
105
						tlen = 0;
106
					}
107
				}
108
			if (len)
109
				WR(t + 1, len);
110
			if (tlen)
111
				WR(sp, tlen);
112
		}
113
	} else {
114
		if (wrap && (len = ep - p))
115
			WR(p, len);
116
		if ((len = p - sp))
117
			WR(sp, len);
118
	}
119
120
	free(sp);
121
	return(0);
122
}
123
124
/*
125
 * lines -- read lines to an offset from the end and display.
126
 *
127
 * This is the function that reads to a line offset from the end of the input,
128
 * storing the data in an array of buffers which is then displayed.  If the
129
 * rflag is set, the data is displayed in lines in reverse order, and this
130
 * routine has the usual nastiness of trying to find the newlines.  Otherwise,
131
 * it is displayed from the line closest to the beginning of the input to
132
 * the end.
133
 *
134
 * A non-zero return means an (non-fatal) error occurred.
135
 *
136
 */
137
int
138
lines(struct tailfile *tf, off_t off)
139
{
140
	struct {
141
		size_t blen;
142
		size_t len;
143
		char *l;
144
	} *lines = NULL;
145
	int ch, rc = 0;
146
	char *p = NULL;
147
	int wrap;
148
	size_t cnt, lineno, nlineno, recno, blen, newsize;
149
	char *sp = NULL, *newp = NULL;
150
151
228
	if (off > SIZE_MAX)
152
		errx(1, "offset too large");
153
154
	lineno = blen = cnt = recno = wrap = 0;
155
156

66362
	while ((ch = getc(tf->fp)) != EOF) {
157
16448
		if (++cnt > blen) {
158
114
			newsize = blen + 1024;
159
114
			if ((newp = realloc(sp, newsize)) == NULL)
160
				err(1, NULL);
161
			sp = newp;
162
			blen = newsize;
163
114
			p = sp + cnt - 1;
164
114
		}
165
16448
		*p++ = ch;
166
16448
		if (recno >= lineno) {
167
114
			nlineno = (lineno + 1024) > off ?
168
			    (size_t) off : lineno + 1024;
169
228
			if ((lines = recallocarray(lines, lineno, nlineno,
170
114
			    sizeof(*lines))) == NULL)
171
				err(1, NULL);
172
			lineno = nlineno;
173
114
		}
174
16448
		if (ch == '\n') {
175
1115
			if (lines[recno].blen < cnt) {
176
118
				newsize = cnt + 256;
177
236
				if ((newp = realloc(lines[recno].l,
178
118
				    newsize)) == NULL)
179
					err(1, NULL);
180
118
				lines[recno].l = newp;
181
118
				lines[recno].blen = newsize;
182
118
			}
183
1115
			memcpy(lines[recno].l, sp, (lines[recno].len = cnt));
184
			cnt = 0;
185
			p = sp;
186
1115
			if (++recno == off) {
187
				wrap = 1;
188
				recno = 0;
189
1109
			}
190
		}
191
	}
192

228
	if (ferror(tf->fp)) {
193
		ierr(tf->fname);
194
		rc = 1;
195
		goto done;
196
	}
197
114
	if (cnt) {
198
		lines[recno].l = sp;
199
		lines[recno].len = cnt;
200
		sp = NULL;
201
		if (++recno == off) {
202
			wrap = 1;
203
			recno = 0;
204
		}
205
	}
206
207
114
	if (rflag) {
208
		for (cnt = recno; cnt > 0; --cnt)
209
			WR(lines[cnt - 1].l, lines[cnt - 1].len);
210
		if (wrap)
211
			for (cnt = off; cnt > recno; --cnt)
212
				WR(lines[cnt - 1].l, lines[cnt - 1].len);
213
	} else {
214
114
		if (wrap)
215
460
			for (cnt = recno; cnt < off; ++cnt)
216
116
				WR(lines[cnt].l, lines[cnt].len);
217
232
		for (cnt = 0; cnt < recno; ++cnt)
218
2
			WR(lines[cnt].l, lines[cnt].len);
219
	}
220
done:
221
464
	for (cnt = 0; cnt < lineno; cnt++)
222
118
		free(lines[cnt].l);
223
114
	free(sp);
224
114
	free(lines);
225
114
	return(rc);
226
}