xref: /freebsd/usr.bin/tail/reverse.c (revision 9b50d9027575220cb6dd09b3e62f03f511e908b8)
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
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)reverse.c	8.1 (Berkeley) 6/6/93";
399b50d902SRodney W. Grimes #endif /* not lint */
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <sys/param.h>
429b50d902SRodney W. Grimes #include <sys/stat.h>
439b50d902SRodney W. Grimes #include <sys/mman.h>
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <limits.h>
469b50d902SRodney W. Grimes #include <errno.h>
479b50d902SRodney W. Grimes #include <unistd.h>
489b50d902SRodney W. Grimes #include <stdio.h>
499b50d902SRodney W. Grimes #include <stdlib.h>
509b50d902SRodney W. Grimes #include <string.h>
519b50d902SRodney W. Grimes #include "extern.h"
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes static void r_buf __P((FILE *));
549b50d902SRodney W. Grimes static void r_reg __P((FILE *, enum STYLE, long, struct stat *));
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes /*
579b50d902SRodney W. Grimes  * reverse -- display input in reverse order by line.
589b50d902SRodney W. Grimes  *
599b50d902SRodney W. Grimes  * There are six separate cases for this -- regular and non-regular
609b50d902SRodney W. Grimes  * files by bytes, lines or the whole file.
619b50d902SRodney W. Grimes  *
629b50d902SRodney W. Grimes  * BYTES	display N bytes
639b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
649b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
659b50d902SRodney W. Grimes  *
669b50d902SRodney W. Grimes  * LINES	display N lines
679b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
689b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
699b50d902SRodney W. Grimes  *
709b50d902SRodney W. Grimes  * FILE		display the entire file
719b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
729b50d902SRodney W. Grimes  *	NOREG	cyclically read input into a linked list of buffers
739b50d902SRodney W. Grimes  */
749b50d902SRodney W. Grimes void
759b50d902SRodney W. Grimes reverse(fp, style, off, sbp)
769b50d902SRodney W. Grimes 	FILE *fp;
779b50d902SRodney W. Grimes 	enum STYLE style;
789b50d902SRodney W. Grimes 	long off;
799b50d902SRodney W. Grimes 	struct stat *sbp;
809b50d902SRodney W. Grimes {
819b50d902SRodney W. Grimes 	if (style != REVERSE && off == 0)
829b50d902SRodney W. Grimes 		return;
839b50d902SRodney W. Grimes 
849b50d902SRodney W. Grimes 	if (S_ISREG(sbp->st_mode))
859b50d902SRodney W. Grimes 		r_reg(fp, style, off, sbp);
869b50d902SRodney W. Grimes 	else
879b50d902SRodney W. Grimes 		switch(style) {
889b50d902SRodney W. Grimes 		case FBYTES:
899b50d902SRodney W. Grimes 		case RBYTES:
909b50d902SRodney W. Grimes 			bytes(fp, off);
919b50d902SRodney W. Grimes 			break;
929b50d902SRodney W. Grimes 		case FLINES:
939b50d902SRodney W. Grimes 		case RLINES:
949b50d902SRodney W. Grimes 			lines(fp, off);
959b50d902SRodney W. Grimes 			break;
969b50d902SRodney W. Grimes 		case REVERSE:
979b50d902SRodney W. Grimes 			r_buf(fp);
989b50d902SRodney W. Grimes 			break;
999b50d902SRodney W. Grimes 		}
1009b50d902SRodney W. Grimes }
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes /*
1039b50d902SRodney W. Grimes  * r_reg -- display a regular file in reverse order by line.
1049b50d902SRodney W. Grimes  */
1059b50d902SRodney W. Grimes static void
1069b50d902SRodney W. Grimes r_reg(fp, style, off, sbp)
1079b50d902SRodney W. Grimes 	FILE *fp;
1089b50d902SRodney W. Grimes 	register enum STYLE style;
1099b50d902SRodney W. Grimes 	long off;
1109b50d902SRodney W. Grimes 	struct stat *sbp;
1119b50d902SRodney W. Grimes {
1129b50d902SRodney W. Grimes 	register off_t size;
1139b50d902SRodney W. Grimes 	register int llen;
1149b50d902SRodney W. Grimes 	register char *p;
1159b50d902SRodney W. Grimes 	char *start;
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
1189b50d902SRodney W. Grimes 		return;
1199b50d902SRodney W. Grimes 
1209b50d902SRodney W. Grimes 	if (size > SIZE_T_MAX) {
1219b50d902SRodney W. Grimes 		err(0, "%s: %s", fname, strerror(EFBIG));
1229b50d902SRodney W. Grimes 		return;
1239b50d902SRodney W. Grimes 	}
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes 	if ((start = mmap(NULL, (size_t)size,
1269b50d902SRodney W. Grimes 	    PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
1279b50d902SRodney W. Grimes 		err(0, "%s: %s", fname, strerror(EFBIG));
1289b50d902SRodney W. Grimes 		return;
1299b50d902SRodney W. Grimes 	}
1309b50d902SRodney W. Grimes 	p = start + size - 1;
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes 	if (style == RBYTES && off < size)
1339b50d902SRodney W. Grimes 		size = off;
1349b50d902SRodney W. Grimes 
1359b50d902SRodney W. Grimes 	/* Last char is special, ignore whether newline or not. */
1369b50d902SRodney W. Grimes 	for (llen = 1; --size; ++llen)
1379b50d902SRodney W. Grimes 		if (*--p == '\n') {
1389b50d902SRodney W. Grimes 			WR(p + 1, llen);
1399b50d902SRodney W. Grimes 			llen = 0;
1409b50d902SRodney W. Grimes 			if (style == RLINES && !--off) {
1419b50d902SRodney W. Grimes 				++p;
1429b50d902SRodney W. Grimes 				break;
1439b50d902SRodney W. Grimes 			}
1449b50d902SRodney W. Grimes 		}
1459b50d902SRodney W. Grimes 	if (llen)
1469b50d902SRodney W. Grimes 		WR(p, llen);
1479b50d902SRodney W. Grimes 	if (munmap(start, (size_t)sbp->st_size))
1489b50d902SRodney W. Grimes 		err(0, "%s: %s", fname, strerror(errno));
1499b50d902SRodney W. Grimes }
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes typedef struct bf {
1529b50d902SRodney W. Grimes 	struct bf *next;
1539b50d902SRodney W. Grimes 	struct bf *prev;
1549b50d902SRodney W. Grimes 	int len;
1559b50d902SRodney W. Grimes 	char *l;
1569b50d902SRodney W. Grimes } BF;
1579b50d902SRodney W. Grimes 
1589b50d902SRodney W. Grimes /*
1599b50d902SRodney W. Grimes  * r_buf -- display a non-regular file in reverse order by line.
1609b50d902SRodney W. Grimes  *
1619b50d902SRodney W. Grimes  * This is the function that saves the entire input, storing the data in a
1629b50d902SRodney W. Grimes  * doubly linked list of buffers and then displays them in reverse order.
1639b50d902SRodney W. Grimes  * It has the usual nastiness of trying to find the newlines, as there's no
1649b50d902SRodney W. Grimes  * guarantee that a newline occurs anywhere in the file, let alone in any
1659b50d902SRodney W. Grimes  * particular buffer.  If we run out of memory, input is discarded (and the
1669b50d902SRodney W. Grimes  * user warned).
1679b50d902SRodney W. Grimes  */
1689b50d902SRodney W. Grimes static void
1699b50d902SRodney W. Grimes r_buf(fp)
1709b50d902SRodney W. Grimes 	FILE *fp;
1719b50d902SRodney W. Grimes {
1729b50d902SRodney W. Grimes 	register BF *mark, *tl, *tr;
1739b50d902SRodney W. Grimes 	register int ch, len, llen;
1749b50d902SRodney W. Grimes 	register char *p;
1759b50d902SRodney W. Grimes 	off_t enomem;
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes #define	BSZ	(128 * 1024)
1789b50d902SRodney W. Grimes 	for (mark = NULL, enomem = 0;;) {
1799b50d902SRodney W. Grimes 		/*
1809b50d902SRodney W. Grimes 		 * Allocate a new block and link it into place in a doubly
1819b50d902SRodney W. Grimes 		 * linked list.  If out of memory, toss the LRU block and
1829b50d902SRodney W. Grimes 		 * keep going.
1839b50d902SRodney W. Grimes 		 */
1849b50d902SRodney W. Grimes 		if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
1859b50d902SRodney W. Grimes 		    (tl->l = malloc(BSZ)) == NULL) {
1869b50d902SRodney W. Grimes 			if (!mark)
1879b50d902SRodney W. Grimes 				err(1, "%s", strerror(errno));
1889b50d902SRodney W. Grimes 			tl = enomem ? tl->next : mark;
1899b50d902SRodney W. Grimes 			enomem += tl->len;
1909b50d902SRodney W. Grimes 		} else if (mark) {
1919b50d902SRodney W. Grimes 			tl->next = mark;
1929b50d902SRodney W. Grimes 			tl->prev = mark->prev;
1939b50d902SRodney W. Grimes 			mark->prev->next = tl;
1949b50d902SRodney W. Grimes 			mark->prev = tl;
1959b50d902SRodney W. Grimes 		} else
1969b50d902SRodney W. Grimes 			mark->next = mark->prev = (mark = tl);
1979b50d902SRodney W. Grimes 
1989b50d902SRodney W. Grimes 		/* Fill the block with input data. */
1999b50d902SRodney W. Grimes 		for (p = tl->l, len = 0;
2009b50d902SRodney W. Grimes 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
2019b50d902SRodney W. Grimes 			*p++ = ch;
2029b50d902SRodney W. Grimes 
2039b50d902SRodney W. Grimes 		/*
2049b50d902SRodney W. Grimes 		 * If no input data for this block and we tossed some data,
2059b50d902SRodney W. Grimes 		 * recover it.
2069b50d902SRodney W. Grimes 		 */
2079b50d902SRodney W. Grimes 		if (!len) {
2089b50d902SRodney W. Grimes 			if (enomem)
2099b50d902SRodney W. Grimes 				enomem -= tl->len;
2109b50d902SRodney W. Grimes 			tl = tl->prev;
2119b50d902SRodney W. Grimes 			break;
2129b50d902SRodney W. Grimes 		}
2139b50d902SRodney W. Grimes 
2149b50d902SRodney W. Grimes 		tl->len = len;
2159b50d902SRodney W. Grimes 		if (ch == EOF)
2169b50d902SRodney W. Grimes 			break;
2179b50d902SRodney W. Grimes 	}
2189b50d902SRodney W. Grimes 
2199b50d902SRodney W. Grimes 	if (enomem) {
2209b50d902SRodney W. Grimes 		(void)fprintf(stderr,
2219b50d902SRodney W. Grimes 		    "tail: warning: %ld bytes discarded\n", enomem);
2229b50d902SRodney W. Grimes 		rval = 1;
2239b50d902SRodney W. Grimes 	}
2249b50d902SRodney W. Grimes 
2259b50d902SRodney W. Grimes 	/*
2269b50d902SRodney W. Grimes 	 * Step through the blocks in the reverse order read.  The last char
2279b50d902SRodney W. Grimes 	 * is special, ignore whether newline or not.
2289b50d902SRodney W. Grimes 	 */
2299b50d902SRodney W. Grimes 	for (mark = tl;;) {
2309b50d902SRodney W. Grimes 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
2319b50d902SRodney W. Grimes 		    --p, ++llen)
2329b50d902SRodney W. Grimes 			if (*p == '\n') {
2339b50d902SRodney W. Grimes 				if (llen) {
2349b50d902SRodney W. Grimes 					WR(p + 1, llen);
2359b50d902SRodney W. Grimes 					llen = 0;
2369b50d902SRodney W. Grimes 				}
2379b50d902SRodney W. Grimes 				if (tl == mark)
2389b50d902SRodney W. Grimes 					continue;
2399b50d902SRodney W. Grimes 				for (tr = tl->next; tr->len; tr = tr->next) {
2409b50d902SRodney W. Grimes 					WR(tr->l, tr->len);
2419b50d902SRodney W. Grimes 					tr->len = 0;
2429b50d902SRodney W. Grimes 					if (tr == mark)
2439b50d902SRodney W. Grimes 						break;
2449b50d902SRodney W. Grimes 				}
2459b50d902SRodney W. Grimes 			}
2469b50d902SRodney W. Grimes 		tl->len = llen;
2479b50d902SRodney W. Grimes 		if ((tl = tl->prev) == mark)
2489b50d902SRodney W. Grimes 			break;
2499b50d902SRodney W. Grimes 	}
2509b50d902SRodney W. Grimes 	tl = tl->next;
2519b50d902SRodney W. Grimes 	if (tl->len) {
2529b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2539b50d902SRodney W. Grimes 		tl->len = 0;
2549b50d902SRodney W. Grimes 	}
2559b50d902SRodney W. Grimes 	while ((tl = tl->next)->len) {
2569b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2579b50d902SRodney W. Grimes 		tl->len = 0;
2589b50d902SRodney W. Grimes 	}
2599b50d902SRodney W. Grimes }
260