Annotation of src/usr.bin/tail/reverse.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: reverse.c,v 1.6 1994/11/23 07:42:10 jtc 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.2 ! deraadt 44: static char rcsid[] = "$OpenBSD: reverse.c,v 1.6 1994/11/23 07:42:10 jtc 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:
51: #include <limits.h>
52: #include <errno.h>
53: #include <unistd.h>
54: #include <stdio.h>
55: #include <stdlib.h>
56: #include <string.h>
57: #include "extern.h"
58:
59: static void r_buf __P((FILE *));
60: static void r_reg __P((FILE *, enum STYLE, long, struct stat *));
61:
62: /*
63: * reverse -- display input in reverse order by line.
64: *
65: * There are six separate cases for this -- regular and non-regular
66: * files by bytes, lines or the whole file.
67: *
68: * BYTES display N bytes
69: * REG mmap the file and display the lines
70: * NOREG cyclically read characters into a wrap-around buffer
71: *
72: * LINES display N lines
73: * REG mmap the file and display the lines
74: * NOREG cyclically read lines into a wrap-around array of buffers
75: *
76: * FILE display the entire file
77: * REG mmap the file and display the lines
78: * NOREG cyclically read input into a linked list of buffers
79: */
80: void
81: reverse(fp, style, off, sbp)
82: FILE *fp;
83: enum STYLE style;
84: long off;
85: struct stat *sbp;
86: {
87: if (style != REVERSE && off == 0)
88: return;
89:
90: if (S_ISREG(sbp->st_mode))
91: r_reg(fp, style, off, sbp);
92: else
93: switch(style) {
94: case FBYTES:
95: case RBYTES:
96: bytes(fp, off);
97: break;
98: case FLINES:
99: case RLINES:
100: lines(fp, off);
101: break;
102: case REVERSE:
103: r_buf(fp);
104: break;
105: }
106: }
107:
108: /*
109: * r_reg -- display a regular file in reverse order by line.
110: */
111: static void
112: r_reg(fp, style, off, sbp)
113: FILE *fp;
114: register enum STYLE style;
115: long off;
116: struct stat *sbp;
117: {
118: register off_t size;
119: register int llen;
120: register char *p;
121: char *start;
122:
123: if (!(size = sbp->st_size))
124: return;
125:
126: if (size > SIZE_T_MAX) {
127: err(0, "%s: %s", fname, strerror(EFBIG));
128: return;
129: }
130:
131: if ((start = mmap(NULL, (size_t)size,
132: PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
133: err(0, "%s: %s", fname, strerror(EFBIG));
134: return;
135: }
136: p = start + size - 1;
137:
138: if (style == RBYTES && off < size)
139: size = off;
140:
141: /* Last char is special, ignore whether newline or not. */
142: for (llen = 1; --size; ++llen)
143: if (*--p == '\n') {
144: WR(p + 1, llen);
145: llen = 0;
146: if (style == RLINES && !--off) {
147: ++p;
148: break;
149: }
150: }
151: if (llen)
152: WR(p, llen);
153: if (munmap(start, (size_t)sbp->st_size))
154: err(0, "%s: %s", fname, strerror(errno));
155: }
156:
157: typedef struct bf {
158: struct bf *next;
159: struct bf *prev;
160: int len;
161: char *l;
162: } BF;
163:
164: /*
165: * r_buf -- display a non-regular file in reverse order by line.
166: *
167: * This is the function that saves the entire input, storing the data in a
168: * doubly linked list of buffers and then displays them in reverse order.
169: * It has the usual nastiness of trying to find the newlines, as there's no
170: * guarantee that a newline occurs anywhere in the file, let alone in any
171: * particular buffer. If we run out of memory, input is discarded (and the
172: * user warned).
173: */
174: static void
175: r_buf(fp)
176: FILE *fp;
177: {
178: register BF *mark, *tl, *tr;
179: register int ch, len, llen;
180: register char *p;
181: off_t enomem;
182:
183: #define BSZ (128 * 1024)
184: for (mark = NULL, enomem = 0;;) {
185: /*
186: * Allocate a new block and link it into place in a doubly
187: * linked list. If out of memory, toss the LRU block and
188: * keep going.
189: */
190: if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
191: (tl->l = malloc(BSZ)) == NULL) {
192: if (!mark)
193: err(1, "%s", strerror(errno));
194: tl = enomem ? tl->next : mark;
195: enomem += tl->len;
196: } else if (mark) {
197: tl->next = mark;
198: tl->prev = mark->prev;
199: mark->prev->next = tl;
200: mark->prev = tl;
201: } else
202: mark->next = mark->prev = (mark = tl);
203:
204: /* Fill the block with input data. */
205: for (p = tl->l, len = 0;
206: len < BSZ && (ch = getc(fp)) != EOF; ++len)
207: *p++ = ch;
208:
209: /*
210: * If no input data for this block and we tossed some data,
211: * recover it.
212: */
213: if (!len) {
214: if (enomem)
215: enomem -= tl->len;
216: tl = tl->prev;
217: break;
218: }
219:
220: tl->len = len;
221: if (ch == EOF)
222: break;
223: }
224:
225: if (enomem) {
226: (void)fprintf(stderr,
227: "tail: warning: %ld bytes discarded\n", enomem);
228: rval = 1;
229: }
230:
231: /*
232: * Step through the blocks in the reverse order read. The last char
233: * is special, ignore whether newline or not.
234: */
235: for (mark = tl;;) {
236: for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
237: --p, ++llen)
238: if (*p == '\n') {
239: if (llen) {
240: WR(p + 1, llen);
241: llen = 0;
242: }
243: if (tl == mark)
244: continue;
245: for (tr = tl->next; tr->len; tr = tr->next) {
246: WR(tr->l, tr->len);
247: tr->len = 0;
248: if (tr == mark)
249: break;
250: }
251: }
252: tl->len = llen;
253: if ((tl = tl->prev) == mark)
254: break;
255: }
256: tl = tl->next;
257: if (tl->len) {
258: WR(tl->l, tl->len);
259: tl->len = 0;
260: }
261: while ((tl = tl->next)->len) {
262: WR(tl->l, tl->len);
263: tl->len = 0;
264: }
265: }