xref: /freebsd/usr.bin/tail/read.c (revision 814e3a92a9538486cd1621e811c475c932e32227)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1991, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Edward Sze-Tyan Wang.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
37814e3a92SMark Murray #include <sys/cdefs.h>
38814e3a92SMark Murray 
39814e3a92SMark Murray __FBSDID("$FreeBSD$");
40814e3a92SMark Murray 
419b50d902SRodney W. Grimes #ifndef lint
42814e3a92SMark Murray static const char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
43247e7cb1SJeroen Ruigrok van der Werven #endif
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/types.h>
469b50d902SRodney W. Grimes #include <sys/stat.h>
47814e3a92SMark Murray 
48814e3a92SMark Murray #include <err.h>
499b50d902SRodney W. Grimes #include <errno.h>
50814e3a92SMark Murray #include <fcntl.h>
519b50d902SRodney W. Grimes #include <stdio.h>
529b50d902SRodney W. Grimes #include <stdlib.h>
539b50d902SRodney W. Grimes #include <string.h>
54814e3a92SMark Murray #include <unistd.h>
55814e3a92SMark Murray 
569b50d902SRodney W. Grimes #include "extern.h"
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes /*
599b50d902SRodney W. Grimes  * bytes -- read bytes to an offset from the end and display.
609b50d902SRodney W. Grimes  *
619b50d902SRodney W. Grimes  * This is the function that reads to a byte offset from the end of the input,
629b50d902SRodney W. Grimes  * storing the data in a wrap-around buffer which is then displayed.  If the
639b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
649b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
659b50d902SRodney W. Grimes  * it is displayed from the character closest to the beginning of the input to
669b50d902SRodney W. Grimes  * the end.
679b50d902SRodney W. Grimes  */
6849a598abSAdam David int
699b50d902SRodney W. Grimes bytes(fp, off)
7048a1ef22SJeroen Ruigrok van der Werven 	FILE *fp;
719b50d902SRodney W. Grimes 	off_t off;
729b50d902SRodney W. Grimes {
7348a1ef22SJeroen Ruigrok van der Werven 	int ch, len, tlen;
7448a1ef22SJeroen Ruigrok van der Werven 	char *ep, *p, *t;
759b50d902SRodney W. Grimes 	int wrap;
769b50d902SRodney W. Grimes 	char *sp;
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes 	if ((sp = p = malloc(off)) == NULL)
79ac551270SPeter Wemm 		err(1, "malloc");
809b50d902SRodney W. Grimes 
819b50d902SRodney W. Grimes 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
829b50d902SRodney W. Grimes 		*p = ch;
839b50d902SRodney W. Grimes 		if (++p == ep) {
849b50d902SRodney W. Grimes 			wrap = 1;
859b50d902SRodney W. Grimes 			p = sp;
869b50d902SRodney W. Grimes 		}
879b50d902SRodney W. Grimes 	}
889b50d902SRodney W. Grimes 	if (ferror(fp)) {
899b50d902SRodney W. Grimes 		ierr();
9049a598abSAdam David 		return 1;
919b50d902SRodney W. Grimes 	}
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes 	if (rflag) {
949b50d902SRodney W. Grimes 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
959b50d902SRodney W. Grimes 			if (*t == '\n' && len) {
969b50d902SRodney W. Grimes 				WR(t + 1, len);
979b50d902SRodney W. Grimes 				len = 0;
989b50d902SRodney W. Grimes 		}
999b50d902SRodney W. Grimes 		if (wrap) {
1009b50d902SRodney W. Grimes 			tlen = len;
1019b50d902SRodney W. Grimes 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
1029b50d902SRodney W. Grimes 				if (*t == '\n') {
1039b50d902SRodney W. Grimes 					if (len) {
1049b50d902SRodney W. Grimes 						WR(t + 1, len);
1059b50d902SRodney W. Grimes 						len = 0;
1069b50d902SRodney W. Grimes 					}
1079b50d902SRodney W. Grimes 					if (tlen) {
1089b50d902SRodney W. Grimes 						WR(sp, tlen);
1099b50d902SRodney W. Grimes 						tlen = 0;
1109b50d902SRodney W. Grimes 					}
1119b50d902SRodney W. Grimes 				}
1129b50d902SRodney W. Grimes 			if (len)
1139b50d902SRodney W. Grimes 				WR(t + 1, len);
1149b50d902SRodney W. Grimes 			if (tlen)
1159b50d902SRodney W. Grimes 				WR(sp, tlen);
1169b50d902SRodney W. Grimes 		}
1179b50d902SRodney W. Grimes 	} else {
1189b50d902SRodney W. Grimes 		if (wrap && (len = ep - p))
1199b50d902SRodney W. Grimes 			WR(p, len);
120814e3a92SMark Murray 		len = p - sp;
121814e3a92SMark Murray 		if (len)
1229b50d902SRodney W. Grimes 			WR(sp, len);
1239b50d902SRodney W. Grimes 	}
1246439f56eSAdam David 	return 0;
1259b50d902SRodney W. Grimes }
1269b50d902SRodney W. Grimes 
1279b50d902SRodney W. Grimes /*
1289b50d902SRodney W. Grimes  * lines -- read lines to an offset from the end and display.
1299b50d902SRodney W. Grimes  *
1309b50d902SRodney W. Grimes  * This is the function that reads to a line offset from the end of the input,
1319b50d902SRodney W. Grimes  * storing the data in an array of buffers which is then displayed.  If the
1329b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
1339b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
1349b50d902SRodney W. Grimes  * it is displayed from the line closest to the beginning of the input to
1359b50d902SRodney W. Grimes  * the end.
1369b50d902SRodney W. Grimes  */
13749a598abSAdam David int
1389b50d902SRodney W. Grimes lines(fp, off)
13948a1ef22SJeroen Ruigrok van der Werven 	FILE *fp;
1409b50d902SRodney W. Grimes 	off_t off;
1419b50d902SRodney W. Grimes {
1429b50d902SRodney W. Grimes 	struct {
1439b50d902SRodney W. Grimes 		u_int blen;
1449b50d902SRodney W. Grimes 		u_int len;
1459b50d902SRodney W. Grimes 		char *l;
146814e3a92SMark Murray 	} *llines;
14748a1ef22SJeroen Ruigrok van der Werven 	int ch;
148814e3a92SMark Murray 	char *p, *sp;
149814e3a92SMark Murray 	int recno, wrap;
150814e3a92SMark Murray 	u_int cnt, blen;
1519b50d902SRodney W. Grimes 
152814e3a92SMark Murray 	if ((llines = malloc(off * sizeof(*llines))) == NULL)
153ac551270SPeter Wemm 		err(1, "malloc");
154814e3a92SMark Murray 	bzero(llines, off * sizeof(*llines));
1559b50d902SRodney W. Grimes 	sp = NULL;
1569b50d902SRodney W. Grimes 	blen = cnt = recno = wrap = 0;
1579b50d902SRodney W. Grimes 
1589b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF) {
1599b50d902SRodney W. Grimes 		if (++cnt > blen) {
1609b50d902SRodney W. Grimes 			if ((sp = realloc(sp, blen += 1024)) == NULL)
161ac551270SPeter Wemm 				err(1, "realloc");
1629b50d902SRodney W. Grimes 			p = sp + cnt - 1;
1639b50d902SRodney W. Grimes 		}
1649b50d902SRodney W. Grimes 		*p++ = ch;
1659b50d902SRodney W. Grimes 		if (ch == '\n') {
166814e3a92SMark Murray 			if (llines[recno].blen < cnt) {
167814e3a92SMark Murray 				llines[recno].blen = cnt + 256;
168814e3a92SMark Murray 				if ((llines[recno].l = realloc(llines[recno].l,
169814e3a92SMark Murray 				    llines[recno].blen)) == NULL)
170ac551270SPeter Wemm 					err(1, "realloc");
1719b50d902SRodney W. Grimes 			}
172814e3a92SMark Murray 			bcopy(sp, llines[recno].l, llines[recno].len = cnt);
1739b50d902SRodney W. Grimes 			cnt = 0;
1749b50d902SRodney W. Grimes 			p = sp;
1759b50d902SRodney W. Grimes 			if (++recno == off) {
1769b50d902SRodney W. Grimes 				wrap = 1;
1779b50d902SRodney W. Grimes 				recno = 0;
1789b50d902SRodney W. Grimes 			}
1799b50d902SRodney W. Grimes 		}
1809b50d902SRodney W. Grimes 	}
1819b50d902SRodney W. Grimes 	if (ferror(fp)) {
1829b50d902SRodney W. Grimes 		ierr();
18349a598abSAdam David 		return 1;
1849b50d902SRodney W. Grimes 	}
1859b50d902SRodney W. Grimes 	if (cnt) {
186814e3a92SMark Murray 		llines[recno].l = sp;
187814e3a92SMark Murray 		llines[recno].len = cnt;
1889b50d902SRodney W. Grimes 		if (++recno == off) {
1899b50d902SRodney W. Grimes 			wrap = 1;
1909b50d902SRodney W. Grimes 			recno = 0;
1919b50d902SRodney W. Grimes 		}
1929b50d902SRodney W. Grimes 	}
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes 	if (rflag) {
195814e3a92SMark Murray 		for (cnt = recno - 1; cnt != 0; --cnt)
196814e3a92SMark Murray 			WR(llines[cnt].l, llines[cnt].len);
1979b50d902SRodney W. Grimes 		if (wrap)
198814e3a92SMark Murray 			for (cnt = off - 1; cnt >= (u_int)recno; --cnt)
199814e3a92SMark Murray 				WR(llines[cnt].l, llines[cnt].len);
2009b50d902SRodney W. Grimes 	} else {
2019b50d902SRodney W. Grimes 		if (wrap)
202814e3a92SMark Murray 			for (cnt = recno; cnt < (u_int)off; ++cnt)
203814e3a92SMark Murray 				WR(llines[cnt].l, llines[cnt].len);
204814e3a92SMark Murray 		for (cnt = 0; cnt < (u_int)recno; ++cnt)
205814e3a92SMark Murray 			WR(llines[cnt].l, llines[cnt].len);
2069b50d902SRodney W. Grimes 	}
2076439f56eSAdam David 	return 0;
2089b50d902SRodney W. Grimes }
209