xref: /netbsd/usr.bin/tail/read.c (revision bf9ec67e)
1 /*	$NetBSD: read.c,v 1.7 1999/07/21 06:38:50 cgd 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.7 1999/07/21 06:38:50 cgd 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(fp, off)
71 	FILE *fp;
72 	off_t off;
73 {
74 	int ch, len, tlen;
75 	char *ep, *p, *t;
76 	int wrap;
77 	char *sp;
78 
79 	if ((sp = p = malloc(off)) == NULL)
80 		err(1, "%s", strerror(errno));
81 
82 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
83 		*p = ch;
84 		if (++p == ep) {
85 			wrap = 1;
86 			p = sp;
87 		}
88 	}
89 	if (ferror(fp)) {
90 		ierr();
91 		return (1);
92 	}
93 
94 	if (rflag) {
95 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
96 			if (*t == '\n' && len) {
97 				WR(t + 1, len);
98 				len = 0;
99 		}
100 		if (wrap) {
101 			tlen = len;
102 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
103 				if (*t == '\n') {
104 					if (len) {
105 						WR(t + 1, len);
106 						len = 0;
107 					}
108 					if (tlen) {
109 						WR(sp, tlen);
110 						tlen = 0;
111 					}
112 				}
113 			if (len)
114 				WR(t + 1, len);
115 			if (tlen)
116 				WR(sp, tlen);
117 		}
118 	} else {
119 		if (wrap && (len = ep - p))
120 			WR(p, len);
121 		if ((len = p - sp) == 0)
122 			WR(sp, len);
123 	}
124 	return (0);
125 }
126 
127 /*
128  * lines -- read lines to an offset from the end and display.
129  *
130  * This is the function that reads to a line offset from the end of the input,
131  * storing the data in an array of buffers which is then displayed.  If the
132  * rflag is set, the data is displayed in lines in reverse order, and this
133  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
134  * it is displayed from the line closest to the beginning of the input to
135  * the end.
136  *
137  * Non-zero return means than a (non-fatal) error occurred.
138  */
139 int
140 lines(fp, off)
141 	FILE *fp;
142 	off_t off;
143 {
144 	struct {
145 		u_int blen;
146 		u_int len;
147 		char *l;
148 	} *lines;
149 	int ch;
150 	char *p;
151 	int blen, cnt, recno, wrap;
152 	char *sp;
153 
154 	p = NULL;
155 	if ((lines = malloc(off * sizeof(*lines))) == NULL)
156 		err(1, "%s", strerror(errno));
157 
158 	memset(lines, 0, sizeof(*lines) * off);
159 
160 	sp = NULL;
161 	blen = cnt = recno = wrap = 0;
162 
163 	while ((ch = getc(fp)) != EOF) {
164 		if (++cnt > blen) {
165 			if ((sp = realloc(sp, blen += 1024)) == NULL)
166 				err(1, "%s", strerror(errno));
167 			p = sp + cnt - 1;
168 		}
169 		*p++ = ch;
170 		if (ch == '\n') {
171 			if (lines[recno].blen < cnt) {
172 				lines[recno].blen = cnt + 256;
173 				if ((lines[recno].l = realloc(lines[recno].l,
174 				    lines[recno].blen)) == NULL)
175 					err(1, "%s", strerror(errno));
176 			}
177 			memmove(lines[recno].l, sp, lines[recno].len = cnt);
178 			cnt = 0;
179 			p = sp;
180 			if (++recno == off) {
181 				wrap = 1;
182 				recno = 0;
183 			}
184 		}
185 	}
186 	if (ferror(fp)) {
187 		ierr();
188 		return (1);
189 	}
190 	if (cnt) {
191 		lines[recno].l = sp;
192 		lines[recno].len = cnt;
193 		if (++recno == off) {
194 			wrap = 1;
195 			recno = 0;
196 		}
197 	}
198 
199 	if (rflag) {
200 		for (cnt = recno - 1; cnt >= 0; --cnt)
201 			WR(lines[cnt].l, lines[cnt].len);
202 		if (wrap)
203 			for (cnt = off - 1; cnt >= recno; --cnt)
204 				WR(lines[cnt].l, lines[cnt].len);
205 	} else {
206 		if (wrap)
207 			for (cnt = recno; cnt < off; ++cnt)
208 				WR(lines[cnt].l, lines[cnt].len);
209 		for (cnt = 0; cnt < recno; ++cnt)
210 			WR(lines[cnt].l, lines[cnt].len);
211 	}
212 	return (0);
213 }
214