1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Edward Sze-Tyan Wang. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)reverse.c 5.7 (Berkeley) 06/24/92"; 13 #endif /* not lint */ 14 15 #include <sys/param.h> 16 #include <sys/stat.h> 17 #include <sys/mman.h> 18 19 #include <limits.h> 20 #include <errno.h> 21 #include <unistd.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include "extern.h" 26 27 static void r_buf __P((FILE *)); 28 static void r_reg __P((FILE *, enum STYLE, long, struct stat *)); 29 30 /* 31 * reverse -- display input in reverse order by line. 32 * 33 * There are six separate cases for this -- regular and non-regular 34 * files by bytes, lines or the whole file. 35 * 36 * BYTES display N bytes 37 * REG mmap the file and display the lines 38 * NOREG cyclically read characters into a wrap-around buffer 39 * 40 * LINES display N lines 41 * REG mmap the file and display the lines 42 * NOREG cyclically read lines into a wrap-around array of buffers 43 * 44 * FILE display the entire file 45 * REG mmap the file and display the lines 46 * NOREG cyclically read input into a linked list of buffers 47 */ 48 void 49 reverse(fp, style, off, sbp) 50 FILE *fp; 51 enum STYLE style; 52 long off; 53 struct stat *sbp; 54 { 55 if (style != REVERSE && off == 0) 56 return; 57 58 if (S_ISREG(sbp->st_mode)) 59 r_reg(fp, style, off, sbp); 60 else 61 switch(style) { 62 case FBYTES: 63 case RBYTES: 64 bytes(fp, off); 65 break; 66 case FLINES: 67 case RLINES: 68 lines(fp, off); 69 break; 70 case REVERSE: 71 r_buf(fp); 72 break; 73 } 74 } 75 76 /* 77 * r_reg -- display a regular file in reverse order by line. 78 */ 79 static void 80 r_reg(fp, style, off, sbp) 81 FILE *fp; 82 register enum STYLE style; 83 long off; 84 struct stat *sbp; 85 { 86 register off_t size; 87 register int llen; 88 register char *p; 89 90 if (!(size = sbp->st_size)) 91 return; 92 93 if (size > SIZE_T_MAX) { 94 err(0, "%s: %s", fname, strerror(EFBIG)); 95 return; 96 } 97 98 if ((p = mmap(NULL, (size_t)size, 99 PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) { 100 err(0, "%s: %s", fname, strerror(EFBIG)); 101 return; 102 } 103 p += size - 1; 104 105 if (style == RBYTES && off < size) 106 size = off; 107 108 /* Last char is special, ignore whether newline or not. */ 109 for (llen = 1; --size; ++llen) 110 if (*--p == '\n') { 111 WR(p + 1, llen); 112 llen = 0; 113 if (style == RLINES && !--off) { 114 ++p; 115 break; 116 } 117 } 118 if (llen) 119 WR(p, llen); 120 } 121 122 typedef struct bf { 123 struct bf *next; 124 struct bf *prev; 125 int len; 126 char *l; 127 } BF; 128 129 /* 130 * r_buf -- display a non-regular file in reverse order by line. 131 * 132 * This is the function that saves the entire input, storing the data in a 133 * doubly linked list of buffers and then displays them in reverse order. 134 * It has the usual nastiness of trying to find the newlines, as there's no 135 * guarantee that a newline occurs anywhere in the file, let alone in any 136 * particular buffer. If we run out of memory, input is discarded (and the 137 * user warned). 138 */ 139 static void 140 r_buf(fp) 141 FILE *fp; 142 { 143 register BF *mark, *tl, *tr; 144 register int ch, len, llen; 145 register char *p; 146 off_t enomem; 147 148 #define BSZ (128 * 1024) 149 for (mark = NULL, enomem = 0;;) { 150 /* 151 * Allocate a new block and link it into place in a doubly 152 * linked list. If out of memory, toss the LRU block and 153 * keep going. 154 */ 155 if (enomem || (tl = malloc(sizeof(BF))) == NULL || 156 (tl->l = malloc(BSZ)) == NULL) { 157 if (!mark) 158 err(1, "%s", strerror(errno)); 159 tl = enomem ? tl->next : mark; 160 enomem += tl->len; 161 } else if (mark) { 162 tl->next = mark; 163 tl->prev = mark->prev; 164 mark->prev->next = tl; 165 mark->prev = tl; 166 } else 167 mark->next = mark->prev = (mark = tl); 168 169 /* Fill the block with input data. */ 170 for (p = tl->l, len = 0; 171 len < BSZ && (ch = getc(fp)) != EOF; ++len) 172 *p++ = ch; 173 174 /* 175 * If no input data for this block and we tossed some data, 176 * recover it. 177 */ 178 if (!len) { 179 if (enomem) 180 enomem -= tl->len; 181 tl = tl->prev; 182 break; 183 } 184 185 tl->len = len; 186 if (ch == EOF) 187 break; 188 } 189 190 if (enomem) { 191 (void)fprintf(stderr, 192 "tail: warning: %ld bytes discarded\n", enomem); 193 rval = 1; 194 } 195 196 /* 197 * Step through the blocks in the reverse order read. The last char 198 * is special, ignore whether newline or not. 199 */ 200 for (mark = tl;;) { 201 for (p = tl->l + (len = tl->len) - 1, llen = 0; len--; 202 --p, ++llen) 203 if (*p == '\n') { 204 if (llen) { 205 WR(p + 1, llen); 206 llen = 0; 207 } 208 if (tl == mark) 209 continue; 210 for (tr = tl->next; tr->len; tr = tr->next) { 211 WR(tr->l, tr->len); 212 tr->len = 0; 213 if (tr == mark) 214 break; 215 } 216 } 217 tl->len = llen; 218 if ((tl = tl->prev) == mark) 219 break; 220 } 221 tl = tl->next; 222 if (tl->len) { 223 WR(tl->l, tl->len); 224 tl->len = 0; 225 } 226 while ((tl = tl->next)->len) { 227 WR(tl->l, tl->len); 228 tl->len = 0; 229 } 230 } 231