diff options
author | Sami Kerola | 2011-02-24 20:16:40 +0100 |
---|---|---|
committer | Karel Zak | 2011-03-08 11:55:28 +0100 |
commit | b363e86d76340e84f1e21d8bce3727e0d7ff539a (patch) | |
tree | 276fbc606bc686b8f4c0e72fb53ed77848f4fa34 /misc-utils/logger.c | |
parent | logger: fix variable type compiler warning (diff) | |
download | kernel-qcow2-util-linux-b363e86d76340e84f1e21d8bce3727e0d7ff539a.tar.gz kernel-qcow2-util-linux-b363e86d76340e84f1e21d8bce3727e0d7ff539a.tar.xz kernel-qcow2-util-linux-b363e86d76340e84f1e21d8bce3727e0d7ff539a.zip |
logger: support long options
Use getopt_long and usage output changed to match long options.
This patch will also scrutiny argument of formerly undocumented
-P option.
[kzak@redhat.com: - include c.h]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'misc-utils/logger.c')
-rw-r--r-- | misc-utils/logger.c | 71 |
1 files changed, 59 insertions, 12 deletions
diff --git a/misc-utils/logger.c b/misc-utils/logger.c index a61959d6b..50a9358c1 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -49,14 +49,17 @@ #include <sys/un.h> #include <arpa/inet.h> #include <netdb.h> +#include <getopt.h> + +#include "c.h" #include "nls.h" +#include "strutils.h" #define SYSLOG_NAMES #include <syslog.h> int decode __P((char *, CODE *)); int pencode __P((char *)); -void usage __P((void)); static int optd = 0; static int udpport = 514; @@ -140,6 +143,30 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) { } } +static void __attribute__ ((__noreturn__)) usage(FILE *out) +{ + fprintf(out, + _("\nUsage:\n" + " %s [options] message\n"), + program_invocation_short_name); + + fprintf(out, _( + "\nOptions:\n" + " -i, --id log process id\n" + " -s, --stderr log message to standard error as well\n" + " -f, --file FILE log contents of the specified file\n" + " -p, --priority PRI enter message priority\n" + " -t, --tag TAG mark every line with tag\n" + " -u, --socket SOCK write to socket\n" + " -d, --udp use udp (tcp is default)\n" + " -n, --server ADDR write to remote syslog server\n" + " -P, --port define port 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); +} + /* * logger -- read and log utility * @@ -153,6 +180,22 @@ main(int argc, char **argv) { char *usock = NULL; char *udpserver = NULL; int LogSock = -1; + long tmpport; + + static const struct option longopts[] = { + { "id", no_argument, 0, 'i' }, + { "stderr", no_argument, 0, 's' }, + { "file", required_argument, 0, 'f' }, + { "priority", required_argument, 0, 'p' }, + { "tag", required_argument, 0, 't' }, + { "socket", required_argument, 0, 'u' }, + { "udp", no_argument, 0, 'd' }, + { "server", required_argument, 0, 'n' }, + { "port", required_argument, 0, 'P' }, + { "version", no_argument, 0, 'V' }, + { "help", no_argument, 0, 'h' }, + { NULL, 0, 0, 0 } + }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -161,7 +204,8 @@ main(int argc, char **argv) { tag = NULL; pri = LOG_NOTICE; logflags = 0; - while ((ch = getopt(argc, argv, "f:ip:st:u:dn:P:")) != -1) + while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh", + longopts, NULL)) != -1) switch((char)ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) { @@ -194,11 +238,22 @@ main(int argc, char **argv) { udpserver = optarg; break; case 'P': /* change udp port */ - udpport = atoi(optarg); + tmpport = strtol_or_err(optarg, + _("failed to parse port number")); + if (tmpport < 0 || 65535 < tmpport) + errx(EXIT_FAILURE, _("port `%ld' out of range"), + tmpport); + udpport = (int) tmpport; break; + case 'V': + printf(_("%s from %s\n"), program_invocation_short_name, + PACKAGE_STRING); + exit(EXIT_SUCCESS); + case 'h': + usage(stdout); case '?': default: - usage(); + usage(stderr); } argc -= optind; argv += optind; @@ -316,11 +371,3 @@ decode(name, codetab) return (-1); } - -void -usage() -{ - (void)fprintf(stderr, - _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u socket] [ message ... ]\n")); - exit(1); -} |