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 --- sessreg.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 18 deletions(-) (limited to 'sessreg.c') 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