Skip to contentSkip to conversation

keyctl: Be more friendly about invocation errors

PATCH New Robbie Harwood 1 participant 1 message Oct 15, 2018 Keys/keyrings

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 keyctl: Be more friendly about invocation errors

From 901cc5241e4a66168aed4998077d8fbe39efa866 Mon Sep 17 00:00:00 2001
From: Robbie Harwood <[email protected]>
Date: Mon, 15 Oct 2018 15:57:51 -0400
Subject: [PATCH] keyctl: Be more friendly about invocation errors

When option parsing fails in main(), display usage information.

Also cleanly handle keyctl --help.

---
 keyctl.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
keyctl.c +13 -2
index 61990b4..406f216 100644
@@ -31,6 +31,7 @@ struct command {
 #define nr __attribute__((noreturn))
 
 static nr void act_keyctl___version(int argc, char *argv[]);
+static nr void act_keyctl___help(int argc, char *argv[]);
 static nr void act_keyctl_show(int argc, char *argv[]);
 static nr void act_keyctl_add(int argc, char *argv[]);
 static nr void act_keyctl_padd(int argc, char *argv[]);
@@ -74,6 +75,7 @@ static nr void act_keyctl_restrict_keyring(int argc, char *argv[]);
 
 const struct command commands[] = {
 	{ act_keyctl___version,	"--version",	"" },
+	{ act_keyctl___help,	"--help",	"" },
 	{ act_keyctl_add,	"add",		"<type> <desc> <data> <keyring>" },
 	{ act_keyctl_chgrp,	"chgrp",	"<key> <gid>" },
 	{ act_keyctl_chown,	"chown",	"<key> <uid>" },
@@ -178,7 +180,7 @@ int main(int argc, char *argv[])
 		/* partial match */
 		if (best) {
 			fprintf(stderr, "Ambiguous command\n");
-			exit(2);
+			format();
 		}
 
 		best = cmd;
@@ -186,7 +188,7 @@ int main(int argc, char *argv[])
 
 	if (!best) {
 		fprintf(stderr, "Unknown command\n");
-		exit(2);
+		format();
 	}
 
 	best->action(argc, argv);
@@ -235,6 +237,15 @@ static void act_keyctl___version(int argc, char *argv[])
 	exit(0);
 }
 
+/*****************************************************************************/
+/*
+ * Display help text (wrapper)
+ */
+static void act_keyctl___help(int argc, char *argv[])
+{
+	format();
+}
+
 /*****************************************************************************/
 /*
  * grab data from stdin
Signature · 1 line
2.19.1