xref: /freebsd/usr.bin/tail/reverse.c (revision 13685eeec2456c781616eed7a4e0b8a7934ab383)
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 
3713685eeeSDavid Malone #if 0
389b50d902SRodney W. Grimes #ifndef lint
3913685eeeSDavid Malone static char sccsid[] = "@(#)reverse.c	8.1 (Berkeley) 6/6/93";
4013685eeeSDavid Malone #endif /* not lint */
41ea7cc495SPhilippe Charnier #endif
429b50d902SRodney W. Grimes 
4313685eeeSDavid Malone #include <sys/cdefs.h>
4413685eeeSDavid Malone __FBSDID("$FreeBSD$");
4513685eeeSDavid Malone 
469b50d902SRodney W. Grimes #include <sys/param.h>
479b50d902SRodney W. Grimes #include <sys/stat.h>
489b50d902SRodney W. Grimes #include <sys/mman.h>
499b50d902SRodney W. Grimes 
50814e3a92SMark Murray #include <err.h>
519b50d902SRodney W. Grimes #include <errno.h>
52814e3a92SMark Murray #include <limits.h>
539b50d902SRodney W. Grimes #include <stdio.h>
549b50d902SRodney W. Grimes #include <stdlib.h>
559b50d902SRodney W. Grimes #include <string.h>
56814e3a92SMark Murray #include <unistd.h>
57814e3a92SMark Murray 
589b50d902SRodney W. Grimes #include "extern.h"
599b50d902SRodney W. Grimes 
603f330d7dSWarner Losh static void r_buf(FILE *);
613f330d7dSWarner Losh static void r_reg(FILE *, enum STYLE, off_t, struct stat *);
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes /*
649b50d902SRodney W. Grimes  * reverse -- display input in reverse order by line.
659b50d902SRodney W. Grimes  *
669b50d902SRodney W. Grimes  * There are six separate cases for this -- regular and non-regular
679b50d902SRodney W. Grimes  * files by bytes, lines or the whole file.
689b50d902SRodney W. Grimes  *
699b50d902SRodney W. Grimes  * BYTES	display N bytes
709b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
719b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
729b50d902SRodney W. Grimes  *
739b50d902SRodney W. Grimes  * LINES	display N lines
749b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
759b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
769b50d902SRodney W. Grimes  *
779b50d902SRodney W. Grimes  * FILE		display the entire file
789b50d902SRodney W. Grimes  *	REG	mmap the file and display the lines
799b50d902SRodney W. Grimes  *	NOREG	cyclically read input into a linked list of buffers
809b50d902SRodney W. Grimes  */
819b50d902SRodney W. Grimes void
829b50d902SRodney W. Grimes reverse(fp, style, off, sbp)
839b50d902SRodney W. Grimes 	FILE *fp;
849b50d902SRodney W. Grimes 	enum STYLE style;
85bd9dc975SAndrey A. Chernov 	off_t off;
869b50d902SRodney W. Grimes 	struct stat *sbp;
879b50d902SRodney W. Grimes {
889b50d902SRodney W. Grimes 	if (style != REVERSE && off == 0)
899b50d902SRodney W. Grimes 		return;
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes 	if (S_ISREG(sbp->st_mode))
929b50d902SRodney W. Grimes 		r_reg(fp, style, off, sbp);
939b50d902SRodney W. Grimes 	else
949b50d902SRodney W. Grimes 		switch(style) {
959b50d902SRodney W. Grimes 		case FBYTES:
969b50d902SRodney W. Grimes 		case RBYTES:
979b50d902SRodney W. Grimes 			bytes(fp, off);
989b50d902SRodney W. Grimes 			break;
999b50d902SRodney W. Grimes 		case FLINES:
1009b50d902SRodney W. Grimes 		case RLINES:
1019b50d902SRodney W. Grimes 			lines(fp, off);
1029b50d902SRodney W. Grimes 			break;
1039b50d902SRodney W. Grimes 		case REVERSE:
1049b50d902SRodney W. Grimes 			r_buf(fp);
1059b50d902SRodney W. Grimes 			break;
106814e3a92SMark Murray 		default:
107b77b9b9aSMurray Stokely 			break;
1089b50d902SRodney W. Grimes 		}
1099b50d902SRodney W. Grimes }
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes /*
1129b50d902SRodney W. Grimes  * r_reg -- display a regular file in reverse order by line.
1139b50d902SRodney W. Grimes  */
1149b50d902SRodney W. Grimes static void
1159b50d902SRodney W. Grimes r_reg(fp, style, off, sbp)
1169b50d902SRodney W. Grimes 	FILE *fp;
11748a1ef22SJeroen Ruigrok van der Werven 	enum STYLE style;
118bd9dc975SAndrey A. Chernov 	off_t off;
1199b50d902SRodney W. Grimes 	struct stat *sbp;
1209b50d902SRodney W. Grimes {
121726098d3SDavid Malone 	struct mapinfo map;
122726098d3SDavid Malone 	off_t curoff, size, lineend;
123726098d3SDavid Malone 	int i;
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
1269b50d902SRodney W. Grimes 		return;
1279b50d902SRodney W. Grimes 
128726098d3SDavid Malone 	map.start = NULL;
129726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
130726098d3SDavid Malone 	map.fd = fileno(fp);
131726098d3SDavid Malone 
132726098d3SDavid Malone 	/*
133726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
134726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
135726098d3SDavid Malone 	 */
136726098d3SDavid Malone 	curoff = size - 2;
137726098d3SDavid Malone 	lineend = size;
138726098d3SDavid Malone 	while (curoff >= 0) {
139726098d3SDavid Malone 		if (curoff < map.mapoff || curoff >= map.mapoff + map.maplen) {
140726098d3SDavid Malone 			if (maparound(&map, curoff) != 0) {
14144cf272fSAdam David 				ierr();
1429b50d902SRodney W. Grimes 				return;
1439b50d902SRodney W. Grimes 			}
144726098d3SDavid Malone 		}
145726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--) {
146726098d3SDavid Malone 			if (style == RBYTES && --off == 0)
147726098d3SDavid Malone 				break;
148726098d3SDavid Malone 			if (map.start[i] == '\n')
149726098d3SDavid Malone 				break;
150726098d3SDavid Malone 		}
151726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
152726098d3SDavid Malone 		curoff = map.mapoff + i;
153726098d3SDavid Malone 		if (i < 0)
154726098d3SDavid Malone 			continue;
1559b50d902SRodney W. Grimes 
156726098d3SDavid Malone 		/* Print the line and update offsets. */
157726098d3SDavid Malone 		if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
15844cf272fSAdam David 			ierr();
1599b50d902SRodney W. Grimes 			return;
1609b50d902SRodney W. Grimes 		}
161726098d3SDavid Malone 		lineend = curoff + 1;
162726098d3SDavid Malone 		curoff--;
1639b50d902SRodney W. Grimes 
164726098d3SDavid Malone 		if (style == RLINES)
165726098d3SDavid Malone 			off--;
1669b50d902SRodney W. Grimes 
167726098d3SDavid Malone 		if (off == 0 && style != REVERSE) {
168726098d3SDavid Malone 			/* Avoid printing anything below. */
169726098d3SDavid Malone 			curoff = 0;
1709b50d902SRodney W. Grimes 			break;
1719b50d902SRodney W. Grimes 		}
1729b50d902SRodney W. Grimes 	}
173726098d3SDavid Malone 	if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
174726098d3SDavid Malone 		ierr();
175726098d3SDavid Malone 		return;
176726098d3SDavid Malone 	}
177726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen))
17844cf272fSAdam David 		ierr();
1799b50d902SRodney W. Grimes }
1809b50d902SRodney W. Grimes 
1819b50d902SRodney W. Grimes typedef struct bf {
1829b50d902SRodney W. Grimes 	struct bf *next;
1839b50d902SRodney W. Grimes 	struct bf *prev;
1849b50d902SRodney W. Grimes 	int len;
1859b50d902SRodney W. Grimes 	char *l;
1869b50d902SRodney W. Grimes } BF;
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes /*
1899b50d902SRodney W. Grimes  * r_buf -- display a non-regular file in reverse order by line.
1909b50d902SRodney W. Grimes  *
1919b50d902SRodney W. Grimes  * This is the function that saves the entire input, storing the data in a
1929b50d902SRodney W. Grimes  * doubly linked list of buffers and then displays them in reverse order.
1939b50d902SRodney W. Grimes  * It has the usual nastiness of trying to find the newlines, as there's no
1949b50d902SRodney W. Grimes  * guarantee that a newline occurs anywhere in the file, let alone in any
1959b50d902SRodney W. Grimes  * particular buffer.  If we run out of memory, input is discarded (and the
1969b50d902SRodney W. Grimes  * user warned).
1979b50d902SRodney W. Grimes  */
1989b50d902SRodney W. Grimes static void
1999b50d902SRodney W. Grimes r_buf(fp)
2009b50d902SRodney W. Grimes 	FILE *fp;
2019b50d902SRodney W. Grimes {
20248a1ef22SJeroen Ruigrok van der Werven 	BF *mark, *tl, *tr;
20348a1ef22SJeroen Ruigrok van der Werven 	int ch, len, llen;
20448a1ef22SJeroen Ruigrok van der Werven 	char *p;
2059b50d902SRodney W. Grimes 	off_t enomem;
2069b50d902SRodney W. Grimes 
2079b50d902SRodney W. Grimes #define	BSZ	(128 * 1024)
2089b50d902SRodney W. Grimes 	for (mark = NULL, enomem = 0;;) {
2099b50d902SRodney W. Grimes 		/*
2109b50d902SRodney W. Grimes 		 * Allocate a new block and link it into place in a doubly
2119b50d902SRodney W. Grimes 		 * linked list.  If out of memory, toss the LRU block and
2129b50d902SRodney W. Grimes 		 * keep going.
2139b50d902SRodney W. Grimes 		 */
2149b50d902SRodney W. Grimes 		if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
2159b50d902SRodney W. Grimes 		    (tl->l = malloc(BSZ)) == NULL) {
2169b50d902SRodney W. Grimes 			if (!mark)
217ac551270SPeter Wemm 				err(1, "malloc");
2189b50d902SRodney W. Grimes 			tl = enomem ? tl->next : mark;
2199b50d902SRodney W. Grimes 			enomem += tl->len;
2209b50d902SRodney W. Grimes 		} else if (mark) {
2219b50d902SRodney W. Grimes 			tl->next = mark;
2229b50d902SRodney W. Grimes 			tl->prev = mark->prev;
2239b50d902SRodney W. Grimes 			mark->prev->next = tl;
2249b50d902SRodney W. Grimes 			mark->prev = tl;
22513685eeeSDavid Malone 		} else {
22613685eeeSDavid Malone 			mark = tl;
22713685eeeSDavid Malone 			mark->next = mark->prev = mark;
22813685eeeSDavid Malone 		}
2299b50d902SRodney W. Grimes 
2309b50d902SRodney W. Grimes 		/* Fill the block with input data. */
2319b50d902SRodney W. Grimes 		for (p = tl->l, len = 0;
2329b50d902SRodney W. Grimes 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
2339b50d902SRodney W. Grimes 			*p++ = ch;
2349b50d902SRodney W. Grimes 
23549a598abSAdam David 		if (ferror(fp)) {
23649a598abSAdam David 			ierr();
23749a598abSAdam David 			return;
23849a598abSAdam David 		}
23949a598abSAdam David 
2409b50d902SRodney W. Grimes 		/*
2419b50d902SRodney W. Grimes 		 * If no input data for this block and we tossed some data,
2429b50d902SRodney W. Grimes 		 * recover it.
2439b50d902SRodney W. Grimes 		 */
2449b50d902SRodney W. Grimes 		if (!len) {
2459b50d902SRodney W. Grimes 			if (enomem)
2469b50d902SRodney W. Grimes 				enomem -= tl->len;
2479b50d902SRodney W. Grimes 			tl = tl->prev;
2489b50d902SRodney W. Grimes 			break;
2499b50d902SRodney W. Grimes 		}
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 		tl->len = len;
2529b50d902SRodney W. Grimes 		if (ch == EOF)
2539b50d902SRodney W. Grimes 			break;
2549b50d902SRodney W. Grimes 	}
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes 	if (enomem) {
25722694ebaSBruce Evans 		warnx("warning: %qd bytes discarded", enomem);
2589b50d902SRodney W. Grimes 		rval = 1;
2599b50d902SRodney W. Grimes 	}
2609b50d902SRodney W. Grimes 
2619b50d902SRodney W. Grimes 	/*
2629b50d902SRodney W. Grimes 	 * Step through the blocks in the reverse order read.  The last char
2639b50d902SRodney W. Grimes 	 * is special, ignore whether newline or not.
2649b50d902SRodney W. Grimes 	 */
2659b50d902SRodney W. Grimes 	for (mark = tl;;) {
2669b50d902SRodney W. Grimes 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
2679b50d902SRodney W. Grimes 		    --p, ++llen)
2689b50d902SRodney W. Grimes 			if (*p == '\n') {
2699b50d902SRodney W. Grimes 				if (llen) {
2709b50d902SRodney W. Grimes 					WR(p + 1, llen);
2719b50d902SRodney W. Grimes 					llen = 0;
2729b50d902SRodney W. Grimes 				}
2739b50d902SRodney W. Grimes 				if (tl == mark)
2749b50d902SRodney W. Grimes 					continue;
2759b50d902SRodney W. Grimes 				for (tr = tl->next; tr->len; tr = tr->next) {
2769b50d902SRodney W. Grimes 					WR(tr->l, tr->len);
2779b50d902SRodney W. Grimes 					tr->len = 0;
2789b50d902SRodney W. Grimes 					if (tr == mark)
2799b50d902SRodney W. Grimes 						break;
2809b50d902SRodney W. Grimes 				}
2819b50d902SRodney W. Grimes 			}
2829b50d902SRodney W. Grimes 		tl->len = llen;
2839b50d902SRodney W. Grimes 		if ((tl = tl->prev) == mark)
2849b50d902SRodney W. Grimes 			break;
2859b50d902SRodney W. Grimes 	}
2869b50d902SRodney W. Grimes 	tl = tl->next;
2879b50d902SRodney W. Grimes 	if (tl->len) {
2889b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2899b50d902SRodney W. Grimes 		tl->len = 0;
2909b50d902SRodney W. Grimes 	}
2919b50d902SRodney W. Grimes 	while ((tl = tl->next)->len) {
2929b50d902SRodney W. Grimes 		WR(tl->l, tl->len);
2939b50d902SRodney W. Grimes 		tl->len = 0;
2949b50d902SRodney W. Grimes 	}
2959b50d902SRodney W. Grimes }
296