From: Sami Kerola Date: Thu, 24 Feb 2011 19:16:40 +0000 (+0100) Subject: logger: support long options X-Git-Tag: v2.20-rc1~484 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b363e86d76340e84f1e21d8bce3727e0d7ff539a;p=thirdparty%2Futil-linux.git 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 Signed-off-by: Karel Zak --- diff --git a/misc-utils/Makefile.am b/misc-utils/Makefile.am index bc33bf4194..237df2bf5d 100644 --- a/misc-utils/Makefile.am +++ b/misc-utils/Makefile.am @@ -10,6 +10,7 @@ usrbin_exec_PROGRAMS = cal ddate logger look mcookie namei whereis EXTRA_DIST += README.cal README.ddate README.namei README.namei2 +logger_SOURCES = logger.c $(top_srcdir)/lib/strutils.c mcookie_SOURCES = mcookie.c $(top_srcdir)/lib/md5.c usrbin_exec_SCRIPTS = chkdupexe diff --git a/misc-utils/logger.c b/misc-utils/logger.c index a61959d6bb..50a9358c1c 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -49,14 +49,17 @@ #include #include #include +#include + +#include "c.h" #include "nls.h" +#include "strutils.h" #define SYSLOG_NAMES #include 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); -}