]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.bin/tail/reverse.c
MFC r311160, r311210, r311288, r311292, r311298, r311340
[FreeBSD/stable/10.git] / usr.bin / tail / reverse.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #if 0
34 #ifndef lint
35 static char sccsid[] = "@(#)reverse.c   8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37 #endif
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/mman.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <stdint.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "extern.h"
56
57 static void r_buf(FILE *, const char *);
58 static void r_reg(FILE *, const char *, enum STYLE, off_t, struct stat *);
59
60 /*
61  * reverse -- display input in reverse order by line.
62  *
63  * There are six separate cases for this -- regular and non-regular
64  * files by bytes, lines or the whole file.
65  *
66  * BYTES        display N bytes
67  *      REG     mmap the file and display the lines
68  *      NOREG   cyclically read characters into a wrap-around buffer
69  *
70  * LINES        display N lines
71  *      REG     mmap the file and display the lines
72  *      NOREG   cyclically read lines into a wrap-around array of buffers
73  *
74  * FILE         display the entire file
75  *      REG     mmap the file and display the lines
76  *      NOREG   cyclically read input into a linked list of buffers
77  */
78 void
79 reverse(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
80 {
81         if (style != REVERSE && off == 0)
82                 return;
83
84         if (S_ISREG(sbp->st_mode))
85                 r_reg(fp, fn, style, off, sbp);
86         else
87                 switch(style) {
88                 case FBYTES:
89                 case RBYTES:
90                         bytes(fp, fn, off);
91                         break;
92                 case FLINES:
93                 case RLINES:
94                         lines(fp, fn, off);
95                         break;
96                 case REVERSE:
97                         r_buf(fp, fn);
98                         break;
99                 default:
100                         break;
101                 }
102 }
103
104 /*
105  * r_reg -- display a regular file in reverse order by line.
106  */
107 static void
108 r_reg(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
109 {
110         struct mapinfo map;
111         off_t curoff, size, lineend;
112         int i;
113
114         if (!(size = sbp->st_size))
115                 return;
116
117         map.start = NULL;
118         map.mapoff = map.maxoff = size;
119         map.fd = fileno(fp);
120         map.maplen = 0;
121
122         /*
123          * Last char is special, ignore whether newline or not. Note that
124          * size == 0 is dealt with above, and size == 1 sets curoff to -1.
125          */
126         curoff = size - 2;
127         lineend = size;
128         while (curoff >= 0) {
129                 if (curoff < map.mapoff ||
130                     curoff >= map.mapoff + (off_t)map.maplen) {
131                         if (maparound(&map, curoff) != 0) {
132                                 ierr(fn);
133                                 return;
134                         }
135                 }
136                 for (i = curoff - map.mapoff; i >= 0; i--) {
137                         if (style == RBYTES && --off == 0)
138                                 break;
139                         if (map.start[i] == '\n')
140                                 break;
141                 }
142                 /* `i' is either the map offset of a '\n', or -1. */
143                 curoff = map.mapoff + i;
144                 if (i < 0)
145                         continue;
146
147                 /* Print the line and update offsets. */
148                 if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
149                         ierr(fn);
150                         return;
151                 }
152                 lineend = curoff + 1;
153                 curoff--;
154
155                 if (style == RLINES)
156                         off--;
157
158                 if (off == 0 && style != REVERSE) {
159                         /* Avoid printing anything below. */
160                         curoff = 0;
161                         break;
162                 }
163         }
164         if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
165                 ierr(fn);
166                 return;
167         }
168         if (map.start != NULL && munmap(map.start, map.maplen))
169                 ierr(fn);
170 }
171
172 typedef struct bf {
173         struct bf *next;
174         struct bf *prev;
175         int len;
176         char *l;
177 } BF;
178
179 /*
180  * r_buf -- display a non-regular file in reverse order by line.
181  *
182  * This is the function that saves the entire input, storing the data in a
183  * doubly linked list of buffers and then displays them in reverse order.
184  * It has the usual nastiness of trying to find the newlines, as there's no
185  * guarantee that a newline occurs anywhere in the file, let alone in any
186  * particular buffer.  If we run out of memory, input is discarded (and the
187  * user warned).
188  */
189 static void
190 r_buf(FILE *fp, const char *fn)
191 {
192         BF *mark, *tl, *tr;
193         int ch, len, llen;
194         char *p;
195         off_t enomem;
196
197         tl = NULL;
198 #define BSZ     (128 * 1024)
199         for (mark = NULL, enomem = 0;;) {
200                 /*
201                  * Allocate a new block and link it into place in a doubly
202                  * linked list.  If out of memory, toss the LRU block and
203                  * keep going.
204                  */
205                 if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
206                     (tl->l = malloc(BSZ)) == NULL) {
207                         if (!mark)
208                                 err(1, "malloc");
209                         if (enomem)
210                                 tl = tl->next;
211                         else {
212                                 if (tl)
213                                         free(tl);
214                                 tl = mark;
215                         }
216                         enomem += tl->len;
217                 } else if (mark) {
218                         tl->next = mark;
219                         tl->prev = mark->prev;
220                         mark->prev->next = tl;
221                         mark->prev = tl;
222                 } else {
223                         mark = tl;
224                         mark->next = mark->prev = mark;
225                 }
226
227                 /* Fill the block with input data. */
228                 for (p = tl->l, len = 0;
229                     len < BSZ && (ch = getc(fp)) != EOF; ++len)
230                         *p++ = ch;
231
232                 if (ferror(fp)) {
233                         ierr(fn);
234                         return;
235                 }
236
237                 /*
238                  * If no input data for this block and we tossed some data,
239                  * recover it.
240                  */
241                 if (!len && enomem) {
242                         enomem -= tl->len;
243                         tl = tl->prev;
244                         break;
245                 }
246
247                 tl->len = len;
248                 if (ch == EOF)
249                         break;
250         }
251
252         if (enomem) {
253                 warnx("warning: %jd bytes discarded", (intmax_t)enomem);
254                 rval = 1;
255         }
256
257         /*
258          * Step through the blocks in the reverse order read.  The last char
259          * is special, ignore whether newline or not.
260          */
261         for (mark = tl;;) {
262                 for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
263                     --p, ++llen)
264                         if (*p == '\n') {
265                                 if (llen) {
266                                         WR(p + 1, llen);
267                                         llen = 0;
268                                 }
269                                 if (tl == mark)
270                                         continue;
271                                 for (tr = tl->next; tr->len; tr = tr->next) {
272                                         WR(tr->l, tr->len);
273                                         tr->len = 0;
274                                         if (tr == mark)
275                                                 break;
276                                 }
277                         }
278                 tl->len = llen;
279                 if ((tl = tl->prev) == mark)
280                         break;
281         }
282         tl = tl->next;
283         if (tl->len) {
284                 WR(tl->l, tl->len);
285                 tl->len = 0;
286         }
287         while ((tl = tl->next)->len) {
288                 WR(tl->l, tl->len);
289                 tl->len = 0;
290         }
291 }