xref: /freebsd/usr.bin/tail/read.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Sze-Tyan Wang.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 
35 __FBSDID("$FreeBSD$");
36 
37 #ifndef lint
38 static const char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "extern.h"
53 
54 /*
55  * bytes -- read bytes to an offset from the end and display.
56  *
57  * This is the function that reads to a byte offset from the end of the input,
58  * storing the data in a wrap-around buffer which is then displayed.  If the
59  * rflag is set, the data is displayed in lines in reverse order, and this
60  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
61  * it is displayed from the character closest to the beginning of the input to
62  * the end.
63  */
64 int
65 bytes(FILE *fp, const char *fn, off_t off)
66 {
67 	int ch, len, tlen;
68 	char *ep, *p, *t;
69 	int wrap;
70 	char *sp;
71 
72 	if ((sp = p = malloc(off)) == NULL)
73 		err(1, "malloc");
74 
75 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
76 		*p = ch;
77 		if (++p == ep) {
78 			wrap = 1;
79 			p = sp;
80 		}
81 	}
82 	if (ferror(fp)) {
83 		ierr(fn);
84 		free(sp);
85 		return 1;
86 	}
87 
88 	if (rflag) {
89 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
90 			if (*t == '\n' && len) {
91 				WR(t + 1, len);
92 				len = 0;
93 		}
94 		if (wrap) {
95 			tlen = len;
96 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
97 				if (*t == '\n') {
98 					if (len) {
99 						WR(t + 1, len);
100 						len = 0;
101 					}
102 					if (tlen) {
103 						WR(sp, tlen);
104 						tlen = 0;
105 					}
106 				}
107 			if (len)
108 				WR(t + 1, len);
109 			if (tlen)
110 				WR(sp, tlen);
111 		}
112 	} else {
113 		if (wrap && (len = ep - p))
114 			WR(p, len);
115 		len = p - sp;
116 		if (len)
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 int
135 lines(FILE *fp, const char *fn, off_t off)
136 {
137 	struct {
138 		int blen;
139 		u_int len;
140 		char *l;
141 	} *llines;
142 	int ch, rc;
143 	char *p, *sp;
144 	int blen, cnt, recno, wrap;
145 
146 	if ((llines = calloc(off, sizeof(*llines))) == NULL)
147 		err(1, "calloc");
148 	p = sp = NULL;
149 	blen = cnt = recno = wrap = 0;
150 	rc = 0;
151 
152 	while ((ch = getc(fp)) != EOF) {
153 		if (++cnt > blen) {
154 			if ((sp = realloc(sp, blen += 1024)) == NULL)
155 				err(1, "realloc");
156 			p = sp + cnt - 1;
157 		}
158 		*p++ = ch;
159 		if (ch == '\n') {
160 			if ((int)llines[recno].blen < cnt) {
161 				llines[recno].blen = cnt + 256;
162 				if ((llines[recno].l = realloc(llines[recno].l,
163 				    llines[recno].blen)) == NULL)
164 					err(1, "realloc");
165 			}
166 			bcopy(sp, llines[recno].l, llines[recno].len = cnt);
167 			cnt = 0;
168 			p = sp;
169 			if (++recno == off) {
170 				wrap = 1;
171 				recno = 0;
172 			}
173 		}
174 	}
175 	if (ferror(fp)) {
176 		ierr(fn);
177 		rc = 1;
178 		goto done;
179 	}
180 	if (cnt) {
181 		llines[recno].l = sp;
182 		sp = NULL;
183 		llines[recno].len = cnt;
184 		if (++recno == off) {
185 			wrap = 1;
186 			recno = 0;
187 		}
188 	}
189 
190 	if (rflag) {
191 		for (cnt = recno - 1; cnt >= 0; --cnt)
192 			WR(llines[cnt].l, llines[cnt].len);
193 		if (wrap)
194 			for (cnt = off - 1; cnt >= recno; --cnt)
195 				WR(llines[cnt].l, llines[cnt].len);
196 	} else {
197 		if (wrap)
198 			for (cnt = recno; cnt < off; ++cnt)
199 				WR(llines[cnt].l, llines[cnt].len);
200 		for (cnt = 0; cnt < recno; ++cnt)
201 			WR(llines[cnt].l, llines[cnt].len);
202 	}
203 done:
204 	for (cnt = 0; cnt < off; cnt++)
205 		free(llines[cnt].l);
206 	free(sp);
207 	free(llines);
208 	return (rc);
209 }
210