xref: /freebsd/usr.bin/tail/read.c (revision 48a1ef22849bc50fdfcdbccfd1d44b116d14f50a)
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 
379b50d902SRodney W. Grimes #ifndef lint
38247e7cb1SJeroen Ruigrok van der Werven #if 0
399b50d902SRodney W. Grimes static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
40247e7cb1SJeroen Ruigrok van der Werven #endif
41247e7cb1SJeroen Ruigrok van der Werven static const char rcsid[] =
42247e7cb1SJeroen Ruigrok van der Werven   "$FreeBSD$";
439b50d902SRodney W. Grimes #endif /* not lint */
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/types.h>
469b50d902SRodney W. Grimes #include <sys/stat.h>
479b50d902SRodney W. Grimes #include <fcntl.h>
489b50d902SRodney W. Grimes #include <errno.h>
499b50d902SRodney W. Grimes #include <unistd.h>
509b50d902SRodney W. Grimes #include <stdio.h>
519b50d902SRodney W. Grimes #include <stdlib.h>
529b50d902SRodney W. Grimes #include <string.h>
53c3fd5728SPeter Wemm #include <err.h>
549b50d902SRodney W. Grimes #include "extern.h"
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes /*
579b50d902SRodney W. Grimes  * bytes -- read bytes to an offset from the end and display.
589b50d902SRodney W. Grimes  *
599b50d902SRodney W. Grimes  * This is the function that reads to a byte offset from the end of the input,
609b50d902SRodney W. Grimes  * storing the data in a wrap-around buffer which is then displayed.  If the
619b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
629b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
639b50d902SRodney W. Grimes  * it is displayed from the character closest to the beginning of the input to
649b50d902SRodney W. Grimes  * the end.
659b50d902SRodney W. Grimes  */
6649a598abSAdam David int
679b50d902SRodney W. Grimes bytes(fp, off)
6848a1ef22SJeroen Ruigrok van der Werven 	FILE *fp;
699b50d902SRodney W. Grimes 	off_t off;
709b50d902SRodney W. Grimes {
7148a1ef22SJeroen Ruigrok van der Werven 	int ch, len, tlen;
7248a1ef22SJeroen Ruigrok van der Werven 	char *ep, *p, *t;
739b50d902SRodney W. Grimes 	int wrap;
749b50d902SRodney W. Grimes 	char *sp;
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes 	if ((sp = p = malloc(off)) == NULL)
77ac551270SPeter Wemm 		err(1, "malloc");
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
809b50d902SRodney W. Grimes 		*p = ch;
819b50d902SRodney W. Grimes 		if (++p == ep) {
829b50d902SRodney W. Grimes 			wrap = 1;
839b50d902SRodney W. Grimes 			p = sp;
849b50d902SRodney W. Grimes 		}
859b50d902SRodney W. Grimes 	}
869b50d902SRodney W. Grimes 	if (ferror(fp)) {
879b50d902SRodney W. Grimes 		ierr();
8849a598abSAdam David 		return 1;
899b50d902SRodney W. Grimes 	}
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes 	if (rflag) {
929b50d902SRodney W. Grimes 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
939b50d902SRodney W. Grimes 			if (*t == '\n' && len) {
949b50d902SRodney W. Grimes 				WR(t + 1, len);
959b50d902SRodney W. Grimes 				len = 0;
969b50d902SRodney W. Grimes 		}
979b50d902SRodney W. Grimes 		if (wrap) {
989b50d902SRodney W. Grimes 			tlen = len;
999b50d902SRodney W. Grimes 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
1009b50d902SRodney W. Grimes 				if (*t == '\n') {
1019b50d902SRodney W. Grimes 					if (len) {
1029b50d902SRodney W. Grimes 						WR(t + 1, len);
1039b50d902SRodney W. Grimes 						len = 0;
1049b50d902SRodney W. Grimes 					}
1059b50d902SRodney W. Grimes 					if (tlen) {
1069b50d902SRodney W. Grimes 						WR(sp, tlen);
1079b50d902SRodney W. Grimes 						tlen = 0;
1089b50d902SRodney W. Grimes 					}
1099b50d902SRodney W. Grimes 				}
1109b50d902SRodney W. Grimes 			if (len)
1119b50d902SRodney W. Grimes 				WR(t + 1, len);
1129b50d902SRodney W. Grimes 			if (tlen)
1139b50d902SRodney W. Grimes 				WR(sp, tlen);
1149b50d902SRodney W. Grimes 		}
1159b50d902SRodney W. Grimes 	} else {
1169b50d902SRodney W. Grimes 		if (wrap && (len = ep - p))
1179b50d902SRodney W. Grimes 			WR(p, len);
1189b50d902SRodney W. Grimes 		if (len = p - sp)
1199b50d902SRodney W. Grimes 			WR(sp, len);
1209b50d902SRodney W. Grimes 	}
1216439f56eSAdam David 	return 0;
1229b50d902SRodney W. Grimes }
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes /*
1259b50d902SRodney W. Grimes  * lines -- read lines to an offset from the end and display.
1269b50d902SRodney W. Grimes  *
1279b50d902SRodney W. Grimes  * This is the function that reads to a line offset from the end of the input,
1289b50d902SRodney W. Grimes  * storing the data in an array of buffers which is then displayed.  If the
1299b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
1309b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
1319b50d902SRodney W. Grimes  * it is displayed from the line closest to the beginning of the input to
1329b50d902SRodney W. Grimes  * the end.
1339b50d902SRodney W. Grimes  */
13449a598abSAdam David int
1359b50d902SRodney W. Grimes lines(fp, off)
13648a1ef22SJeroen Ruigrok van der Werven 	FILE *fp;
1379b50d902SRodney W. Grimes 	off_t off;
1389b50d902SRodney W. Grimes {
1399b50d902SRodney W. Grimes 	struct {
1409b50d902SRodney W. Grimes 		u_int blen;
1419b50d902SRodney W. Grimes 		u_int len;
1429b50d902SRodney W. Grimes 		char *l;
1439b50d902SRodney W. Grimes 	} *lines;
14448a1ef22SJeroen Ruigrok van der Werven 	int ch;
14548a1ef22SJeroen Ruigrok van der Werven 	char *p;
1469b50d902SRodney W. Grimes 	int blen, cnt, recno, wrap;
1479b50d902SRodney W. Grimes 	char *sp;
1489b50d902SRodney W. Grimes 
1499b50d902SRodney W. Grimes 	if ((lines = malloc(off * sizeof(*lines))) == NULL)
150ac551270SPeter Wemm 		err(1, "malloc");
151f96ffb5aSPoul-Henning Kamp 	bzero(lines, off * sizeof(*lines));
1529b50d902SRodney W. Grimes 	sp = NULL;
1539b50d902SRodney W. Grimes 	blen = cnt = recno = wrap = 0;
1549b50d902SRodney W. Grimes 
1559b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF) {
1569b50d902SRodney W. Grimes 		if (++cnt > blen) {
1579b50d902SRodney W. Grimes 			if ((sp = realloc(sp, blen += 1024)) == NULL)
158ac551270SPeter Wemm 				err(1, "realloc");
1599b50d902SRodney W. Grimes 			p = sp + cnt - 1;
1609b50d902SRodney W. Grimes 		}
1619b50d902SRodney W. Grimes 		*p++ = ch;
1629b50d902SRodney W. Grimes 		if (ch == '\n') {
1639b50d902SRodney W. Grimes 			if (lines[recno].blen < cnt) {
1649b50d902SRodney W. Grimes 				lines[recno].blen = cnt + 256;
1659b50d902SRodney W. Grimes 				if ((lines[recno].l = realloc(lines[recno].l,
1669b50d902SRodney W. Grimes 				    lines[recno].blen)) == NULL)
167ac551270SPeter Wemm 					err(1, "realloc");
1689b50d902SRodney W. Grimes 			}
1699b50d902SRodney W. Grimes 			bcopy(sp, lines[recno].l, lines[recno].len = cnt);
1709b50d902SRodney W. Grimes 			cnt = 0;
1719b50d902SRodney W. Grimes 			p = sp;
1729b50d902SRodney W. Grimes 			if (++recno == off) {
1739b50d902SRodney W. Grimes 				wrap = 1;
1749b50d902SRodney W. Grimes 				recno = 0;
1759b50d902SRodney W. Grimes 			}
1769b50d902SRodney W. Grimes 		}
1779b50d902SRodney W. Grimes 	}
1789b50d902SRodney W. Grimes 	if (ferror(fp)) {
1799b50d902SRodney W. Grimes 		ierr();
18049a598abSAdam David 		return 1;
1819b50d902SRodney W. Grimes 	}
1829b50d902SRodney W. Grimes 	if (cnt) {
1839b50d902SRodney W. Grimes 		lines[recno].l = sp;
1849b50d902SRodney W. Grimes 		lines[recno].len = cnt;
1859b50d902SRodney W. Grimes 		if (++recno == off) {
1869b50d902SRodney W. Grimes 			wrap = 1;
1879b50d902SRodney W. Grimes 			recno = 0;
1889b50d902SRodney W. Grimes 		}
1899b50d902SRodney W. Grimes 	}
1909b50d902SRodney W. Grimes 
1919b50d902SRodney W. Grimes 	if (rflag) {
1929b50d902SRodney W. Grimes 		for (cnt = recno - 1; cnt >= 0; --cnt)
1939b50d902SRodney W. Grimes 			WR(lines[cnt].l, lines[cnt].len);
1949b50d902SRodney W. Grimes 		if (wrap)
1959b50d902SRodney W. Grimes 			for (cnt = off - 1; cnt >= recno; --cnt)
1969b50d902SRodney W. Grimes 				WR(lines[cnt].l, lines[cnt].len);
1979b50d902SRodney W. Grimes 	} else {
1989b50d902SRodney W. Grimes 		if (wrap)
1999b50d902SRodney W. Grimes 			for (cnt = recno; cnt < off; ++cnt)
2009b50d902SRodney W. Grimes 				WR(lines[cnt].l, lines[cnt].len);
2019b50d902SRodney W. Grimes 		for (cnt = 0; cnt < recno; ++cnt)
2029b50d902SRodney W. Grimes 			WR(lines[cnt].l, lines[cnt].len);
2039b50d902SRodney W. Grimes 	}
2046439f56eSAdam David 	return 0;
2059b50d902SRodney W. Grimes }
206