xref: /freebsd/usr.bin/tail/reverse.c (revision 3f330d7d1a3fe98c53faf01d0f30c0a9fbb37c41)
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:
1069b50d902SRodney W. Grimes 		}
1079b50d902SRodney W. Grimes }
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes /*
1109b50d902SRodney W. Grimes  * r_reg -- display a regular file in reverse order by line.
1119b50d902SRodney W. Grimes  */
1129b50d902SRodney W. Grimes static void
1139b50d902SRodney W. Grimes r_reg(fp, style, off, sbp)
1149b50d902SRodney W. Grimes 	FILE *fp;
11548a1ef22SJeroen Ruigrok van der Werven 	enum STYLE style;
116bd9dc975SAndrey A. Chernov 	off_t off;
1179b50d902SRodney W. Grimes 	struct stat *sbp;
1189b50d902SRodney W. Grimes {
119726098d3SDavid Malone 	struct mapinfo map;
120726098d3SDavid Malone 	off_t curoff, size, lineend;
121726098d3SDavid Malone 	int i;
1229b50d902SRodney W. Grimes 
1239b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
1249b50d902SRodney W. Grimes 		return;
1259b50d902SRodney W. Grimes 
126726098d3SDavid Malone 	map.start = NULL;
127726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
128726098d3SDavid Malone 	map.fd = fileno(fp);
129726098d3SDavid Malone 
130726098d3SDavid Malone 	/*
131726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
132726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
133726098d3SDavid Malone 	 */
134726098d3SDavid Malone 	curoff = size - 2;
135726098d3SDavid Malone 	lineend = size;
136726098d3SDavid Malone 	while (curoff >= 0) {
137726098d3SDavid Malone 		if (curoff < map.mapoff || curoff >= map.mapoff + map.maplen) {
138726098d3SDavid Malone 			if (maparound(&map, curoff) != 0) {
13944cf272fSAdam David 				ierr();
1409b50d902SRodney W. Grimes 				return;
1419b50d902SRodney W. Grimes 			}
142726098d3SDavid Malone 		}
143726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--) {
144726098d3SDavid Malone 			if (style == RBYTES && --off == 0)
145726098d3SDavid Malone 				break;
146726098d3SDavid Malone 			if (map.start[i] == '\n')
147726098d3SDavid Malone 				break;
148726098d3SDavid Malone 		}
149726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
150726098d3SDavid Malone 		curoff = map.mapoff + i;
151726098d3SDavid Malone 		if (i < 0)
152726098d3SDavid Malone 			continue;
1539b50d902SRodney W. Grimes 
154726098d3SDavid Malone 		/* Print the line and update offsets. */
155726098d3SDavid Malone 		if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
15644cf272fSAdam David 			ierr();
1579b50d902SRodney W. Grimes 			return;
1589b50d902SRodney W. Grimes 		}
159726098d3SDavid Malone 		lineend = curoff + 1;
160726098d3SDavid Malone 		curoff--;
1619b50d902SRodney W. Grimes 
162726098d3SDavid Malone 		if (style == RLINES)
163726098d3SDavid Malone 			off--;
1649b50d902SRodney W. Grimes 
165726098d3SDavid Malone 		if (off == 0 && style != REVERSE) {
166726098d3SDavid Malone 			/* Avoid printing anything below. */
167726098d3SDavid Malone 			curoff = 0;
1689b50d902SRodney W. Grimes 			break;
1699b50d902SRodney W. Grimes 		}
1709b50d902SRodney W. Grimes 	}
171726098d3SDavid Malone 	if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
172726098d3SDavid Malone 		ierr();
173726098d3SDavid Malone 		return;
174726098d3SDavid Malone 	}
175726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen))
17644cf272fSAdam David 		ierr();
1779b50d902SRodney W. Grimes }
1789b50d902SRodney W. Grimes 
1799b50d902SRodney W. Grimes typedef struct bf {
1809b50d902SRodney W. Grimes 	struct bf *next;
1819b50d902SRodney W. Grimes 	struct bf *prev;
1829b50d902SRodney W. Grimes 	int len;
1839b50d902SRodney W. Grimes 	char *l;
1849b50d902SRodney W. Grimes } BF;
1859b50d902SRodney W. Grimes 
1869b50d902SRodney W. Grimes /*
1879b50d902SRodney W. Grimes  * r_buf -- display a non-regular file in reverse order by line.
1889b50d902SRodney W. Grimes  *
1899b50d902SRodney W. Grimes  * This is the function that saves the entire input, storing the data in a
1909b50d902SRodney W. Grimes  * doubly linked list of buffers and then displays them in reverse order.
1919b50d902SRodney W. Grimes  * It has the usual nastiness of trying to find the newlines, as there's no
1929b50d902SRodney W. Grimes  * guarantee that a newline occurs anywhere in the file, let alone in any
1939b50d902SRodney W. Grimes  * particular buffer.  If we run out of memory, input is discarded (and the
1949b50d902SRodney W. Grimes  * user warned).
1959b50d902SRodney W. Grimes  */
1969b50d902SRodney W. Grimes static void
1979b50d902SRodney W. Grimes r_buf(fp)
1989b50d902SRodney W. Grimes 	FILE *fp;
1999b50d902SRodney W. Grimes {
20048a1ef22SJeroen Ruigrok van der Werven 	BF *mark, *tl, *tr;
20148a1ef22SJeroen Ruigrok van der Werven 	int ch, len, llen;
20248a1ef22SJeroen Ruigrok van der Werven 	char *p;
2039b50d902SRodney W. Grimes 	off_t enomem;
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes #define	BSZ	(128 * 1024)
2069b50d902SRodney W. Grimes 	for (mark = NULL, enomem = 0;;) {
2079b50d902SRodney W. Grimes 		/*
2089b50d902SRodney W. Grimes 		 * Allocate a new block and link it into place in a doubly
2099b50d902SRodney W. Grimes 		 * linked list.  If out of memory, toss the LRU block and
2109b50d902SRodney W. Grimes 		 * keep going.
2119b50d902SRodney W. Grimes 		 */
2129b50d902SRodney W. Grimes 		if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
2139b50d902SRodney W. Grimes 		    (tl->l = malloc(BSZ)) == NULL) {
2149b50d902SRodney W. Grimes 			if (!mark)
215ac551270SPeter Wemm 				err(1, "malloc");
2169b50d902SRodney W. Grimes 			tl = enomem ? tl->next : mark;
2179b50d902SRodney W. Grimes 			enomem += tl->len;
2189b50d902SRodney W. Grimes 		} else if (mark) {
2199b50d902SRodney W. Grimes 			tl->next = mark;
2209b50d902SRodney W. Grimes 			tl->prev = mark->prev;
2219b50d902SRodney W. Grimes 			mark->prev->next = tl;
2229b50d902SRodney W. Grimes 			mark->prev = tl;
2239b50d902SRodney W. Grimes 		} else
2249b50d902SRodney W. Grimes 			mark->next = mark->prev = (mark = tl);
2259b50d902SRodney W. Grimes 
2269b50d902SRodney W. Grimes 		/* Fill the block with input data. */
2279b50d902SRodney W. Grimes 		for (p = tl->l, len = 0;
2289b50d902SRodney W. Grimes 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
2299b50d902SRodney W. Grimes 			*p++ = ch;
2309b50d902SRodney W. Grimes 
23149a598abSAdam David 		if (ferror(fp)) {
23249a598abSAdam David 			ierr();
23349a598abSAdam David 			return;
23449a598abSAdam David 		}
23549a598abSAdam David 
2369b50d902SRodney W. Grimes 		/*
2379b50d902SRodney W. Grimes 		 * If no input data for this block and we tossed some data,
2389b50d902SRodney W. Grimes 		 * recover it.
2399b50d902SRodney W. Grimes 		 */
2409b50d902SRodney W. Grimes 		if (!len) {
2419b50d902SRodney W. Grimes 			if (enomem)
2429b50d902SRodney W. Grimes 				enomem -= tl->len;
2439b50d902SRodney W. Grimes 			tl = tl->prev;
2449b50d902SRodney W. Grimes 			break;
2459b50d902SRodney W. Grimes 		}
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes 		tl->len = len;
2489b50d902SRodney W. Grimes 		if (ch == EOF)
2499b50d902SRodney W. Grimes 			break;
2509b50d902SRodney W. Grimes 	}
2519b50d902SRodney W. Grimes 
2529b50d902SRodney W. Grimes 	if (enomem) {
25322694ebaSBruce Evans 		warnx("warning: %qd bytes discarded", enomem);
2549b50d902SRodney W. Grimes 		rval = 1;
2559b50d902SRodney W. Grimes 	}
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes 	/*
2589b50d902SRodney W. Grimes 	 * Step through the blocks in the reverse order read.  The last char
2599b50d902SRodney W. Grimes 	 * is special, ignore whether newline or not.
2609b50d902SRodney W. Grimes 	 */
2619b50d902SRodney W. Grimes 	for (mark = tl;;) {
2629b50d902SRodney W. Grimes 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
2639b50d902SRodney W. Grimes 		    --p, ++llen)
2649b50d902SRodney W. Grimes 			if (*p == '\n') {
2659b50d902SRodney W. Grimes 				if (llen) {
2669b50d902SRodney W. Grimes 					WR(p + 1, llen);
2679b50d902SRodney W. Grimes 					llen = 0;
2689b50d902SRodney W. Grimes 				}
2699b50d902SRodney W. Grimes 				if (tl == mark)
2709b50d902SRodney W. Grimes 					continue;
2719b50d902SRodney W. Grimes 				for (tr = tl->next; tr->len; tr = tr->next) {
2729b50d902SRodney W. Grimes 					WR(tr->l, tr->len);
2739b50d902SRodney W. Grimes 					tr->len = 0;
2749b50d902SRodney W. Grimes 					if (tr == mark)
2759b50d902SRodney W. Grimes 						break;
2769b50d902SRodney W. Grimes 				}
2779b50d902SRodney W. Grimes 			}
2789b50d902SRodney W. Grimes 		tl->len = llen;
2799b50d902SRodney W. Grimes 		if ((tl = tl->prev) == mark)
2809b50d902SRodney W. Grimes 			break;
2819b50d902SRodney W. Grimes 	}
2829b50d902SRodney W. Grimes 	tl = tl->next;
2839b50d902SRodney W. Grimes 	if (tl->len) {
2849b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2859b50d902SRodney W. Grimes 		tl->len = 0;
2869b50d902SRodney W. Grimes 	}
2879b50d902SRodney W. Grimes 	while ((tl = tl->next)->len) {
2889b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2899b50d902SRodney W. Grimes 		tl->len = 0;
2909b50d902SRodney W. Grimes 	}
2919b50d902SRodney W. Grimes }
292