xref: /freebsd/usr.bin/tail/read.c (revision dea85013f1d16893c2cce8c63c2033372dbc2070)
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  * 4. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes  *    without specific prior written permission.
199b50d902SRodney W. Grimes  *
209b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes  * SUCH DAMAGE.
319b50d902SRodney W. Grimes  */
329b50d902SRodney W. Grimes 
33814e3a92SMark Murray #include <sys/cdefs.h>
34814e3a92SMark Murray 
35814e3a92SMark Murray __FBSDID("$FreeBSD$");
36814e3a92SMark Murray 
379b50d902SRodney W. Grimes #ifndef lint
38814e3a92SMark Murray static const char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
39247e7cb1SJeroen Ruigrok van der Werven #endif
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <sys/types.h>
429b50d902SRodney W. Grimes #include <sys/stat.h>
43814e3a92SMark Murray 
44814e3a92SMark Murray #include <err.h>
459b50d902SRodney W. Grimes #include <errno.h>
46814e3a92SMark Murray #include <fcntl.h>
479b50d902SRodney W. Grimes #include <stdio.h>
489b50d902SRodney W. Grimes #include <stdlib.h>
499b50d902SRodney W. Grimes #include <string.h>
50814e3a92SMark Murray #include <unistd.h>
51814e3a92SMark Murray 
529b50d902SRodney W. Grimes #include "extern.h"
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes /*
559b50d902SRodney W. Grimes  * bytes -- read bytes to an offset from the end and display.
569b50d902SRodney W. Grimes  *
579b50d902SRodney W. Grimes  * This is the function that reads to a byte offset from the end of the input,
589b50d902SRodney W. Grimes  * storing the data in a wrap-around buffer which is then displayed.  If the
599b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
609b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
619b50d902SRodney W. Grimes  * it is displayed from the character closest to the beginning of the input to
629b50d902SRodney W. Grimes  * the end.
639b50d902SRodney W. Grimes  */
6449a598abSAdam David int
6522da50cfSBrian Somers bytes(FILE *fp, const char *fn, off_t off)
669b50d902SRodney W. Grimes {
6748a1ef22SJeroen Ruigrok van der Werven 	int ch, len, tlen;
6848a1ef22SJeroen Ruigrok van der Werven 	char *ep, *p, *t;
699b50d902SRodney W. Grimes 	int wrap;
709b50d902SRodney W. Grimes 	char *sp;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes 	if ((sp = p = malloc(off)) == NULL)
73ac551270SPeter Wemm 		err(1, "malloc");
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
769b50d902SRodney W. Grimes 		*p = ch;
779b50d902SRodney W. Grimes 		if (++p == ep) {
789b50d902SRodney W. Grimes 			wrap = 1;
799b50d902SRodney W. Grimes 			p = sp;
809b50d902SRodney W. Grimes 		}
819b50d902SRodney W. Grimes 	}
829b50d902SRodney W. Grimes 	if (ferror(fp)) {
8322da50cfSBrian Somers 		ierr(fn);
847ce8354aSKonstantin Belousov 		free(sp);
8549a598abSAdam David 		return 1;
869b50d902SRodney W. Grimes 	}
879b50d902SRodney W. Grimes 
889b50d902SRodney W. Grimes 	if (rflag) {
899b50d902SRodney W. Grimes 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
909b50d902SRodney W. Grimes 			if (*t == '\n' && len) {
919b50d902SRodney W. Grimes 				WR(t + 1, len);
929b50d902SRodney W. Grimes 				len = 0;
939b50d902SRodney W. Grimes 		}
949b50d902SRodney W. Grimes 		if (wrap) {
959b50d902SRodney W. Grimes 			tlen = len;
969b50d902SRodney W. Grimes 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
979b50d902SRodney W. Grimes 				if (*t == '\n') {
989b50d902SRodney W. Grimes 					if (len) {
999b50d902SRodney W. Grimes 						WR(t + 1, len);
1009b50d902SRodney W. Grimes 						len = 0;
1019b50d902SRodney W. Grimes 					}
1029b50d902SRodney W. Grimes 					if (tlen) {
1039b50d902SRodney W. Grimes 						WR(sp, tlen);
1049b50d902SRodney W. Grimes 						tlen = 0;
1059b50d902SRodney W. Grimes 					}
1069b50d902SRodney W. Grimes 				}
1079b50d902SRodney W. Grimes 			if (len)
1089b50d902SRodney W. Grimes 				WR(t + 1, len);
1099b50d902SRodney W. Grimes 			if (tlen)
1109b50d902SRodney W. Grimes 				WR(sp, tlen);
1119b50d902SRodney W. Grimes 		}
1129b50d902SRodney W. Grimes 	} else {
1139b50d902SRodney W. Grimes 		if (wrap && (len = ep - p))
1149b50d902SRodney W. Grimes 			WR(p, len);
115814e3a92SMark Murray 		len = p - sp;
116814e3a92SMark Murray 		if (len)
1179b50d902SRodney W. Grimes 			WR(sp, len);
1189b50d902SRodney W. Grimes 	}
1197ce8354aSKonstantin Belousov 
1207ce8354aSKonstantin Belousov 	free(sp);
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
13522da50cfSBrian Somers lines(FILE *fp, const char *fn, off_t off)
1369b50d902SRodney W. Grimes {
1379b50d902SRodney W. Grimes 	struct {
13896b5910fSMark Murray 		int blen;
1399b50d902SRodney W. Grimes 		u_int len;
1409b50d902SRodney W. Grimes 		char *l;
141814e3a92SMark Murray 	} *llines;
1427ce8354aSKonstantin Belousov 	int ch, rc;
143814e3a92SMark Murray 	char *p, *sp;
14496b5910fSMark Murray 	int blen, cnt, recno, wrap;
1459b50d902SRodney W. Grimes 
146*dea85013SXin LI 	if ((llines = calloc(off, sizeof(*llines))) == NULL)
147*dea85013SXin LI 		err(1, "calloc");
148f8c2d2bdSPhilippe Charnier 	p = sp = NULL;
1499b50d902SRodney W. Grimes 	blen = cnt = recno = wrap = 0;
1507ce8354aSKonstantin Belousov 	rc = 0;
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF) {
1539b50d902SRodney W. Grimes 		if (++cnt > blen) {
1549b50d902SRodney W. Grimes 			if ((sp = realloc(sp, blen += 1024)) == NULL)
155ac551270SPeter Wemm 				err(1, "realloc");
1569b50d902SRodney W. Grimes 			p = sp + cnt - 1;
1579b50d902SRodney W. Grimes 		}
1589b50d902SRodney W. Grimes 		*p++ = ch;
1599b50d902SRodney W. Grimes 		if (ch == '\n') {
16096b5910fSMark Murray 			if ((int)llines[recno].blen < cnt) {
161814e3a92SMark Murray 				llines[recno].blen = cnt + 256;
162814e3a92SMark Murray 				if ((llines[recno].l = realloc(llines[recno].l,
163814e3a92SMark Murray 				    llines[recno].blen)) == NULL)
164ac551270SPeter Wemm 					err(1, "realloc");
1659b50d902SRodney W. Grimes 			}
166814e3a92SMark Murray 			bcopy(sp, llines[recno].l, llines[recno].len = cnt);
1679b50d902SRodney W. Grimes 			cnt = 0;
1689b50d902SRodney W. Grimes 			p = sp;
1699b50d902SRodney W. Grimes 			if (++recno == off) {
1709b50d902SRodney W. Grimes 				wrap = 1;
1719b50d902SRodney W. Grimes 				recno = 0;
1729b50d902SRodney W. Grimes 			}
1739b50d902SRodney W. Grimes 		}
1749b50d902SRodney W. Grimes 	}
1759b50d902SRodney W. Grimes 	if (ferror(fp)) {
17622da50cfSBrian Somers 		ierr(fn);
1777ce8354aSKonstantin Belousov 		rc = 1;
1787ce8354aSKonstantin Belousov 		goto done;
1799b50d902SRodney W. Grimes 	}
1809b50d902SRodney W. Grimes 	if (cnt) {
181814e3a92SMark Murray 		llines[recno].l = sp;
182e7356456STai-hwa Liang 		sp = NULL;
183814e3a92SMark Murray 		llines[recno].len = cnt;
1849b50d902SRodney W. Grimes 		if (++recno == off) {
1859b50d902SRodney W. Grimes 			wrap = 1;
1869b50d902SRodney W. Grimes 			recno = 0;
1879b50d902SRodney W. Grimes 		}
1889b50d902SRodney W. Grimes 	}
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes 	if (rflag) {
19196b5910fSMark Murray 		for (cnt = recno - 1; cnt >= 0; --cnt)
192814e3a92SMark Murray 			WR(llines[cnt].l, llines[cnt].len);
1939b50d902SRodney W. Grimes 		if (wrap)
19496b5910fSMark Murray 			for (cnt = off - 1; cnt >= recno; --cnt)
195814e3a92SMark Murray 				WR(llines[cnt].l, llines[cnt].len);
1969b50d902SRodney W. Grimes 	} else {
1979b50d902SRodney W. Grimes 		if (wrap)
19896b5910fSMark Murray 			for (cnt = recno; cnt < off; ++cnt)
199814e3a92SMark Murray 				WR(llines[cnt].l, llines[cnt].len);
20096b5910fSMark Murray 		for (cnt = 0; cnt < recno; ++cnt)
201814e3a92SMark Murray 			WR(llines[cnt].l, llines[cnt].len);
2029b50d902SRodney W. Grimes 	}
2037ce8354aSKonstantin Belousov done:
2047ce8354aSKonstantin Belousov 	for (cnt = 0; cnt < off; cnt++)
2057ce8354aSKonstantin Belousov 		free(llines[cnt].l);
2067ce8354aSKonstantin Belousov 	free(sp);
2077ce8354aSKonstantin Belousov 	free(llines);
2087ce8354aSKonstantin Belousov 	return (rc);
2099b50d902SRodney W. Grimes }
210