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