.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
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 (faith@acm.org) and may be freely
distributed under the terms of the X11/MIT License. There is ABSOLUTELY
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
+#include <ctype.h>
+#include "errs.h"
#include "nls.h"
+#define DEFAULT_LINES 10
+
static size_t filesize(const char *filename)
{
struct stat sb;
FILE *str;
const char *filename;
int count, wcount;
+ 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++;
+
+ 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"));
}
- filename = argv[1];
+ 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);