This was TODO item from commit 947a7c9c. The patch also introduces version and help switches. Signed-off-by: Sami Kerola <kerolasa@xxxxxx> --- text-utils/Makefile.am | 2 + text-utils/tailf.c | 86 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/text-utils/Makefile.am b/text-utils/Makefile.am index 5a098b9..513f1c3 100644 --- a/text-utils/Makefile.am +++ b/text-utils/Makefile.am @@ -7,6 +7,8 @@ usrbin_exec_PROGRAMS = col colcrt colrm column hexdump rev line tailf hexdump_SOURCES = hexdump.c conv.c display.c hexsyntax.c parse.c \ hexdump.h $(top_srcdir)/lib/strutils.c +tailf_SOURCES = tailf.c $(top_srcdir)/lib/strutils.c + dist_man_MANS = col.1 colcrt.1 colrm.1 column.1 hexdump.1 rev.1 line.1 tailf.1 if HAVE_NCURSES diff --git a/text-utils/tailf.c b/text-utils/tailf.c index c995d97..1f911db 100644 --- a/text-utils/tailf.c +++ b/text-utils/tailf.c @@ -35,6 +35,7 @@ #include <fcntl.h> #include <ctype.h> #include <errno.h> +#include <getopt.h> #ifdef HAVE_INOTIFY_INIT #include <sys/inotify.h> #endif @@ -42,6 +43,7 @@ #include "nls.h" #include "xalloc.h" #include "usleep.h" +#include "strutils.h" #include "c.h" #define DEFAULT_LINES 10 @@ -188,10 +190,43 @@ watch_file_inotify(const char *filename, off_t *size) #endif /* HAVE_INOTIFY_INIT */ +static void __attribute__ ((__noreturn__)) usage(FILE *out) +{ + fprintf(out, + _("\nUsage:\n" + " %s [option] file\n"), + program_invocation_short_name); + + fprintf(out, _( + "\nOptions:\n" + " -n, --lines NUMBER output the last N lines\n" + " -NUMBER same as -n NUMBER\n" + " -V, --version output version information and exit\n" + " -h, --help display this help and exit\n\n")); + + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); +} + +long old_style_option(int argc, char **argv) +{ + int i, j; + long lines = -1; + for (i = j = 0; i < argc; i++) { + if (argv[i][0] == '-' && isdigit(argv[i][1])) { + lines = strtol_or_err(++argv[i], _("invalid number of lines")); + j++; + } + if (i + j < argc) + argv[i] = argv[i+j]; + } + return lines; +} + int main(int argc, char **argv) { const char *filename; - int lines = DEFAULT_LINES; + long lines; + int ch; struct stat st; off_t size = 0; @@ -199,27 +234,40 @@ int main(int argc, char **argv) bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - 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")); + struct option longopts[] = { + { "lines", required_argument, 0, 'n' }, + { "version", no_argument, 0, 'V' }, + { "help", no_argument, 0, 'h' }, + { NULL, 0, 0, 0 } + }; + + lines = old_style_option(argc, argv); + if (lines < 0) + lines = DEFAULT_LINES; + + while ((ch = getopt_long(argc, argv, "n:N:Vh0123456789", longopts, NULL)) != -1) + switch((char)ch) { + case 'n': + case 'N': + lines = strtol_or_err(optarg, _("invalid number of lines")); + break; + case 'V': + printf(_("%s from %s\n"), program_invocation_short_name, + PACKAGE_STRING); + exit(EXIT_SUCCESS); + case 'h': + usage(stdout); + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + errx(EXIT_FAILURE, _("option used in invalid context -- %c"), ch); + default: + usage(stderr); } - 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) - errx(EXIT_FAILURE, _("usage: tailf [-n N | -N] logfile")); + if (argc == optind) + errx(EXIT_FAILURE, _("no input file specified")); - filename = argv[0]; + filename = argv[optind]; if (stat(filename, &st) != 0) err(EXIT_FAILURE, _("cannot stat \"%s\""), filename); -- 1.7.4.1 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html