xref: /netbsd-src/usr.bin/tail/reverse.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Sze-Tyan Wang.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)reverse.c	5.3 (Berkeley) 2/12/92";*/
39 static char rcsid[] = "$Id: reverse.c,v 1.2 1993/08/01 18:08:02 mycroft Exp $";
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/mman.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "extern.h"
51 
52 static void r_buf __P((FILE *));
53 static void r_reg __P((FILE *, enum STYLE, long, struct stat *));
54 
55 /*
56  * reverse -- display input in reverse order by line.
57  *
58  * There are six separate cases for this -- regular and non-regular
59  * files by bytes, lines or the whole file.
60  *
61  * BYTES	display N bytes
62  *	REG	mmap the file and display the lines
63  *	NOREG	cyclically read characters into a wrap-around buffer
64  *
65  * LINES	display N lines
66  *	REG	mmap the file and display the lines
67  *	NOREG	cyclically read lines into a wrap-around array of buffers
68  *
69  * FILE		display the entire file
70  *	REG	mmap the file and display the lines
71  *	NOREG	cyclically read input into a linked list of buffers
72  */
73 void
74 reverse(fp, style, off, sbp)
75 	FILE *fp;
76 	enum STYLE style;
77 	long off;
78 	struct stat *sbp;
79 {
80 	if (style != REVERSE && off == 0)
81 		return;
82 
83 	if (S_ISREG(sbp->st_mode))
84 		r_reg(fp, style, off, sbp);
85 	else
86 		switch(style) {
87 		case FBYTES:
88 			bytes(fp, off);
89 			break;
90 		case FLINES:
91 			lines(fp, off);
92 			break;
93 		case REVERSE:
94 			r_buf(fp);
95 			break;
96 		}
97 }
98 
99 /*
100  * r_reg -- display a regular file in reverse order by line.
101  */
102 static void
103 r_reg(fp, style, off, sbp)
104 	FILE *fp;
105 	register enum STYLE style;
106 	long off;
107 	struct stat *sbp;
108 {
109 	register off_t size;
110 	register int llen;
111 	register char *p;
112 	int fd;
113 
114 	if (!(size = sbp->st_size))
115 		return;
116 
117 	fd = fileno(fp);
118 	if ((p =
119 	    mmap(NULL, size, PROT_READ, MAP_FILE, fd, (off_t)0)) == (caddr_t)-1)
120 		err("%s", strerror(errno));
121 	p += size - 1;
122 
123 	if (style == RBYTES && off < size)
124 		size = off;
125 
126 	/* Last char is special, ignore whether newline or not. */
127 	for (llen = 1; --size; ++llen)
128 		if (*--p == '\n') {
129 			WR(p + 1, llen);
130 			llen = 0;
131 			if (style == RLINES && !--off) {
132 				++p;
133 				break;
134 			}
135 		}
136 	if (llen)
137 		WR(p, llen);
138 }
139 
140 typedef struct bf {
141 	struct bf *next;
142 	struct bf *prev;
143 	int len;
144 	char *l;
145 } BF;
146 
147 /*
148  * r_buf -- display a non-regular file in reverse order by line.
149  *
150  * This is the function that saves the entire input, storing the data in a
151  * doubly linked list of buffers and then displays them in reverse order.
152  * It has the usual nastiness of trying to find the newlines, as there's no
153  * guarantee that a newline occurs anywhere in the file, let alone in any
154  * particular buffer.  If we run out of memory, input is discarded (and the
155  * user warned).
156  */
157 static void
158 r_buf(fp)
159 	FILE *fp;
160 {
161 	register BF *mark, *tl, *tr;
162 	register int ch, len, llen;
163 	register char *p;
164 	off_t enomem;
165 
166 #define	BSZ	(128 * 1024)
167 	for (mark = NULL, enomem = 0;;) {
168 		/*
169 		 * Allocate a new block and link it into place in a doubly
170 		 * linked list.  If out of memory, toss the LRU block and
171 		 * keep going.
172 		 */
173 		if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
174 		    (tl->l = malloc(BSZ)) == NULL) {
175 			if (!mark)
176 				err("%s", strerror(errno));
177 			tl = enomem ? tl->next : mark;
178 			enomem += tl->len;
179 		} else if (mark) {
180 			tl->next = mark;
181 			tl->prev = mark->prev;
182 			mark->prev->next = tl;
183 			mark->prev = tl;
184 		} else
185 			mark->next = mark->prev = (mark = tl);
186 
187 		/* Fill the block with input data. */
188 		for (p = tl->l, len = 0;
189 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
190 			*p++ = ch;
191 
192 		/*
193 		 * If no input data for this block and we tossed some data,
194 		 * recover it.
195 		 */
196 		if (!len) {
197 			if (enomem)
198 				enomem -= tl->len;
199 			tl = tl->prev;
200 			break;
201 		}
202 
203 		tl->len = len;
204 		if (ch == EOF)
205 			break;
206 	}
207 
208 	if (enomem) {
209 		(void)fprintf(stderr,
210 		    "tail: warning: %ld bytes discarded\n", enomem);
211 		rval = 1;
212 	}
213 
214 	/*
215 	 * Step through the blocks in the reverse order read.  The last char
216 	 * is special, ignore whether newline or not.
217 	 */
218 	for (mark = tl;;) {
219 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
220 		    --p, ++llen)
221 			if (*p == '\n') {
222 				if (llen) {
223 					WR(p + 1, llen);
224 					llen = 0;
225 				}
226 				if (tl == mark)
227 					continue;
228 				for (tr = tl->next; tr->len; tr = tr->next) {
229 					WR(tr->l, tr->len);
230 					tr->len = 0;
231 					if (tr == mark)
232 						break;
233 				}
234 			}
235 		tl->len = llen;
236 		if ((tl = tl->prev) == mark)
237 			break;
238 	}
239 	tl = tl->next;
240 	if (tl->len) {
241 		WR(tl->l, tl->len);
242 		tl->len = 0;
243 	}
244 	while ((tl = tl->next)->len) {
245 		WR(tl->l, tl->len);
246 		tl->len = 0;
247 	}
248 }
249