From 1dacc4b5554f794f8219356a0d2bd2dc6a499160 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Mon, 3 Nov 2014 18:57:20 -0800 Subject: Print which option was in error along with usage message In the process, try to make the code a little less painful to read. Still, no love to whomever came up with (*++*a) style coding. Signed-off-by: Alan Coopersmith --- configure.ac | 2 +- sessreg.c | 67 ++++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/configure.ac b/configure.ac index 3801170..828d14f 100644 --- a/configure.ac +++ b/configure.ac @@ -67,7 +67,7 @@ AC_CHECK_TYPES([ AC_CHECK_FUNCS([pututline updwtmpx utmpxname]) # Obtain compiler/linker options for depedencies -PKG_CHECK_MODULES(SESSREG, xproto) +PKG_CHECK_MODULES(SESSREG, [xproto >= 7.0.25]) AC_CONFIG_FILES([ Makefile diff --git a/sessreg.c b/sessreg.c index bdfe8f0..9e0354a 100644 --- a/sessreg.c +++ b/sessreg.c @@ -74,6 +74,7 @@ #include #include +#include #include #include #include @@ -126,34 +127,45 @@ static int Xslot (char *ttys_file, char *servers_file, char *tty_line, char *host_name, int addp); #endif -static int +static void _X_NORETURN _X_COLD usage (int x) { - if (x) { - fprintf (stderr, - "%s: usage %s {-a -d} [-w wtmp-file] [-u utmp-file]" + fprintf (stderr, + "%s: usage %s {-a -d} [-w wtmp-file] [-u utmp-file]" #ifdef USE_LASTLOG - " [-L lastlog-file]" + " [-L lastlog-file]" #endif - "\n" - " [-t ttys-file] [-l line-name] [-h host-name] [-V]\n" - " [-s slot-number] [-x servers-file] user-name\n", - program_name, program_name); - exit (1); - } - return x; + "\n" + " [-t ttys-file] [-l line-name] [-h host-name] [-V]\n" + " [-s slot-number] [-x servers-file] user-name\n", + program_name, program_name); + exit (x); } static char * getstring (char ***avp, int *flagp) { char **a = *avp; + char *flag = *a; - usage ((*flagp)++); + if (*flagp != 0) { + fprintf (stderr, "%s: cannot give more than one -%s option\n", + program_name, flag); + usage (1); + } + *flagp = 1; + /* if the argument is given immediately following the flag, + i.e. "sessreg -hfoo ...", not "sessreg -h foo ...", + then return the rest of the string as the argument value */ if (*++*a) return *a; + /* else use the next pointer in the argv list as the argument value */ ++a; - usage (!*a); + if (!*a) { + fprintf (stderr, "%s: -%s requires an argument\n", + program_name, flag); + usage (1); + } *avp = a; return *a; } @@ -245,17 +257,36 @@ main (int argc, char **argv) printf("%s\n", PACKAGE_STRING); exit (0); default: + fprintf (stderr, "%s: unrecognized option '%s'\n", + program_name, argv[0]); usage (1); } } - usage (!(user_name = *argv++)); - usage (*argv != NULL); + user_name = *argv++; + if (user_name == NULL) { + fprintf (stderr, "%s: missing required user-name argument\n", + program_name); + usage (1); + } + if (*argv != NULL) { + fprintf (stderr, "%s: unrecognized argument '%s'\n", + program_name, argv[0]); + usage (1); + } /* * complain if neither aflag nor dflag are set, * or if both are set. */ - usage (!(aflag ^ dflag)); - usage (xflag && !lflag); + if (!(aflag ^ dflag)) { + fprintf (stderr, "%s: must specify exactly one of -a or -d\n", + program_name); + usage (1); + } + if (xflag && !lflag) { + fprintf (stderr, "%s: must specify -l when -x is used\n", + program_name); + usage (1); + } /* set up default file names */ if (!wflag) { wtmp_file = WTMP_FILE; -- cgit v1.2.3