blob: 9f775351289a99cc2bc007b2747505657b87e92c [file] [log] [blame]
From de6e173c9d2105d581d30066b861e964ea38b2b2 Mon Sep 17 00:00:00 2001
From: Mandeep Singh Baines <msb@chromium.org>
Date: Mon, 4 Mar 2013 16:01:28 -0800
Subject: [PATCH] getopt: makes exits codes consistent with util-linux
bb getopt now works with shflags. Otherwise, you get:
flags:FATAL unable to determine getopt version
shflags still needs to be fixed to properly check whether
busybox getopt has longopt support builtin or not.
Signed-off-by: Mandeep Singh Baines <msb@chromium.org>
---
util-linux/getopt.c | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index 58df1c8..d8203e9 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -106,6 +106,18 @@
#endif
#include "libbb.h"
+/* Exit codes:
+ * 0) No errors, successful operation.
+ * 1) getopt(3) returned an error.
+ * 2) A problem with parameter parsing for getopt(1).
+ * 3) Internal error, out of memory
+ * 4) Returned for -T
+ */
+#define GETOPT_EXIT_CODE 1
+#define PARAMETER_EXIT_CODE 2
+#define XALLOC_EXIT_CODE 3
+#define TEST_EXIT_CODE 4
+
/* NON_OPT is the code that is returned when a non-option is found in '+'
mode */
enum {
@@ -246,7 +258,7 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru
if (opt == -1)
break;
if (opt == '?' || opt == ':' )
- exit_code = 1;
+ exit_code = GETOPT_EXIT_CODE;
else if (!quiet_output) {
#if ENABLE_FEATURE_GETOPT_LONG
if (opt == LONG_OPT) {
@@ -381,7 +393,8 @@ int getopt_main(int argc, char **argv)
printf(" --\n");
return 0;
}
- bb_error_msg_and_die("missing optstring argument");
+ bb_error_msg("missing optstring argument");
+ return PARAMETER_EXIT_CODE;
}
if (argv[1][0] != '-' || compatible) {
@@ -411,7 +424,7 @@ int getopt_main(int argc, char **argv)
}
if (opt & OPT_T) {
- return 4;
+ return TEST_EXIT_CODE;
}
/* All options controlling the applet have now been parsed */
--
1.7.12.4