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