diff options
-rw-r--r-- | usr.bin/tail/forward.c | 23 | ||||
-rw-r--r-- | usr.bin/tail/tail.1 | 25 |
2 files changed, 36 insertions, 12 deletions
diff --git a/usr.bin/tail/forward.c b/usr.bin/tail/forward.c index 55089c43416..ae48d56791c 100644 --- a/usr.bin/tail/forward.c +++ b/usr.bin/tail/forward.c @@ -1,4 +1,4 @@ -/* $OpenBSD: forward.c,v 1.5 1997/05/29 14:57:31 kstailey Exp $ */ +/* $OpenBSD: forward.c,v 1.6 1997/05/30 19:33:44 millert Exp $ */ /* $NetBSD: forward.c,v 1.7 1996/02/13 16:49:10 ghudson Exp $ */ /*- @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93"; #endif -static char rcsid[] = "$OpenBSD: forward.c,v 1.5 1997/05/29 14:57:31 kstailey Exp $"; +static char rcsid[] = "$OpenBSD: forward.c,v 1.6 1997/05/30 19:33:44 millert Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -91,6 +91,7 @@ forward(fp, style, off, sbp) struct stat *sbp; { register int ch; + struct stat nsb; switch(style) { case FBYTES: @@ -164,7 +165,7 @@ forward(fp, style, off, sbp) } for (;;) { - while ((ch = getc(fp)) != EOF) + while (!feof(fp) && (ch = getc(fp)) != EOF) if (putchar(ch) == EOF) oerr(); if (ferror(fp)) { @@ -176,6 +177,22 @@ forward(fp, style, off, sbp) break; sleep(1); clearerr(fp); + + if (stat(fname, &nsb) != 0) + continue; + /* Reopen file if the inode changes or file was truncated */ + if (nsb.st_ino != sbp->st_ino) { + warnx("%s has been replaced, reopening.", fname); + if ((fp = freopen(fname, "r", fp)) == NULL) { + ierr(); + return; + } + (void)memcpy(sbp, &nsb, sizeof(nsb)); + } else if (nsb.st_size < sbp->st_size) { + warnx("%s has been truncated, resetting.", fname); + rewind(fp); + (void)memcpy(sbp, &nsb, sizeof(nsb)); + } } } diff --git a/usr.bin/tail/tail.1 b/usr.bin/tail/tail.1 index 2c55eaaa386..9a0cc4327c6 100644 --- a/usr.bin/tail/tail.1 +++ b/usr.bin/tail/tail.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tail.1,v 1.2 1996/06/26 05:40:17 deraadt Exp $ +.\" $OpenBSD: tail.1,v 1.3 1997/05/30 19:33:45 millert Exp $ .\" $NetBSD: tail.1,v 1.4 1994/11/23 07:42:13 jtc Exp $ .\" .\" Copyright (c) 1980, 1990, 1991, 1993 @@ -44,7 +44,7 @@ .Nm tail .Nd display the last part of a file .Sh SYNOPSIS -.Nm tail +.Nm .Op Fl f Li | Fl r .Oo .Fl b Ar number | @@ -54,7 +54,7 @@ .Op Ar file ... .Sh DESCRIPTION The -.Nm tail +.Nm utility displays the contents of .Ar file or, by default, its standard input, to the standard output. @@ -88,9 +88,16 @@ bytes. The .Fl f option causes -.Nm tail +.Nm to not stop when end of file is reached, but rather to wait for additional -data to be appended to the input. +data to be appended to the input. If the file is is replaced (ie. the +inode number changes), +.Nm +will reopen the file and continue. If the file is truncated, +.Nm +will reset its position back to the beginning. This makes +.Nm +more useful for watching log files that may get rotated. The .Fl f option is ignored if the standard input is a pipe, but not if it is a FIFO. @@ -126,7 +133,7 @@ where is the name of the file. .Pp The -.Nm tail +.Nm utility exits 0 on success, and >0 if an error occurs. .Sh SEE ALSO .Xr cat 1 , @@ -134,7 +141,7 @@ utility exits 0 on success, and >0 if an error occurs. .Xr sed 1 .Sh STANDARDS The -.Nm tail +.Nm utility is expected to be a superset of the .St -p1003.2-92 specification. @@ -145,7 +152,7 @@ and options are extensions to that standard. .Pp The historic command line syntax of -.Nm tail +.Nm is supported by this implementation. The only difference between this implementation and historic versions of @@ -164,6 +171,6 @@ would ignore the option and display the last 4 lines of the input. .Sh HISTORY A -.Nm tail +.Nm command appeared in .At v7 . |