[PATCH:sessreg 1/5] Print which option was in error along with usage message

Alan Coopersmith alan.coopersmith at oracle.com
Tue Nov 4 19:13:22 PST 2014


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 <alan.coopersmith at oracle.com>
---
 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 <X11/Xos.h>
 #include <X11/Xfuncs.h>
+#include <X11/Xfuncproto.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
@@ -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;
-- 
1.7.9.2



More information about the xorg-devel mailing list