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