summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2006-09-20 10:24:36 +0200
committerTobias Klauser <tklauser@xenon.tklauser.home>2006-09-20 10:24:36 +0200
commit03d6399d98272e657b5f037c8a8281109bb38507 (patch)
tree7de370113c0f4d9358447b14a860b67fd0734ad3
parent1399781d572b8779ffedd2fe3ed3a35d834fa41d (diff)
Use getopt for command option parsing
-rw-r--r--inotail.112
-rw-r--r--inotail.c54
2 files changed, 36 insertions, 30 deletions
diff --git a/inotail.1 b/inotail.1
index f1516e8..5d4ea48 100644
--- a/inotail.1
+++ b/inotail.1
@@ -26,22 +26,22 @@ will not work on systems running kernels without Inotify. To enable Inotify ,
please set CONFIG_INOTIFY=y in your kernel configuration and recompile it.
.SH OPTIONS
.TP
-.B \-c \fIN\fR
+.B \-c \fIN\fR, \fB\-\-bytes\fR=\fIN\fR
output the last N bytes
.TP
-.B \-f
+.B \-f, \fB\-\-follow
keep the file(s) open and print appended data as the file grows
.TP
-.B \-n \fIN\fR
+.B \-n \fIN\fR, \fB\-\-lines\fR=\fIN\fR
output the last N lines (default: 10)
.TP
-.B \-v
+.B \-v, \fB\-\-verbose
print headers with file names
.TP
-.B \-h
+.B \-h, \fB\-\-help
show help and exit
.TP
-.B \-V
+.B \-V, \fB\-\-version
show inotail version and exit
.SH AUTHOR
.PP
diff --git a/inotail.c b/inotail.c
index 06277f0..7f191c6 100644
--- a/inotail.c
+++ b/inotail.c
@@ -29,6 +29,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
+#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -43,15 +44,25 @@
/* Print header with filename before tailing the file? */
static char verbose = 0;
-static void usage(int status)
+static const struct option long_opts[] = {
+ {"bytes", required_argument, NULL, 'c'},
+ {"follow", optional_argument, NULL, 'f'},
+ {"lines", required_argument, NULL, 'n'},
+ {"verbose", no_argument, NULL, 'v'},
+ {"help", no_argument, NULL, 'h'},
+ {"version", no_argument, NULL, 'V'},
+ {NULL, 0, NULL, 0}
+};
+
+static void usage(const int status)
{
fprintf(stderr, "Usage: %s [OPTION]... [FILE]...\n\n", PROGRAM_NAME);
- fprintf(stderr, " -c N output the last N bytes\n");
- fprintf(stderr, " -f output as the file grows\n");
- fprintf(stderr, " -n N output the last N lines (default: %d)\n", DEFAULT_N_LINES);
- fprintf(stderr, " -v print headers with file names\n");
- fprintf(stderr, " -h show this help and exit\n");
- fprintf(stderr, " -V show version and exit\n");
+ fprintf(stderr, " -c N, --bytes=N output the last N bytes\n");
+ fprintf(stderr, " -f, --follow output as the file grows\n");
+ fprintf(stderr, " -n N, --lines=N output the last N lines (default: %d)\n", DEFAULT_N_LINES);
+ fprintf(stderr, " -v, --verbose print headers with file names\n");
+ fprintf(stderr, " -h, --help show this help and exit\n");
+ fprintf(stderr, " -V, --version show version and exit\n");
exit(status);
}
@@ -130,7 +141,6 @@ static int bytes_to_offset(struct file_struct *f, int n_lines)
/* We will just tail everything here */
static int tail_pipe(struct file_struct *f)
{
- dprintf(" Trying to tail from '%s'\n", f->name);
if (verbose)
write_header(f->name);
return 0;
@@ -290,30 +300,26 @@ static int watch_files(struct file_struct *f, int n_files)
int main(int argc, char **argv)
{
- int i, opt, ret = 0;
+ int i, c, ret = 0;
int n_files = 0;
int n_lines = DEFAULT_N_LINES;
char forever = 0, mode = M_LINES;
char **filenames;
struct file_struct *files;
- for (opt = 1; (opt < argc) && (argv[opt][0] == '-'); opt++) {
- switch (argv[opt][1]) {
+ while ((c = getopt_long(argc, argv, "c:n:fvVh", long_opts, NULL)) != -1) {
+ switch (c) {
case 'c':
- mode = M_BYTES;
- n_lines = strtoul(argv[++opt], NULL, 0);
+ case 'n':
+ if (c == 'c')
+ mode = M_BYTES;
+ n_lines = strtoul(optarg, NULL, 0);
if (n_lines < 0)
n_lines = 0;
break;
case 'f':
forever = 1;
break;
- case 'n':
- mode = M_LINES;
- n_lines = strtoul(argv[++opt], NULL, 0);
- if (n_lines < 0)
- n_lines = 0;
- break;
case 'v':
verbose = 1;
break;
@@ -324,13 +330,13 @@ int main(int argc, char **argv)
usage(EXIT_SUCCESS);
default:
usage(EXIT_FAILURE);
- }
- }
+ }
+ }
/* Do we have some files to read from? */
- if (opt < argc) {
- n_files = argc - opt;
- filenames = argv + opt;
+ if (optind < argc) {
+ n_files = argc - optind;
+ filenames = argv + optind;
} else {
/* It must be stdin then */
static char *dummy_stdin = "-";