Home | History | Annotate | Line # | Download | only in tail
forward.c revision 1.12
      1  1.12  mycroft /*	$NetBSD: forward.c,v 1.12 1998/02/20 07:34:59 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.8    lukem #include <sys/cdefs.h>
     40   1.1    glass #ifndef lint
     41   1.6      jtc #if 0
     42   1.6      jtc static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
     43   1.6      jtc #endif
     44  1.12  mycroft __RCSID("$NetBSD: forward.c,v 1.12 1998/02/20 07:34:59 mycroft Exp $");
     45   1.1    glass #endif /* not lint */
     46   1.1    glass 
     47   1.1    glass #include <sys/types.h>
     48   1.1    glass #include <sys/stat.h>
     49   1.1    glass #include <sys/time.h>
     50   1.1    glass #include <sys/mman.h>
     51   1.6      jtc 
     52   1.6      jtc #include <limits.h>
     53   1.1    glass #include <fcntl.h>
     54   1.1    glass #include <errno.h>
     55   1.1    glass #include <unistd.h>
     56   1.1    glass #include <stdio.h>
     57   1.1    glass #include <stdlib.h>
     58   1.1    glass #include <string.h>
     59   1.1    glass #include "extern.h"
     60   1.1    glass 
     61   1.1    glass static void rlines __P((FILE *, long, struct stat *));
     62   1.1    glass 
     63   1.1    glass /*
     64   1.1    glass  * forward -- display the file, from an offset, forward.
     65   1.1    glass  *
     66   1.1    glass  * There are eight separate cases for this -- regular and non-regular
     67   1.1    glass  * files, by bytes or lines and from the beginning or end of the file.
     68   1.1    glass  *
     69   1.1    glass  * FBYTES	byte offset from the beginning of the file
     70   1.1    glass  *	REG	seek
     71   1.1    glass  *	NOREG	read, counting bytes
     72   1.1    glass  *
     73   1.1    glass  * FLINES	line offset from the beginning of the file
     74   1.1    glass  *	REG	read, counting lines
     75   1.1    glass  *	NOREG	read, counting lines
     76   1.1    glass  *
     77   1.1    glass  * RBYTES	byte offset from the end of the file
     78   1.1    glass  *	REG	seek
     79   1.1    glass  *	NOREG	cyclically read characters into a wrap-around buffer
     80   1.1    glass  *
     81   1.1    glass  * RLINES
     82   1.1    glass  *	REG	mmap the file and step back until reach the correct offset.
     83   1.1    glass  *	NOREG	cyclically read lines into a wrap-around array of buffers
     84   1.1    glass  */
     85   1.1    glass void
     86   1.1    glass forward(fp, style, off, sbp)
     87   1.1    glass 	FILE *fp;
     88   1.1    glass 	enum STYLE style;
     89   1.1    glass 	long off;
     90   1.1    glass 	struct stat *sbp;
     91   1.1    glass {
     92   1.8    lukem 	int ch;
     93   1.1    glass 	struct timeval second;
     94   1.9      cjs 	int dostat = 0;
     95   1.9      cjs 	struct stat statbuf;
     96   1.9      cjs 	off_t lastsize = 0;
     97   1.9      cjs 	dev_t lastdev;
     98   1.9      cjs 	ino_t lastino;
     99   1.9      cjs 
    100   1.9      cjs 	/* Keep track of file's previous incarnation. */
    101   1.9      cjs 	lastdev = sbp->st_dev;
    102   1.9      cjs 	lastino = sbp->st_ino;
    103   1.1    glass 
    104   1.1    glass 	switch(style) {
    105   1.1    glass 	case FBYTES:
    106   1.1    glass 		if (off == 0)
    107   1.1    glass 			break;
    108   1.1    glass 		if (S_ISREG(sbp->st_mode)) {
    109   1.1    glass 			if (sbp->st_size < off)
    110   1.1    glass 				off = sbp->st_size;
    111   1.6      jtc 			if (fseek(fp, off, SEEK_SET) == -1) {
    112   1.1    glass 				ierr();
    113   1.6      jtc 				return;
    114   1.6      jtc 			}
    115   1.1    glass 		} else while (off--)
    116   1.1    glass 			if ((ch = getc(fp)) == EOF) {
    117   1.6      jtc 				if (ferror(fp)) {
    118   1.1    glass 					ierr();
    119   1.6      jtc 					return;
    120   1.1    glass 				}
    121   1.6      jtc 				break;
    122   1.6      jtc 			}
    123   1.1    glass 		break;
    124   1.1    glass 	case FLINES:
    125   1.1    glass 		if (off == 0)
    126   1.1    glass 			break;
    127   1.1    glass 		for (;;) {
    128   1.1    glass 			if ((ch = getc(fp)) == EOF) {
    129   1.6      jtc 				if (ferror(fp)) {
    130   1.1    glass 					ierr();
    131   1.6      jtc 					return;
    132   1.6      jtc 				}
    133   1.1    glass 				break;
    134   1.1    glass 			}
    135   1.1    glass 			if (ch == '\n' && !--off)
    136   1.1    glass 				break;
    137   1.1    glass 		}
    138   1.1    glass 		break;
    139   1.1    glass 	case RBYTES:
    140   1.1    glass 		if (S_ISREG(sbp->st_mode)) {
    141   1.1    glass 			if (sbp->st_size >= off &&
    142   1.6      jtc 			    fseek(fp, -off, SEEK_END) == -1) {
    143   1.1    glass 				ierr();
    144   1.6      jtc 				return;
    145   1.6      jtc 			}
    146   1.1    glass 		} else if (off == 0) {
    147   1.1    glass 			while (getc(fp) != EOF);
    148   1.6      jtc 			if (ferror(fp)) {
    149   1.1    glass 				ierr();
    150   1.6      jtc 				return;
    151   1.6      jtc 			}
    152   1.1    glass 		} else
    153   1.1    glass 			bytes(fp, off);
    154   1.1    glass 		break;
    155   1.1    glass 	case RLINES:
    156   1.1    glass 		if (S_ISREG(sbp->st_mode))
    157   1.1    glass 			if (!off) {
    158   1.6      jtc 				if (fseek(fp, 0L, SEEK_END) == -1) {
    159   1.1    glass 					ierr();
    160   1.6      jtc 					return;
    161   1.6      jtc 				}
    162   1.1    glass 			} else
    163   1.1    glass 				rlines(fp, off, sbp);
    164   1.1    glass 		else if (off == 0) {
    165   1.1    glass 			while (getc(fp) != EOF);
    166   1.6      jtc 			if (ferror(fp)) {
    167   1.1    glass 				ierr();
    168   1.6      jtc 				return;
    169   1.6      jtc 			}
    170   1.1    glass 		} else
    171   1.1    glass 			lines(fp, off);
    172   1.1    glass 		break;
    173   1.8    lukem 	default:
    174   1.8    lukem 		break;
    175   1.1    glass 	}
    176   1.1    glass 
    177   1.1    glass 	for (;;) {
    178   1.9      cjs 		while ((ch = getc(fp)) != EOF)  {
    179   1.9      cjs 			lastsize++;	/* track size changes between stats */
    180   1.1    glass 			if (putchar(ch) == EOF)
    181   1.1    glass 				oerr();
    182   1.9      cjs 		}
    183   1.6      jtc 		if (ferror(fp)) {
    184   1.1    glass 			ierr();
    185   1.6      jtc 			return;
    186   1.6      jtc 		}
    187   1.1    glass 		(void)fflush(stdout);
    188   1.1    glass 		if (!fflag)
    189   1.1    glass 			break;
    190   1.7  ghudson 		/*
    191   1.7  ghudson 		 * We pause for one second after displaying any data that has
    192   1.7  ghudson 		 * accumulated since we read the file.  Since sleep(3) takes
    193   1.7  ghudson 		 * eight system calls, use select() instead.
    194   1.7  ghudson 		 */
    195   1.7  ghudson 		second.tv_sec = 1;
    196   1.7  ghudson 		second.tv_usec = 0;
    197   1.7  ghudson 		if (select(0, NULL, NULL, NULL, &second) == -1)
    198   1.6      jtc 			err(1, "select: %s", strerror(errno));
    199   1.1    glass 		clearerr(fp);
    200   1.9      cjs 
    201   1.9      cjs 		if (fflag == 1)
    202   1.9      cjs 			continue;
    203   1.9      cjs 		/*
    204   1.9      cjs 		 * We restat the original filename every five seconds. If
    205   1.9      cjs 		 * the size is ever smaller than the last time we read it,
    206   1.9      cjs 		 * the file has probably been truncated; if the inode or
    207   1.9      cjs 		 * or device number are different, it has been rotated.
    208   1.9      cjs 		 * This causes us to close it, reopen it, and continue
    209   1.9      cjs 		 * the tail -f. If stat returns an error (say, because
    210   1.9      cjs 		 * the file has been removed), just continue with what
    211   1.9      cjs 		 * we've got open now.
    212   1.9      cjs 		 */
    213   1.9      cjs 		if (dostat > 0)  {
    214   1.9      cjs 			dostat -= 1;
    215   1.9      cjs 		} else {
    216   1.9      cjs 			dostat = 5;
    217   1.9      cjs 			if (stat(fname, &statbuf) == 0)  {
    218   1.9      cjs 				if (statbuf.st_dev != lastdev ||
    219   1.9      cjs 				    statbuf.st_ino != lastino ||
    220   1.9      cjs 				    statbuf.st_size < lastsize)  {
    221   1.9      cjs 					lastdev = statbuf.st_dev;
    222   1.9      cjs 					lastino = statbuf.st_ino;
    223   1.9      cjs 					lastsize = 0;
    224   1.9      cjs 					fclose(fp);
    225   1.9      cjs 					if ((fp = fopen(fname, "r")) == NULL)
    226   1.9      cjs 						err(1, "can't reopen %s: %s",
    227   1.9      cjs 						    fname, strerror(errno));
    228   1.9      cjs 				} else {
    229   1.9      cjs 					lastsize = statbuf.st_size;
    230   1.9      cjs 				}
    231   1.9      cjs 			}
    232   1.9      cjs 		}
    233   1.1    glass 	}
    234   1.1    glass }
    235   1.1    glass 
    236   1.1    glass /*
    237   1.1    glass  * rlines -- display the last offset lines of the file.
    238   1.1    glass  */
    239   1.1    glass static void
    240   1.1    glass rlines(fp, off, sbp)
    241   1.1    glass 	FILE *fp;
    242   1.1    glass 	long off;
    243   1.1    glass 	struct stat *sbp;
    244   1.1    glass {
    245   1.8    lukem 	off_t size;
    246   1.8    lukem 	char *p;
    247   1.5      jtc 	char *start;
    248   1.1    glass 
    249   1.1    glass 	if (!(size = sbp->st_size))
    250   1.1    glass 		return;
    251   1.1    glass 
    252   1.6      jtc 	if (size > SIZE_T_MAX) {
    253   1.6      jtc 		err(0, "%s: %s", fname, strerror(EFBIG));
    254   1.6      jtc 		return;
    255   1.6      jtc 	}
    256   1.6      jtc 
    257  1.12  mycroft 	if ((start = mmap(NULL, (size_t)size, PROT_READ,
    258  1.12  mycroft 	    MAP_FILE|MAP_SHARED, fileno(fp), (off_t)0)) == (caddr_t)-1) {
    259   1.6      jtc 		err(0, "%s: %s", fname, strerror(EFBIG));
    260   1.6      jtc 		return;
    261   1.6      jtc 	}
    262   1.1    glass 
    263   1.1    glass 	/* Last char is special, ignore whether newline or not. */
    264   1.5      jtc 	for (p = start + size - 1; --size;)
    265   1.1    glass 		if (*--p == '\n' && !--off) {
    266   1.1    glass 			++p;
    267   1.1    glass 			break;
    268   1.1    glass 		}
    269   1.1    glass 
    270   1.1    glass 	/* Set the file pointer to reflect the length displayed. */
    271   1.1    glass 	size = sbp->st_size - size;
    272   1.1    glass 	WR(p, size);
    273   1.6      jtc 	if (fseek(fp, (long)sbp->st_size, SEEK_SET) == -1) {
    274   1.1    glass 		ierr();
    275   1.6      jtc 		return;
    276   1.6      jtc 	}
    277   1.5      jtc 	if (munmap(start, (size_t)sbp->st_size)) {
    278   1.6      jtc 		err(0, "%s: %s", fname, strerror(errno));
    279   1.6      jtc 		return;
    280   1.5      jtc 	}
    281   1.1    glass }
    282