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