xref: /openbsd-src/usr.bin/tail/read.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: read.c,v 1.13 2008/06/02 06:01:15 otto 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$OpenBSD: read.c,v 1.13 2008/06/02 06:01:15 otto Exp $";
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/limits.h>
46 
47 #include <err.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include "extern.h"
54 
55 /*
56  * bytes -- read bytes to an offset from the end and display.
57  *
58  * This is the function that reads to a byte offset from the end of the input,
59  * storing the data in a wrap-around buffer which is then displayed.  If the
60  * rflag is set, the data is displayed in lines in reverse order, and this
61  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
62  * it is displayed from the character closest to the beginning of the input to
63  * the end.
64  *
65  * A non-zero return means an (non-fatal) error occurred.
66  *
67  */
68 int
69 bytes(FILE *fp, off_t off)
70 {
71 	int ch;
72 	size_t len, tlen;
73 	char *ep, *p, *t;
74 	int wrap;
75 	char *sp;
76 
77 	if (off > SIZE_T_MAX)
78 		errx(1, "offset too large");
79 
80 	if ((sp = p = malloc(off)) == NULL)
81 		err(1, NULL);
82 
83 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
84 		*p = ch;
85 		if (++p == ep) {
86 			wrap = 1;
87 			p = sp;
88 		}
89 	}
90 	if (ferror(fp)) {
91 		ierr();
92 		free(sp);
93 		return(1);
94 	}
95 
96 	if (rflag) {
97 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
98 			if (*t == '\n' && len) {
99 				WR(t + 1, len);
100 				len = 0;
101 		}
102 		if (wrap) {
103 			tlen = len;
104 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
105 				if (*t == '\n') {
106 					if (len) {
107 						WR(t + 1, len);
108 						len = 0;
109 					}
110 					if (tlen) {
111 						WR(sp, tlen);
112 						tlen = 0;
113 					}
114 				}
115 			if (len)
116 				WR(t + 1, len);
117 			if (tlen)
118 				WR(sp, tlen);
119 		}
120 	} else {
121 		if (wrap && (len = ep - p))
122 			WR(p, len);
123 		if ((len = p - sp))
124 			WR(sp, len);
125 	}
126 
127 	free(sp);
128 	return(0);
129 }
130 
131 /*
132  * lines -- read lines to an offset from the end and display.
133  *
134  * This is the function that reads to a line offset from the end of the input,
135  * storing the data in an array of buffers which is then displayed.  If the
136  * rflag is set, the data is displayed in lines in reverse order, and this
137  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
138  * it is displayed from the line closest to the beginning of the input to
139  * the end.
140  *
141  * A non-zero return means an (non-fatal) error occurred.
142  *
143  */
144 int
145 lines(FILE *fp, off_t off)
146 {
147 	struct {
148 		size_t blen;
149 		size_t len;
150 		char *l;
151 	} *lines;
152 	int ch, rc = 0;
153 	char *p = NULL;
154 	int wrap;
155 	size_t cnt, recno, blen, newsize;
156 	char *sp = NULL, *newp = NULL;
157 
158 	if (off > SIZE_T_MAX)
159 		errx(1, "offset too large");
160 
161 	if ((lines = calloc(off, sizeof(*lines))) == NULL)
162 		err(1, NULL);
163 
164 	blen = cnt = recno = wrap = 0;
165 
166 	while ((ch = getc(fp)) != EOF) {
167 		if (++cnt > blen) {
168 			newsize = blen + 1024;
169 			if ((newp = realloc(sp, newsize)) == NULL)
170 				err(1, NULL);
171 			sp = newp;
172 			blen = newsize;
173 			p = sp + cnt - 1;
174 		}
175 		*p++ = ch;
176 		if (ch == '\n') {
177 			if (lines[recno].blen < cnt) {
178 				newsize = cnt + 256;
179 				if ((newp = realloc(lines[recno].l,
180 				    newsize)) == NULL)
181 					err(1, NULL);
182 				lines[recno].l = newp;
183 				lines[recno].blen = newsize;
184 			}
185 			memcpy(lines[recno].l, sp, (lines[recno].len = cnt));
186 			cnt = 0;
187 			p = sp;
188 			if (++recno == off) {
189 				wrap = 1;
190 				recno = 0;
191 			}
192 		}
193 	}
194 	if (ferror(fp)) {
195 		ierr();
196 		rc = 1;
197 		goto done;
198 	}
199 	if (cnt) {
200 		lines[recno].l = sp;
201 		lines[recno].len = cnt;
202 		sp = NULL;
203 		if (++recno == off) {
204 			wrap = 1;
205 			recno = 0;
206 		}
207 	}
208 
209 	if (rflag) {
210 		for (cnt = recno; cnt > 0; --cnt)
211 			WR(lines[cnt - 1].l, lines[cnt - 1].len);
212 		if (wrap)
213 			for (cnt = off; cnt > recno; --cnt)
214 				WR(lines[cnt - 1].l, lines[cnt - 1].len);
215 	} else {
216 		if (wrap)
217 			for (cnt = recno; cnt < off; ++cnt)
218 				WR(lines[cnt].l, lines[cnt].len);
219 		for (cnt = 0; cnt < recno; ++cnt)
220 			WR(lines[cnt].l, lines[cnt].len);
221 	}
222 done:
223 	for (cnt = 0; cnt < off; cnt++)
224 		free(lines[cnt].l);
225 	free(sp);
226 	free(lines);
227 	return(rc);
228 }
229