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 694bba8e59SPaul Richards bytes(FILE *fp, 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(); 887ce8354aSKonstantin Belousov free(sp); 8949a598abSAdam David return 1; 909b50d902SRodney W. Grimes } 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes if (rflag) { 939b50d902SRodney W. Grimes for (t = p - 1, len = 0; t >= sp; --t, ++len) 949b50d902SRodney W. Grimes if (*t == '\n' && len) { 959b50d902SRodney W. Grimes WR(t + 1, len); 969b50d902SRodney W. Grimes len = 0; 979b50d902SRodney W. Grimes } 989b50d902SRodney W. Grimes if (wrap) { 999b50d902SRodney W. Grimes tlen = len; 1009b50d902SRodney W. Grimes for (t = ep - 1, len = 0; t >= p; --t, ++len) 1019b50d902SRodney W. Grimes if (*t == '\n') { 1029b50d902SRodney W. Grimes if (len) { 1039b50d902SRodney W. Grimes WR(t + 1, len); 1049b50d902SRodney W. Grimes len = 0; 1059b50d902SRodney W. Grimes } 1069b50d902SRodney W. Grimes if (tlen) { 1079b50d902SRodney W. Grimes WR(sp, tlen); 1089b50d902SRodney W. Grimes tlen = 0; 1099b50d902SRodney W. Grimes } 1109b50d902SRodney W. Grimes } 1119b50d902SRodney W. Grimes if (len) 1129b50d902SRodney W. Grimes WR(t + 1, len); 1139b50d902SRodney W. Grimes if (tlen) 1149b50d902SRodney W. Grimes WR(sp, tlen); 1159b50d902SRodney W. Grimes } 1169b50d902SRodney W. Grimes } else { 1179b50d902SRodney W. Grimes if (wrap && (len = ep - p)) 1189b50d902SRodney W. Grimes WR(p, len); 119814e3a92SMark Murray len = p - sp; 120814e3a92SMark Murray if (len) 1219b50d902SRodney W. Grimes WR(sp, len); 1229b50d902SRodney W. Grimes } 1237ce8354aSKonstantin Belousov 1247ce8354aSKonstantin Belousov free(sp); 1256439f56eSAdam David return 0; 1269b50d902SRodney W. Grimes } 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes /* 1299b50d902SRodney W. Grimes * lines -- read lines to an offset from the end and display. 1309b50d902SRodney W. Grimes * 1319b50d902SRodney W. Grimes * This is the function that reads to a line offset from the end of the input, 1329b50d902SRodney W. Grimes * storing the data in an array of buffers which is then displayed. If the 1339b50d902SRodney W. Grimes * rflag is set, the data is displayed in lines in reverse order, and this 1349b50d902SRodney W. Grimes * routine has the usual nastiness of trying to find the newlines. Otherwise, 1359b50d902SRodney W. Grimes * it is displayed from the line closest to the beginning of the input to 1369b50d902SRodney W. Grimes * the end. 1379b50d902SRodney W. Grimes */ 13849a598abSAdam David int 1394bba8e59SPaul Richards lines(FILE *fp, off_t off) 1409b50d902SRodney W. Grimes { 1419b50d902SRodney W. Grimes struct { 14296b5910fSMark Murray int blen; 1439b50d902SRodney W. Grimes u_int len; 1449b50d902SRodney W. Grimes char *l; 145814e3a92SMark Murray } *llines; 1467ce8354aSKonstantin Belousov int ch, rc; 147814e3a92SMark Murray char *p, *sp; 14896b5910fSMark Murray int blen, cnt, recno, wrap; 1499b50d902SRodney W. Grimes 150814e3a92SMark Murray if ((llines = malloc(off * sizeof(*llines))) == NULL) 151ac551270SPeter Wemm err(1, "malloc"); 152814e3a92SMark Murray bzero(llines, off * sizeof(*llines)); 1539b50d902SRodney W. Grimes sp = NULL; 1549b50d902SRodney W. Grimes blen = cnt = recno = wrap = 0; 1557ce8354aSKonstantin Belousov rc = 0; 1569b50d902SRodney W. Grimes 1579b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF) { 1589b50d902SRodney W. Grimes if (++cnt > blen) { 1599b50d902SRodney W. Grimes if ((sp = realloc(sp, blen += 1024)) == NULL) 160ac551270SPeter Wemm err(1, "realloc"); 1619b50d902SRodney W. Grimes p = sp + cnt - 1; 1629b50d902SRodney W. Grimes } 1639b50d902SRodney W. Grimes *p++ = ch; 1649b50d902SRodney W. Grimes if (ch == '\n') { 16596b5910fSMark Murray if ((int)llines[recno].blen < cnt) { 166814e3a92SMark Murray llines[recno].blen = cnt + 256; 167814e3a92SMark Murray if ((llines[recno].l = realloc(llines[recno].l, 168814e3a92SMark Murray llines[recno].blen)) == NULL) 169ac551270SPeter Wemm err(1, "realloc"); 1709b50d902SRodney W. Grimes } 171814e3a92SMark Murray bcopy(sp, llines[recno].l, llines[recno].len = cnt); 1729b50d902SRodney W. Grimes cnt = 0; 1739b50d902SRodney W. Grimes p = sp; 1749b50d902SRodney W. Grimes if (++recno == off) { 1759b50d902SRodney W. Grimes wrap = 1; 1769b50d902SRodney W. Grimes recno = 0; 1779b50d902SRodney W. Grimes } 1789b50d902SRodney W. Grimes } 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes if (ferror(fp)) { 1819b50d902SRodney W. Grimes ierr(); 1827ce8354aSKonstantin Belousov rc = 1; 1837ce8354aSKonstantin Belousov goto done; 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) { 19596b5910fSMark Murray for (cnt = recno - 1; cnt >= 0; --cnt) 196814e3a92SMark Murray WR(llines[cnt].l, llines[cnt].len); 1979b50d902SRodney W. Grimes if (wrap) 19896b5910fSMark Murray for (cnt = off - 1; cnt >= recno; --cnt) 199814e3a92SMark Murray WR(llines[cnt].l, llines[cnt].len); 2009b50d902SRodney W. Grimes } else { 2019b50d902SRodney W. Grimes if (wrap) 20296b5910fSMark Murray for (cnt = recno; cnt < off; ++cnt) 203814e3a92SMark Murray WR(llines[cnt].l, llines[cnt].len); 20496b5910fSMark Murray for (cnt = 0; cnt < recno; ++cnt) 205814e3a92SMark Murray WR(llines[cnt].l, llines[cnt].len); 2069b50d902SRodney W. Grimes } 2077ce8354aSKonstantin Belousov done: 2087ce8354aSKonstantin Belousov for (cnt = 0; cnt < off; cnt++) 2097ce8354aSKonstantin Belousov free(llines[cnt].l); 2107ce8354aSKonstantin Belousov free(sp); 2117ce8354aSKonstantin Belousov free(llines); 2127ce8354aSKonstantin Belousov return (rc); 2139b50d902SRodney W. Grimes } 214