It will be useful if we can print out the last n lines instead of the last
10, just like tail.

There are examples:

tailf -n 5 file1
tailf --lines 10 file2
tailf -20 file3

Signed-off-by: Li Zefan <[EMAIL PROTECTED]>
---
 tailf.1 |   14 +++++++++-----
 tailf.c |   48 +++++++++++++++++++++++++++++++++---------------
 2 files changed, 42 insertions(+), 20 deletions(-)

diff --git a/text-utils/tailf.1 b/text-utils/tailf.1
index ccce662..8bc53fc 100644
--- a/text-utils/tailf.1
+++ b/text-utils/tailf.1
@@ -26,7 +26,8 @@
 .SH NAME
 tailf \- follow the growth of a log file
 .SH SYNOPSIS
-.BI tailf " file"
+.B tailf
+[\fIOPTION\fR] \fIfile\fR
 .SH DESCRIPTION
 .B tailf
 will print out the last 10 lines of a file and then wait for the file to
@@ -40,10 +41,13 @@ does not occur periodically when no log activity is 
happening.
 is extremely useful for monitoring log files on a laptop when logging is
 infrequent and the user desires that the hard disk spin down to conserve
 battery life.
-.SH BUGS
-An option could be provided to print out the last
-.I n
-lines instead of the last 10.
+.PP
+Mandatory arguments to long options are mandatory for short options too.
+.TP
+\fB\-n\fR, \fB\-\-lines\fR=\fIN\fR, \fB\-N\fR
+output the last
+.I N
+lines, instead of the last 10.
 .SH AUTHOR
 This program was written by Rik Faith ([EMAIL PROTECTED]) and may be freely
 distributed under the terms of the X11/MIT License.  There is ABSOLUTELY
diff --git a/text-utils/tailf.c b/text-utils/tailf.c
index e10243f..3fbe849 100644
--- a/text-utils/tailf.c
+++ b/text-utils/tailf.c
@@ -31,8 +31,11 @@
 #include <unistd.h>
 #include <malloc.h>
 #include <sys/stat.h>
+#include "errs.h"
 #include "nls.h"
 
+#define DEFAULT_LINES  10
+
 static size_t filesize(const char *filename)
 {
     struct stat sb;
@@ -50,9 +53,7 @@ static void tailf(const char *filename, int lines)
     int  i;
 
     if (!(str = fopen(filename, "r"))) {
-       fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);
-       perror("");
-       exit(1);
+       err(EXIT_FAILURE, "Cannot open %s for read", filename);
     }
 
     buffer = malloc(lines * sizeof(*buffer));
@@ -84,28 +85,45 @@ int main(int argc, char **argv)
     FILE       *str;
     const char *filename;
     int        count;
+    int        lines = DEFAULT_LINES;
 
     setlocale(LC_ALL, "");
     bindtextdomain(PACKAGE, LOCALEDIR);
     textdomain(PACKAGE);
 
-    if (argc != 2) {
-       fprintf(stderr, _("Usage: tailf logfile\n"));
-       exit(1);
-    }
+       argc--;
+       argv++;
 
-    filename = argv[1];
+       for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
+               if (!strcmp(*argv, "-n") || !strcmp(*argv, "--lines")) {
+                       argc--;
+                       argv++;
+                       if (argc > 0 && (lines = atoi(argv[0])) <= 0)
+                               errx(EXIT_FAILURE, _("Invalid number of 
lines"));
+               }
+               else if (isdigit(argv[0][1])) {
+                       if ((lines = atoi(*argv + 1)) <= 0)
+                               errx(EXIT_FAILURE, _("Invalid number of 
lines"));
+               }
+               else
+                       errx(EXIT_FAILURE, _("Invalid option"));
+       }       
+
+       if (argc != 1) {
+               fprintf(stderr, _("Usage: tailf [-n N | -N] logfile\n"));
+               exit(EXIT_FAILURE);
+       }
 
-    tailf(filename, 10);
+    filename = argv[0];
+
+    tailf(filename, lines);
 
     for (osize = filesize(filename);;) {
        nsize = filesize(filename);
        if (nsize != osize) {
-           if (!(str = fopen(filename, "r"))) {
-               fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);
-               perror(argv[0]);
-               exit(1);
-           }
+           if (!(str = fopen(filename, "r")))
+               err(EXIT_FAILURE, "Cannot open %s for read", filename);
+
            if (!fseek(str, osize, SEEK_SET))
                 while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)
                     fwrite(buffer, 1, count, stdout);
@@ -115,5 +133,5 @@ int main(int argc, char **argv)
        }
        usleep(250000);         /* 250mS */
     }
-    return 0;
+    return EXIT_SUCCESS;
 }
---


-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to