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 * 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 /*static char sccsid[] = "from: @(#)forward.c 5.4 (Berkeley) 2/12/92";*/ 39 static char rcsid[] = "$Id: forward.c,v 1.4 1994/04/24 20:19:17 deraadt Exp $"; 40 #endif /* not lint */ 41 42 #include <sys/types.h> 43 #include <sys/stat.h> 44 #include <sys/time.h> 45 #include <sys/mman.h> 46 #include <fcntl.h> 47 #include <errno.h> 48 #include <unistd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include "extern.h" 53 54 static void rlines __P((FILE *, long, struct stat *)); 55 56 /* 57 * forward -- display the file, from an offset, forward. 58 * 59 * There are eight separate cases for this -- regular and non-regular 60 * files, by bytes or lines and from the beginning or end of the file. 61 * 62 * FBYTES byte offset from the beginning of the file 63 * REG seek 64 * NOREG read, counting bytes 65 * 66 * FLINES line offset from the beginning of the file 67 * REG read, counting lines 68 * NOREG read, counting lines 69 * 70 * RBYTES byte offset from the end of the file 71 * REG seek 72 * NOREG cyclically read characters into a wrap-around buffer 73 * 74 * RLINES 75 * REG mmap the file and step back until reach the correct offset. 76 * NOREG cyclically read lines into a wrap-around array of buffers 77 */ 78 void 79 forward(fp, style, off, sbp) 80 FILE *fp; 81 enum STYLE style; 82 long off; 83 struct stat *sbp; 84 { 85 register int ch; 86 struct timeval second; 87 fd_set zero; 88 89 switch(style) { 90 case FBYTES: 91 if (off == 0) 92 break; 93 if (S_ISREG(sbp->st_mode)) { 94 if (sbp->st_size < off) 95 off = sbp->st_size; 96 if (fseek(fp, off, SEEK_SET) == -1) 97 ierr(); 98 } else while (off--) 99 if ((ch = getc(fp)) == EOF) { 100 if (ferror(fp)) 101 ierr(); 102 break; 103 } 104 break; 105 case FLINES: 106 if (off == 0) 107 break; 108 for (;;) { 109 if ((ch = getc(fp)) == EOF) { 110 if (ferror(fp)) 111 ierr(); 112 break; 113 } 114 if (ch == '\n' && !--off) 115 break; 116 } 117 break; 118 case RBYTES: 119 if (S_ISREG(sbp->st_mode)) { 120 if (sbp->st_size >= off && 121 fseek(fp, -off, SEEK_END) == -1) 122 ierr(); 123 } else if (off == 0) { 124 while (getc(fp) != EOF); 125 if (ferror(fp)) 126 ierr(); 127 } else 128 bytes(fp, off); 129 break; 130 case RLINES: 131 if (S_ISREG(sbp->st_mode)) 132 if (!off) { 133 if (fseek(fp, 0L, SEEK_END) == -1) 134 ierr(); 135 } else 136 rlines(fp, off, sbp); 137 else if (off == 0) { 138 while (getc(fp) != EOF); 139 if (ferror(fp)) 140 ierr(); 141 } else 142 lines(fp, off); 143 break; 144 } 145 146 /* 147 * We pause for one second after displaying any data that has 148 * accumulated since we read the file. 149 */ 150 if (fflag) { 151 FD_ZERO(&zero); 152 second.tv_sec = 1; 153 second.tv_usec = 0; 154 } 155 156 for (;;) { 157 while ((ch = getc(fp)) != EOF) 158 if (putchar(ch) == EOF) 159 oerr(); 160 if (ferror(fp)) 161 ierr(); 162 (void)fflush(stdout); 163 if (!fflag) 164 break; 165 /* Sleep is eight system calls. Do it fast. */ 166 if (select(0, &zero, &zero, &zero, &second) == -1) 167 err("select: %s", strerror(errno)); 168 clearerr(fp); 169 } 170 } 171 172 /* 173 * rlines -- display the last offset lines of the file. 174 */ 175 static void 176 rlines(fp, off, sbp) 177 FILE *fp; 178 long off; 179 struct stat *sbp; 180 { 181 register int size; 182 register char *p; 183 184 if (!(size = sbp->st_size)) 185 return; 186 187 if ((p = mmap(NULL, 188 size, PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) 189 err("%s", strerror(errno)); 190 191 /* Last char is special, ignore whether newline or not. */ 192 for (p += size - 1; --size;) 193 if (*--p == '\n' && !--off) { 194 ++p; 195 break; 196 } 197 198 /* Set the file pointer to reflect the length displayed. */ 199 size = sbp->st_size - size; 200 WR(p, size); 201 if (fseek(fp, sbp->st_size, SEEK_SET) == -1) 202 ierr(); 203 } 204