xref: /freebsd/usr.bin/tail/read.c (revision b70e57be2cfe83ec9f410e2f317ea38aaac61a98)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1991, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Edward Sze-Tyan Wang.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
35814e3a92SMark Murray 
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #include <sys/types.h>
389b50d902SRodney W. Grimes #include <sys/stat.h>
39814e3a92SMark Murray 
40814e3a92SMark Murray #include <err.h>
419b50d902SRodney W. Grimes #include <errno.h>
42814e3a92SMark Murray #include <fcntl.h>
439b50d902SRodney W. Grimes #include <stdio.h>
449b50d902SRodney W. Grimes #include <stdlib.h>
459b50d902SRodney W. Grimes #include <string.h>
46814e3a92SMark Murray #include <unistd.h>
47814e3a92SMark Murray 
48c851fce6SMariusz Zaborski #include <libcasper.h>
49c851fce6SMariusz Zaborski #include <casper/cap_fileargs.h>
50c851fce6SMariusz Zaborski 
519b50d902SRodney W. Grimes #include "extern.h"
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes /*
549b50d902SRodney W. Grimes  * bytes -- read bytes to an offset from the end and display.
559b50d902SRodney W. Grimes  *
569b50d902SRodney W. Grimes  * This is the function that reads to a byte offset from the end of the input,
579b50d902SRodney W. Grimes  * storing the data in a wrap-around buffer which is then displayed.  If the
589b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
599b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
609b50d902SRodney W. Grimes  * it is displayed from the character closest to the beginning of the input to
619b50d902SRodney W. Grimes  * the end.
629b50d902SRodney W. Grimes  */
6349a598abSAdam David int
bytes(FILE * fp,const char * fn,off_t off)6422da50cfSBrian Somers bytes(FILE *fp, const char *fn, off_t off)
659b50d902SRodney W. Grimes {
6648a1ef22SJeroen Ruigrok van der Werven 	int ch, len, tlen;
6748a1ef22SJeroen Ruigrok van der Werven 	char *ep, *p, *t;
689b50d902SRodney W. Grimes 	int wrap;
699b50d902SRodney W. Grimes 	char *sp;
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes 	if ((sp = p = malloc(off)) == NULL)
72*b70e57beSDag-Erling Smørgrav 		err(1, "failed to allocate memory");
739b50d902SRodney W. Grimes 
749b50d902SRodney W. Grimes 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
759b50d902SRodney W. Grimes 		*p = ch;
769b50d902SRodney W. Grimes 		if (++p == ep) {
779b50d902SRodney W. Grimes 			wrap = 1;
789b50d902SRodney W. Grimes 			p = sp;
799b50d902SRodney W. Grimes 		}
809b50d902SRodney W. Grimes 	}
819b50d902SRodney W. Grimes 	if (ferror(fp)) {
8222da50cfSBrian Somers 		ierr(fn);
837ce8354aSKonstantin Belousov 		free(sp);
8449a598abSAdam David 		return 1;
859b50d902SRodney W. Grimes 	}
869b50d902SRodney W. Grimes 
879b50d902SRodney W. Grimes 	if (rflag) {
889b50d902SRodney W. Grimes 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
899b50d902SRodney W. Grimes 			if (*t == '\n' && len) {
909b50d902SRodney W. Grimes 				WR(t + 1, len);
919b50d902SRodney W. Grimes 				len = 0;
929b50d902SRodney W. Grimes 		}
939b50d902SRodney W. Grimes 		if (wrap) {
949b50d902SRodney W. Grimes 			tlen = len;
959b50d902SRodney W. Grimes 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
969b50d902SRodney W. Grimes 				if (*t == '\n') {
979b50d902SRodney W. Grimes 					if (len) {
989b50d902SRodney W. Grimes 						WR(t + 1, len);
999b50d902SRodney W. Grimes 						len = 0;
1009b50d902SRodney W. Grimes 					}
1019b50d902SRodney W. Grimes 					if (tlen) {
1029b50d902SRodney W. Grimes 						WR(sp, tlen);
1039b50d902SRodney W. Grimes 						tlen = 0;
1049b50d902SRodney W. Grimes 					}
1059b50d902SRodney W. Grimes 				}
1069b50d902SRodney W. Grimes 			if (len)
1079b50d902SRodney W. Grimes 				WR(t + 1, len);
1089b50d902SRodney W. Grimes 			if (tlen)
1099b50d902SRodney W. Grimes 				WR(sp, tlen);
1109b50d902SRodney W. Grimes 		}
1119b50d902SRodney W. Grimes 	} else {
1129b50d902SRodney W. Grimes 		if (wrap && (len = ep - p))
1139b50d902SRodney W. Grimes 			WR(p, len);
114814e3a92SMark Murray 		len = p - sp;
115814e3a92SMark Murray 		if (len)
1169b50d902SRodney W. Grimes 			WR(sp, len);
1179b50d902SRodney W. Grimes 	}
1187ce8354aSKonstantin Belousov 
1197ce8354aSKonstantin Belousov 	free(sp);
1206439f56eSAdam David 	return 0;
1219b50d902SRodney W. Grimes }
1229b50d902SRodney W. Grimes 
1239b50d902SRodney W. Grimes /*
1249b50d902SRodney W. Grimes  * lines -- read lines to an offset from the end and display.
1259b50d902SRodney W. Grimes  *
1269b50d902SRodney W. Grimes  * This is the function that reads to a line offset from the end of the input,
1279b50d902SRodney W. Grimes  * storing the data in an array of buffers which is then displayed.  If the
1289b50d902SRodney W. Grimes  * rflag is set, the data is displayed in lines in reverse order, and this
1299b50d902SRodney W. Grimes  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
1309b50d902SRodney W. Grimes  * it is displayed from the line closest to the beginning of the input to
1319b50d902SRodney W. Grimes  * the end.
1329b50d902SRodney W. Grimes  */
13349a598abSAdam David int
lines(FILE * fp,const char * fn,off_t off)13422da50cfSBrian Somers lines(FILE *fp, const char *fn, off_t off)
1359b50d902SRodney W. Grimes {
1369b50d902SRodney W. Grimes 	struct {
13796b5910fSMark Murray 		int blen;
1389b50d902SRodney W. Grimes 		u_int len;
1399b50d902SRodney W. Grimes 		char *l;
140814e3a92SMark Murray 	} *llines;
1417ce8354aSKonstantin Belousov 	int ch, rc;
142814e3a92SMark Murray 	char *p, *sp;
14396b5910fSMark Murray 	int blen, cnt, recno, wrap;
1449b50d902SRodney W. Grimes 
145dea85013SXin LI 	if ((llines = calloc(off, sizeof(*llines))) == NULL)
146*b70e57beSDag-Erling Smørgrav 		err(1, "failed to allocate memory");
147f8c2d2bdSPhilippe Charnier 	p = sp = NULL;
1489b50d902SRodney W. Grimes 	blen = cnt = recno = wrap = 0;
1497ce8354aSKonstantin Belousov 	rc = 0;
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF) {
1529b50d902SRodney W. Grimes 		if (++cnt > blen) {
1539b50d902SRodney W. Grimes 			if ((sp = realloc(sp, blen += 1024)) == NULL)
154*b70e57beSDag-Erling Smørgrav 				err(1, "failed to allocate memory");
1559b50d902SRodney W. Grimes 			p = sp + cnt - 1;
1569b50d902SRodney W. Grimes 		}
1579b50d902SRodney W. Grimes 		*p++ = ch;
1589b50d902SRodney W. Grimes 		if (ch == '\n') {
15996b5910fSMark Murray 			if ((int)llines[recno].blen < cnt) {
160814e3a92SMark Murray 				llines[recno].blen = cnt + 256;
161814e3a92SMark Murray 				if ((llines[recno].l = realloc(llines[recno].l,
162814e3a92SMark Murray 				    llines[recno].blen)) == NULL)
163*b70e57beSDag-Erling Smørgrav 					err(1, "failed to allocate memory");
1649b50d902SRodney W. Grimes 			}
165814e3a92SMark Murray 			bcopy(sp, llines[recno].l, llines[recno].len = cnt);
1669b50d902SRodney W. Grimes 			cnt = 0;
1679b50d902SRodney W. Grimes 			p = sp;
1689b50d902SRodney W. Grimes 			if (++recno == off) {
1699b50d902SRodney W. Grimes 				wrap = 1;
1709b50d902SRodney W. Grimes 				recno = 0;
1719b50d902SRodney W. Grimes 			}
1729b50d902SRodney W. Grimes 		}
1739b50d902SRodney W. Grimes 	}
1749b50d902SRodney W. Grimes 	if (ferror(fp)) {
17522da50cfSBrian Somers 		ierr(fn);
1767ce8354aSKonstantin Belousov 		rc = 1;
1777ce8354aSKonstantin Belousov 		goto done;
1789b50d902SRodney W. Grimes 	}
1799b50d902SRodney W. Grimes 	if (cnt) {
180814e3a92SMark Murray 		llines[recno].l = sp;
181e7356456STai-hwa Liang 		sp = NULL;
182814e3a92SMark Murray 		llines[recno].len = cnt;
1839b50d902SRodney W. Grimes 		if (++recno == off) {
1849b50d902SRodney W. Grimes 			wrap = 1;
1859b50d902SRodney W. Grimes 			recno = 0;
1869b50d902SRodney W. Grimes 		}
1879b50d902SRodney W. Grimes 	}
1889b50d902SRodney W. Grimes 
1899b50d902SRodney W. Grimes 	if (rflag) {
19096b5910fSMark Murray 		for (cnt = recno - 1; cnt >= 0; --cnt)
191814e3a92SMark Murray 			WR(llines[cnt].l, llines[cnt].len);
1929b50d902SRodney W. Grimes 		if (wrap)
19396b5910fSMark Murray 			for (cnt = off - 1; cnt >= recno; --cnt)
194814e3a92SMark Murray 				WR(llines[cnt].l, llines[cnt].len);
1959b50d902SRodney W. Grimes 	} else {
1969b50d902SRodney W. Grimes 		if (wrap)
19796b5910fSMark Murray 			for (cnt = recno; cnt < off; ++cnt)
198814e3a92SMark Murray 				WR(llines[cnt].l, llines[cnt].len);
19996b5910fSMark Murray 		for (cnt = 0; cnt < recno; ++cnt)
200814e3a92SMark Murray 			WR(llines[cnt].l, llines[cnt].len);
2019b50d902SRodney W. Grimes 	}
2027ce8354aSKonstantin Belousov done:
2037ce8354aSKonstantin Belousov 	for (cnt = 0; cnt < off; cnt++)
2047ce8354aSKonstantin Belousov 		free(llines[cnt].l);
2057ce8354aSKonstantin Belousov 	free(sp);
2067ce8354aSKonstantin Belousov 	free(llines);
2077ce8354aSKonstantin Belousov 	return (rc);
2089b50d902SRodney W. Grimes }
209