xref: /netbsd/usr.bin/tail/read.c (revision c4a72b64)
1 /*	$NetBSD: read.c,v 1.8 2002/06/14 00:47:41 wiz Exp $	*/
2 
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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 __RCSID("$NetBSD: read.c,v 1.8 2002/06/14 00:47:41 wiz Exp $");
45 #endif /* not lint */
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "extern.h"
56 
57 /*
58  * bytes -- read bytes to an offset from the end and display.
59  *
60  * This is the function that reads to a byte offset from the end of the input,
61  * storing the data in a wrap-around buffer which is then displayed.  If the
62  * rflag is set, the data is displayed in lines in reverse order, and this
63  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
64  * it is displayed from the character closest to the beginning of the input to
65  * the end.
66  *
67  * Non-zero return means than a (non-fatal) error occurred.
68  */
69 int
70 bytes(FILE *fp, __off_t off)
71 {
72 	int ch, len, tlen;
73 	char *ep, *p, *t;
74 	int wrap;
75 	char *sp;
76 
77 	if ((sp = p = malloc(off)) == NULL)
78 		err(1, "%s", strerror(errno));
79 
80 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
81 		*p = ch;
82 		if (++p == ep) {
83 			wrap = 1;
84 			p = sp;
85 		}
86 	}
87 	if (ferror(fp)) {
88 		ierr();
89 		return (1);
90 	}
91 
92 	if (rflag) {
93 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
94 			if (*t == '\n' && len) {
95 				WR(t + 1, len);
96 				len = 0;
97 		}
98 		if (wrap) {
99 			tlen = len;
100 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
101 				if (*t == '\n') {
102 					if (len) {
103 						WR(t + 1, len);
104 						len = 0;
105 					}
106 					if (tlen) {
107 						WR(sp, tlen);
108 						tlen = 0;
109 					}
110 				}
111 			if (len)
112 				WR(t + 1, len);
113 			if (tlen)
114 				WR(sp, tlen);
115 		}
116 	} else {
117 		if (wrap && (len = ep - p))
118 			WR(p, len);
119 		if ((len = p - sp) == 0)
120 			WR(sp, len);
121 	}
122 	return (0);
123 }
124 
125 /*
126  * lines -- read lines to an offset from the end and display.
127  *
128  * This is the function that reads to a line offset from the end of the input,
129  * storing the data in an array of buffers which is then displayed.  If the
130  * rflag is set, the data is displayed in lines in reverse order, and this
131  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
132  * it is displayed from the line closest to the beginning of the input to
133  * the end.
134  *
135  * Non-zero return means than a (non-fatal) error occurred.
136  */
137 int
138 lines(FILE *fp, __off_t off)
139 {
140 	struct {
141 		u_int blen;
142 		u_int len;
143 		char *l;
144 	} *lines;
145 	int ch;
146 	char *p;
147 	int blen, cnt, recno, wrap;
148 	char *sp;
149 
150 	p = NULL;
151 	if ((lines = malloc(off * sizeof(*lines))) == NULL)
152 		err(1, "%s", strerror(errno));
153 
154 	memset(lines, 0, sizeof(*lines) * off);
155 
156 	sp = NULL;
157 	blen = cnt = recno = wrap = 0;
158 
159 	while ((ch = getc(fp)) != EOF) {
160 		if (++cnt > blen) {
161 			if ((sp = realloc(sp, blen += 1024)) == NULL)
162 				err(1, "%s", strerror(errno));
163 			p = sp + cnt - 1;
164 		}
165 		*p++ = ch;
166 		if (ch == '\n') {
167 			if (lines[recno].blen < cnt) {
168 				lines[recno].blen = cnt + 256;
169 				if ((lines[recno].l = realloc(lines[recno].l,
170 				    lines[recno].blen)) == NULL)
171 					err(1, "%s", strerror(errno));
172 			}
173 			memmove(lines[recno].l, sp, lines[recno].len = cnt);
174 			cnt = 0;
175 			p = sp;
176 			if (++recno == off) {
177 				wrap = 1;
178 				recno = 0;
179 			}
180 		}
181 	}
182 	if (ferror(fp)) {
183 		ierr();
184 		return (1);
185 	}
186 	if (cnt) {
187 		lines[recno].l = sp;
188 		lines[recno].len = cnt;
189 		if (++recno == off) {
190 			wrap = 1;
191 			recno = 0;
192 		}
193 	}
194 
195 	if (rflag) {
196 		for (cnt = recno - 1; cnt >= 0; --cnt)
197 			WR(lines[cnt].l, lines[cnt].len);
198 		if (wrap)
199 			for (cnt = off - 1; cnt >= recno; --cnt)
200 				WR(lines[cnt].l, lines[cnt].len);
201 	} else {
202 		if (wrap)
203 			for (cnt = recno; cnt < off; ++cnt)
204 				WR(lines[cnt].l, lines[cnt].len);
205 		for (cnt = 0; cnt < recno; ++cnt)
206 			WR(lines[cnt].l, lines[cnt].len);
207 	}
208 	return (0);
209 }
210