]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tailf: use long options
authorSami Kerola <kerolasa@iki.fi>
Thu, 24 Feb 2011 21:39:11 +0000 (22:39 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 8 Mar 2011 12:04:09 +0000 (13:04 +0100)
This was TODO item from commit 947a7c9c. The patch also
introduces version and help switches.

[kzak@redhat.com: rewrite old_style_option()]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
text-utils/Makefile.am
text-utils/tailf.c

index 5a098b99df7b0c619cbe0a015876f489488b6b5b..513f1c36739607c66988c26c8ca0965dd3f02e83 100644 (file)
@@ -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
index c995d979cdd9f14cfcbfd8fba31f3220f1cc6e4b..80b48c45cc4db6b0adc6016435821391fd469601 100644 (file)
@@ -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,47 @@ 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);
+}
+
+/* parses -N option */
+long old_style_option(int *argc, char **argv)
+{
+       int i = 1, nargs = *argc;
+       long lines = -1;
+
+       while(i < nargs) {
+               if (argv[i][0] == '-' && isdigit(argv[i][1])) {
+                       lines = strtol_or_err(argv[i] + 1,
+                                       _("failed to parse number of lines"));
+                       nargs--;
+                       memmove(argv + i, argv + i + 1, sizeof(char *) * nargs);
+               } else
+                       i++;
+       }
+       *argc = nargs;
+       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 +238,38 @@ 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"));
+       static const 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:Vh", longopts, NULL)) != -1)
+               switch((char)ch) {
+               case 'n':
+               case 'N':
+                       lines = strtol_or_err(optarg,
+                                       _("failed to parse number of lines"));
+                       break;
+               case 'V':
+                       printf(_("%s from %s\n"), program_invocation_short_name,
+                                                 PACKAGE_STRING);
+                       exit(EXIT_SUCCESS);
+               case 'h':
+                       usage(stdout);
+               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);