xref: /freebsd/usr.bin/tail/reverse.c (revision b77b9b9a90448a1ebadfe284105b3a918ff46a12)
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[] = "@(#)reverse.c	8.1 (Berkeley) 6/6/93";
43ea7cc495SPhilippe Charnier #endif
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/param.h>
469b50d902SRodney W. Grimes #include <sys/stat.h>
479b50d902SRodney W. Grimes #include <sys/mman.h>
489b50d902SRodney W. Grimes 
49814e3a92SMark Murray #include <err.h>
509b50d902SRodney W. Grimes #include <errno.h>
51814e3a92SMark Murray #include <limits.h>
529b50d902SRodney W. Grimes #include <stdio.h>
539b50d902SRodney W. Grimes #include <stdlib.h>
549b50d902SRodney W. Grimes #include <string.h>
55814e3a92SMark Murray #include <unistd.h>
56814e3a92SMark Murray 
579b50d902SRodney W. Grimes #include "extern.h"
589b50d902SRodney W. Grimes 
593f330d7dSWarner Losh static void r_buf(FILE *);
603f330d7dSWarner Losh static void r_reg(FILE *, enum STYLE, off_t, struct stat *);
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes /*
639b50d902SRodney W. Grimes  * reverse -- display input in reverse order by line.
649b50d902SRodney W. Grimes  *
659b50d902SRodney W. Grimes  * There are six separate cases for this -- regular and non-regular
669b50d902SRodney W. Grimes  * files by bytes, lines or the whole file.
679b50d902SRodney W. Grimes  *
689b50d902SRodney W. Grimes  * BYTES	display N bytes
699b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
709b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
719b50d902SRodney W. Grimes  *
729b50d902SRodney W. Grimes  * LINES	display N lines
739b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
749b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
759b50d902SRodney W. Grimes  *
769b50d902SRodney W. Grimes  * FILE		display the entire file
779b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
789b50d902SRodney W. Grimes  *	NOREG	cyclically read input into a linked list of buffers
799b50d902SRodney W. Grimes  */
809b50d902SRodney W. Grimes void
819b50d902SRodney W. Grimes reverse(fp, style, off, sbp)
829b50d902SRodney W. Grimes 	FILE *fp;
839b50d902SRodney W. Grimes 	enum STYLE style;
84bd9dc975SAndrey A. Chernov 	off_t off;
859b50d902SRodney W. Grimes 	struct stat *sbp;
869b50d902SRodney W. Grimes {
879b50d902SRodney W. Grimes 	if (style != REVERSE && off == 0)
889b50d902SRodney W. Grimes 		return;
899b50d902SRodney W. Grimes 
909b50d902SRodney W. Grimes 	if (S_ISREG(sbp->st_mode))
919b50d902SRodney W. Grimes 		r_reg(fp, style, off, sbp);
929b50d902SRodney W. Grimes 	else
939b50d902SRodney W. Grimes 		switch(style) {
949b50d902SRodney W. Grimes 		case FBYTES:
959b50d902SRodney W. Grimes 		case RBYTES:
969b50d902SRodney W. Grimes 			bytes(fp, off);
979b50d902SRodney W. Grimes 			break;
989b50d902SRodney W. Grimes 		case FLINES:
999b50d902SRodney W. Grimes 		case RLINES:
1009b50d902SRodney W. Grimes 			lines(fp, off);
1019b50d902SRodney W. Grimes 			break;
1029b50d902SRodney W. Grimes 		case REVERSE:
1039b50d902SRodney W. Grimes 			r_buf(fp);
1049b50d902SRodney W. Grimes 			break;
105814e3a92SMark Murray 		default:
106b77b9b9aSMurray Stokely 			break;
1079b50d902SRodney W. Grimes 		}
1089b50d902SRodney W. Grimes }
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes /*
1119b50d902SRodney W. Grimes  * r_reg -- display a regular file in reverse order by line.
1129b50d902SRodney W. Grimes  */
1139b50d902SRodney W. Grimes static void
1149b50d902SRodney W. Grimes r_reg(fp, style, off, sbp)
1159b50d902SRodney W. Grimes 	FILE *fp;
11648a1ef22SJeroen Ruigrok van der Werven 	enum STYLE style;
117bd9dc975SAndrey A. Chernov 	off_t off;
1189b50d902SRodney W. Grimes 	struct stat *sbp;
1199b50d902SRodney W. Grimes {
120726098d3SDavid Malone 	struct mapinfo map;
121726098d3SDavid Malone 	off_t curoff, size, lineend;
122726098d3SDavid Malone 	int i;
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
1259b50d902SRodney W. Grimes 		return;
1269b50d902SRodney W. Grimes 
127726098d3SDavid Malone 	map.start = NULL;
128726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
129726098d3SDavid Malone 	map.fd = fileno(fp);
130726098d3SDavid Malone 
131726098d3SDavid Malone 	/*
132726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
133726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
134726098d3SDavid Malone 	 */
135726098d3SDavid Malone 	curoff = size - 2;
136726098d3SDavid Malone 	lineend = size;
137726098d3SDavid Malone 	while (curoff >= 0) {
138726098d3SDavid Malone 		if (curoff < map.mapoff || curoff >= map.mapoff + map.maplen) {
139726098d3SDavid Malone 			if (maparound(&map, curoff) != 0) {
14044cf272fSAdam David 				ierr();
1419b50d902SRodney W. Grimes 				return;
1429b50d902SRodney W. Grimes 			}
143726098d3SDavid Malone 		}
144726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--) {
145726098d3SDavid Malone 			if (style == RBYTES && --off == 0)
146726098d3SDavid Malone 				break;
147726098d3SDavid Malone 			if (map.start[i] == '\n')
148726098d3SDavid Malone 				break;
149726098d3SDavid Malone 		}
150726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
151726098d3SDavid Malone 		curoff = map.mapoff + i;
152726098d3SDavid Malone 		if (i < 0)
153726098d3SDavid Malone 			continue;
1549b50d902SRodney W. Grimes 
155726098d3SDavid Malone 		/* Print the line and update offsets. */
156726098d3SDavid Malone 		if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
15744cf272fSAdam David 			ierr();
1589b50d902SRodney W. Grimes 			return;
1599b50d902SRodney W. Grimes 		}
160726098d3SDavid Malone 		lineend = curoff + 1;
161726098d3SDavid Malone 		curoff--;
1629b50d902SRodney W. Grimes 
163726098d3SDavid Malone 		if (style == RLINES)
164726098d3SDavid Malone 			off--;
1659b50d902SRodney W. Grimes 
166726098d3SDavid Malone 		if (off == 0 && style != REVERSE) {
167726098d3SDavid Malone 			/* Avoid printing anything below. */
168726098d3SDavid Malone 			curoff = 0;
1699b50d902SRodney W. Grimes 			break;
1709b50d902SRodney W. Grimes 		}
1719b50d902SRodney W. Grimes 	}
172726098d3SDavid Malone 	if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
173726098d3SDavid Malone 		ierr();
174726098d3SDavid Malone 		return;
175726098d3SDavid Malone 	}
176726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen))
17744cf272fSAdam David 		ierr();
1789b50d902SRodney W. Grimes }
1799b50d902SRodney W. Grimes 
1809b50d902SRodney W. Grimes typedef struct bf {
1819b50d902SRodney W. Grimes 	struct bf *next;
1829b50d902SRodney W. Grimes 	struct bf *prev;
1839b50d902SRodney W. Grimes 	int len;
1849b50d902SRodney W. Grimes 	char *l;
1859b50d902SRodney W. Grimes } BF;
1869b50d902SRodney W. Grimes 
1879b50d902SRodney W. Grimes /*
1889b50d902SRodney W. Grimes  * r_buf -- display a non-regular file in reverse order by line.
1899b50d902SRodney W. Grimes  *
1909b50d902SRodney W. Grimes  * This is the function that saves the entire input, storing the data in a
1919b50d902SRodney W. Grimes  * doubly linked list of buffers and then displays them in reverse order.
1929b50d902SRodney W. Grimes  * It has the usual nastiness of trying to find the newlines, as there's no
1939b50d902SRodney W. Grimes  * guarantee that a newline occurs anywhere in the file, let alone in any
1949b50d902SRodney W. Grimes  * particular buffer.  If we run out of memory, input is discarded (and the
1959b50d902SRodney W. Grimes  * user warned).
1969b50d902SRodney W. Grimes  */
1979b50d902SRodney W. Grimes static void
1989b50d902SRodney W. Grimes r_buf(fp)
1999b50d902SRodney W. Grimes 	FILE *fp;
2009b50d902SRodney W. Grimes {
20148a1ef22SJeroen Ruigrok van der Werven 	BF *mark, *tl, *tr;
20248a1ef22SJeroen Ruigrok van der Werven 	int ch, len, llen;
20348a1ef22SJeroen Ruigrok van der Werven 	char *p;
2049b50d902SRodney W. Grimes 	off_t enomem;
2059b50d902SRodney W. Grimes 
2069b50d902SRodney W. Grimes #define	BSZ	(128 * 1024)
2079b50d902SRodney W. Grimes 	for (mark = NULL, enomem = 0;;) {
2089b50d902SRodney W. Grimes 		/*
2099b50d902SRodney W. Grimes 		 * Allocate a new block and link it into place in a doubly
2109b50d902SRodney W. Grimes 		 * linked list.  If out of memory, toss the LRU block and
2119b50d902SRodney W. Grimes 		 * keep going.
2129b50d902SRodney W. Grimes 		 */
2139b50d902SRodney W. Grimes 		if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
2149b50d902SRodney W. Grimes 		    (tl->l = malloc(BSZ)) == NULL) {
2159b50d902SRodney W. Grimes 			if (!mark)
216ac551270SPeter Wemm 				err(1, "malloc");
2179b50d902SRodney W. Grimes 			tl = enomem ? tl->next : mark;
2189b50d902SRodney W. Grimes 			enomem += tl->len;
2199b50d902SRodney W. Grimes 		} else if (mark) {
2209b50d902SRodney W. Grimes 			tl->next = mark;
2219b50d902SRodney W. Grimes 			tl->prev = mark->prev;
2229b50d902SRodney W. Grimes 			mark->prev->next = tl;
2239b50d902SRodney W. Grimes 			mark->prev = tl;
2249b50d902SRodney W. Grimes 		} else
2259b50d902SRodney W. Grimes 			mark->next = mark->prev = (mark = tl);
2269b50d902SRodney W. Grimes 
2279b50d902SRodney W. Grimes 		/* Fill the block with input data. */
2289b50d902SRodney W. Grimes 		for (p = tl->l, len = 0;
2299b50d902SRodney W. Grimes 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
2309b50d902SRodney W. Grimes 			*p++ = ch;
2319b50d902SRodney W. Grimes 
23249a598abSAdam David 		if (ferror(fp)) {
23349a598abSAdam David 			ierr();
23449a598abSAdam David 			return;
23549a598abSAdam David 		}
23649a598abSAdam David 
2379b50d902SRodney W. Grimes 		/*
2389b50d902SRodney W. Grimes 		 * If no input data for this block and we tossed some data,
2399b50d902SRodney W. Grimes 		 * recover it.
2409b50d902SRodney W. Grimes 		 */
2419b50d902SRodney W. Grimes 		if (!len) {
2429b50d902SRodney W. Grimes 			if (enomem)
2439b50d902SRodney W. Grimes 				enomem -= tl->len;
2449b50d902SRodney W. Grimes 			tl = tl->prev;
2459b50d902SRodney W. Grimes 			break;
2469b50d902SRodney W. Grimes 		}
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 		tl->len = len;
2499b50d902SRodney W. Grimes 		if (ch == EOF)
2509b50d902SRodney W. Grimes 			break;
2519b50d902SRodney W. Grimes 	}
2529b50d902SRodney W. Grimes 
2539b50d902SRodney W. Grimes 	if (enomem) {
25422694ebaSBruce Evans 		warnx("warning: %qd bytes discarded", enomem);
2559b50d902SRodney W. Grimes 		rval = 1;
2569b50d902SRodney W. Grimes 	}
2579b50d902SRodney W. Grimes 
2589b50d902SRodney W. Grimes 	/*
2599b50d902SRodney W. Grimes 	 * Step through the blocks in the reverse order read.  The last char
2609b50d902SRodney W. Grimes 	 * is special, ignore whether newline or not.
2619b50d902SRodney W. Grimes 	 */
2629b50d902SRodney W. Grimes 	for (mark = tl;;) {
2639b50d902SRodney W. Grimes 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
2649b50d902SRodney W. Grimes 		    --p, ++llen)
2659b50d902SRodney W. Grimes 			if (*p == '\n') {
2669b50d902SRodney W. Grimes 				if (llen) {
2679b50d902SRodney W. Grimes 					WR(p + 1, llen);
2689b50d902SRodney W. Grimes 					llen = 0;
2699b50d902SRodney W. Grimes 				}
2709b50d902SRodney W. Grimes 				if (tl == mark)
2719b50d902SRodney W. Grimes 					continue;
2729b50d902SRodney W. Grimes 				for (tr = tl->next; tr->len; tr = tr->next) {
2739b50d902SRodney W. Grimes 					WR(tr->l, tr->len);
2749b50d902SRodney W. Grimes 					tr->len = 0;
2759b50d902SRodney W. Grimes 					if (tr == mark)
2769b50d902SRodney W. Grimes 						break;
2779b50d902SRodney W. Grimes 				}
2789b50d902SRodney W. Grimes 			}
2799b50d902SRodney W. Grimes 		tl->len = llen;
2809b50d902SRodney W. Grimes 		if ((tl = tl->prev) == mark)
2819b50d902SRodney W. Grimes 			break;
2829b50d902SRodney W. Grimes 	}
2839b50d902SRodney W. Grimes 	tl = tl->next;
2849b50d902SRodney W. Grimes 	if (tl->len) {
2859b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2869b50d902SRodney W. Grimes 		tl->len = 0;
2879b50d902SRodney W. Grimes 	}
2889b50d902SRodney W. Grimes 	while ((tl = tl->next)->len) {
2899b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2909b50d902SRodney W. Grimes 		tl->len = 0;
2919b50d902SRodney W. Grimes 	}
2929b50d902SRodney W. Grimes }
293