Improve the printing of ==> filename <==. Do not print the same filename
authorMatthew Dillon <dillon@dragonflybsd.org>
Mon, 27 Dec 2004 21:06:39 +0000 (21:06 +0000)
committerMatthew Dillon <dillon@dragonflybsd.org>
Mon, 27 Dec 2004 21:06:39 +0000 (21:06 +0000)
over and over again when running tail -f on multiple files and the next line
output is coming from the same file that the last line output came from.

usr.bin/tail/extern.h
usr.bin/tail/forward.c
usr.bin/tail/tail.c

index ab90648..1f8f508 100644 (file)
@@ -33,7 +33,7 @@
  *     @(#)extern.h    8.1 (Berkeley) 6/6/93
  *
  * $FreeBSD: src/usr.bin/tail/extern.h,v 1.4.6.2 2001/12/19 20:29:29 iedowse Exp $
- * $DragonFly: src/usr.bin/tail/extern.h,v 1.4 2004/12/27 20:55:07 dillon Exp $
+ * $DragonFly: src/usr.bin/tail/extern.h,v 1.5 2004/12/27 21:06:39 dillon Exp $
  */
 
 #define        WR(p, size) do { \
@@ -72,6 +72,7 @@ void ierr(void);
 void oerr(void);
 int mapprint(struct mapinfo *, off_t, off_t);
 int maparound(struct mapinfo *, off_t);
+void showfilename(int index, const char *filename);
 
 extern int Fflag, fflag, rflag, rval, no_files;
 extern char *fname;
index f2da041..fedaa49 100644 (file)
@@ -35,7 +35,7 @@
  *
  * @(#)forward.c       8.1 (Berkeley) 6/6/93
  * $FreeBSD: src/usr.bin/tail/forward.c,v 1.11.6.7 2003/01/07 05:26:22 tjr Exp $
- * $DragonFly: src/usr.bin/tail/forward.c,v 1.4 2004/12/27 20:55:07 dillon Exp $
+ * $DragonFly: src/usr.bin/tail/forward.c,v 1.5 2004/12/27 21:06:39 dillon Exp $
  */
 
 #include <sys/types.h>
@@ -232,14 +232,14 @@ rlines(FILE *fp, off_t off, struct stat *sbp)
  */
 
 static void
-show(file_info_t *file)
+show(file_info_t *file, int index)
 {
        int ch, first;
 
        first = 1;
        while ((ch = getc(file->fp)) != EOF) {
                if (first && no_files > 1) {
-                       printf("\n==> %s <==\n", file->file_name);
+                       showfilename(index, file->file_name);
                        first = 0;
                }
                if (putchar(ch) == EOF)
@@ -254,6 +254,21 @@ show(file_info_t *file)
        }
 }
 
+void
+showfilename(int index, const char *filename)
+{
+       static int last_index = -1;
+       static int continuing = 0;
+
+       if (last_index == index)
+               return;
+       if (continuing)
+               printf("\n");
+       printf("==> %s <==\n", filename);
+       continuing = 1;
+       last_index = index;
+}
+
 static void
 set_events(file_info_t *files)
 {
@@ -304,7 +319,7 @@ follow(file_info_t *files, enum STYLE style, off_t off)
                        active = 1;
                        n++;
                        if (no_files > 1)
-                               printf("\n==> %s <==\n", file->file_name);
+                               showfilename(i, file->file_name);
                        forward(file->fp, style, off, &file->st);
                        if (Fflag && fileno(file->fp) != STDIN_FILENO)
                                n++;
@@ -349,7 +364,7 @@ follow(file_info_t *files, enum STYLE style, off_t off)
                                        }
                                }
                        }
-                       show(file);
+                       show(file, i);
                }
 
                switch (action) {
index 2ac0e57..bc8d017 100644 (file)
@@ -36,7 +36,7 @@
  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
  * @(#)tail.c  8.1 (Berkeley) 6/6/93
  * $FreeBSD: src/usr.bin/tail/tail.c,v 1.6.2.2 2001/12/19 20:29:31 iedowse Exp $
- * $DragonFly: src/usr.bin/tail/tail.c,v 1.4 2004/12/27 20:55:07 dillon Exp $
+ * $DragonFly: src/usr.bin/tail/tail.c,v 1.5 2004/12/27 21:06:39 dillon Exp $
  */
 
 #include <sys/types.h>
@@ -64,7 +64,7 @@ main(int argc, char **argv)
        FILE *fp;
        off_t off;
        enum STYLE style;
-       int i, ch, first;
+       int i, ch;
        file_info_t *file;
        char *p;
 
@@ -181,16 +181,14 @@ main(int argc, char **argv)
                        free(file->file_name);
                free(files);
        } else if (*argv) {
-               for (first = 1; (fname = *argv++) != NULL;) {
+               for (i = 0; (fname = *argv++) != NULL; ++i) {
                        if ((fp = fopen(fname, "r")) == NULL ||
                            fstat(fileno(fp), &sb)) {
                                ierr();
                                continue;
                        }
                        if (argc > 1) {
-                               (void)printf("%s==> %s <==\n",
-                                   first ? "" : "\n", fname);
-                               first = 0;
+                               showfilename(i, fname);
                                (void)fflush(stdout);
                        }