[PATCH] Refactor main()

From: FRIGN <dev_AT_frign.de>
Date: Mon, 22 Aug 2016 00:25:21 +0200

- Add arg.h and fix usage
        Given slock is suid we don't want to have half-measures in place to
        parse the arguments in case the code is changed in the future with
        somebody not paying enough attention.

        Also, fix the usage string output to be more consistent across the
        suckless toolbase and make it reflect the manpage entry.

- Comments
        Use proper block comments and add/change them where necessary
        to help in studying the code.

- Error messages
        Consistently prepend them with "slock:" and fix wording and
        do a proper cleanup before quitting (XCloseDisplay and free
        the locks), making the die() semantics consistent with st's.

- getpwuid() error reporting
        Properly present an error message if getpwuid() fails.

- fork() error reporting
        Properly present an error message if fork() fails. If we cannot
        close the connection within the fork context we abort the
        operation and report an error.

- execvp() error handling
        If execvp fails, we cannot call die() afterwards as this implies
        calling exit(). We must use _exit() to prevent the libc from
        doing now "illegal" cleanup-work.
---
 arg.h   | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
 slock.c | 85 +++++++++++++++++++++++++++++++++++++++++--------------------=
----
 2 files changed, 119 insertions(+), 31 deletions(-)
 create mode 100644 arg.h
diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..0b23c53
--- /dev/null
+++ b/arg.h
_AT_@ -0,0 +1,65 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN	for (argv0 =3D *argv, argv++, argc--;\
+					argv[0] && argv[0][0] =3D=3D '-'\
+					&& argv[0][1];\
+					argc--, argv++) {\
+				char argc_;\
+				char **argv_;\
+				int brk_;\
+				if (argv[0][1] =3D=3D '-' && argv[0][2] =3D=3D '\0') {\
+					argv++;\
+					argc--;\
+					break;\
+				}\
+				for (brk_ =3D 0, argv[0]++, argv_ =3D argv;\
+						argv[0][0] && !brk_;\
+						argv[0]++) {\
+					if (argv_ !=3D argv)\
+						break;\
+					argc_ =3D argv[0][0];\
+					switch (argc_)
+
+/* Handles obsolete -NUM syntax */
+#define ARGNUM				case '0':\
+					case '1':\
+					case '2':\
+					case '3':\
+					case '4':\
+					case '5':\
+					case '6':\
+					case '7':\
+					case '8':\
+					case '9'
+
+#define ARGEND			}\
+			}
+
+#define ARGC()		argc_
+
+#define ARGNUMF()	(brk_ =3D 1, estrtonum(argv[0], 0, INT_MAX))
+
+#define EARGF(x)	((argv[0][1] =3D=3D '\0' && argv[1] =3D=3D NULL)?\
+				((x), abort(), (char *)0) :\
+				(brk_ =3D 1, (argv[0][1] !=3D '\0')?\
+					(&argv[0][1]) :\
+					(argc--, argv++, argv[0])))
+
+#define ARGF()		((argv[0][1] =3D=3D '\0' && argv[1] =3D=3D NULL)?\
+				(char *)0 :\
+				(brk_ =3D 1, (argv[0][1] !=3D '\0')?\
+					(&argv[0][1]) :\
+					(argc--, argv++, argv[0])))
+
+#define LNGARG()	&argv[0][0]
+
+#endif
diff --git a/slock.c b/slock.c
index a00fbb9..210d5c8 100644
--- a/slock.c
+++ b/slock.c
_AT_@ -23,8 +23,11 @@
 #include <bsd_auth.h>
 #endif
=20
+#include "arg.h"
 #include "util.h"
=20
+char *argv0;
+
 enum {
 	INIT,
 	INPUT,
_AT_@ -54,7 +57,6 @@ die(const char *errstr, ...)
 {
 	va_list ap;
=20
-	fputs("slock: ", stderr);
 	va_start(ap, errstr);
 	vfprintf(stderr, errstr, ap);
 	va_end(ap);
_AT_@ -280,8 +282,7 @@ lockscreen(Display *dpy, int screen)
 static void
 usage(void)
 {
-	fprintf(stderr, "usage: slock [-v|POST_LOCK_CMD]\n");
-	exit(1);
+	die("usage: slock [-v | cmd [arg ...]]\n");
 }
=20
 int
_AT_@ -290,64 +291,86 @@ main(int argc, char **argv) {
 	const char *pws;
 #endif
 	Display *dpy;
-	int screen;
+	int s, nlocks;
=20
-	if ((argc >=3D 2) && !strcmp("-v", argv[1]))
-		die("version %s, =C2=A9 2006-2016 slock engineers\n", VERSION);
-
-	/* treat first argument starting with a '-' as option */
-	if ((argc >=3D 2) && argv[1][0] =3D=3D '-')
+	ARGBEGIN {
+	case 'v':
+		fprintf(stderr, "slock-"VERSION"\n");
+		return 0;
+	default:
 		usage();
+	} ARGEND
=20
 #ifdef __linux__
 	dontkillme();
 #endif
=20
-	if (!getpwuid(getuid()))
-		die("no passwd entry for you\n");
+	/* Check if the current user has a password entry */
+	errno =3D 0;
+	if (!getpwuid(getuid())) {
+		if (errno =3D=3D 0)
+			die("slock: no password entry for current user\n");
+		else
+			die("slock: getpwuid: %s\n", strerror(errno));
+	}
=20
 #ifndef HAVE_BSD_AUTH
 	pws =3D getpw();
 #endif
=20
-	if (!(dpy =3D XOpenDisplay(0)))
-		die("cannot open display\n");
+	if (!(dpy =3D XOpenDisplay(NULL)))
+		die("slock: cannot open display\n");
+
+	/* check for Xrandr support */
 	rr =3D XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
-	/* Get the number of screens in display "dpy" and blank them all. */
+
+	/* get number of screens in display "dpy" and blank them */
 	nscreens =3D ScreenCount(dpy);
-	if (!(locks =3D malloc(sizeof(Lock*) * nscreens)))
-		die("Out of memory.\n");
-	int nlocks =3D 0;
-	for (screen =3D 0; screen < nscreens; screen++) {
-		if ((locks[screen] =3D lockscreen(dpy, screen)) !=3D NULL)
+	if (!(locks =3D malloc(sizeof(Lock *) * nscreens))) {
+		XCloseDisplay(dpy);
+		die("slock: out of memory\n");
+	}
+	for (nlocks =3D 0, s =3D 0; s < nscreens; s++) {
+		if ((locks[s] =3D lockscreen(dpy, s)) !=3D NULL)
 			nlocks++;
 	}
-	XSync(dpy, False);
+	XSync(dpy, 0);
=20
-	/* Did we actually manage to lock something? */
-	if (nlocks =3D=3D 0) { /* nothing to protect */
+	/* did we actually manage to lock anything? */
+	if (nlocks =3D=3D 0) {
+		/* nothing to protect */
 		free(locks);
 		XCloseDisplay(dpy);
 		return 1;
 	}
=20
-	if (argc >=3D 2 && fork() =3D=3D 0) {
-		if (dpy)
-			close(ConnectionNumber(dpy));
-		execvp(argv[1], argv+1);
-		die("execvp %s failed: %s\n", argv[1], strerror(errno));
+	/* run post-lock command */
+	if (argc > 0) {
+		switch (fork()) {
+		case -1:
+			free(locks);
+			XCloseDisplay(dpy);
+			die("slock: fork failed: %s\n", strerror(errno));
+		case 0:
+			if (close(ConnectionNumber(dpy)) < 0)
+				die("slock: close: %s\n", strerror(errno));
+			execvp(argv[0], argv);
+			fprintf(stderr, "slock: execvp %s: %s\n", argv[0],
+			        strerror(errno));
+			_exit(1);
+		}
 	}
=20
-	/* Everything is now blank. Now wait for the correct password. */
+	/* everything is now blank. Wait for the correct password */
 #ifdef HAVE_BSD_AUTH
 	readpw(dpy);
 #else
 	readpw(dpy, pws);
 #endif
=20
-	/* Password ok, unlock everything and quit. */
-	for (screen =3D 0; screen < nscreens; screen++)
-		unlockscreen(dpy, locks[screen]);
+	/* password ok, unlock everything and quit */
+	for (s =3D 0; s < nscreens; s++)
+		unlockscreen(dpy, locks[s]);
=20
 	free(locks);
 	XCloseDisplay(dpy);
--=20
2.7.3
--Multipart=_Mon__22_Aug_2016_16_54_31_+0200_conXDITABGea0pPs--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 22 2016 - 18:24:13 CEST