From 3f422f271f3dc28f7d598d5e0d616ac8aff5409b Mon Sep 17 00:00:00 2001 From: Andreas Rottmann Date: Sun, 26 Jun 2016 03:20:18 +0200 Subject: [PATCH] Fix return code for --help and --version When the `--help' or `--version' command-line options were given, quicktun would exit with exit status 1, although the requested action was fulfilled without encountering an error. With this change, these options now will result in a zero exit status. In addition, with both of these option, the header printed was partially overlapping with the message printed by the option, so the header is now printer after option processing. --- src/common.c | 14 +++++++++++--- src/run.combined.c | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/common.c b/src/common.c index dad5a39..f80a2fe 100755 --- a/src/common.c +++ b/src/common.c @@ -101,6 +101,11 @@ int debug = 0; static int gargc = 0; static char** gargv = NULL; +int successexit(const char* text) { + printf("%s\n", text); + return 0; +} + int errorexit(const char* text) { fprintf(stderr, "%s\n", text); return -1; @@ -416,14 +421,17 @@ static char* getconfcmdargs(const char* name) { return NULL; } +/* A positive return value indicates the caller to proceed. A zero return value means the + program should terminate sucessfully after option processing, while a negative return + value indicates an error during argument processing. */ int qtprocessargs(int argc, char** argv) { int i; for (i = 1; i < argc; i++) { char* a = argv[i]; if (!strcmp(a, "-h") || !strcmp(a, "--help")) { - return errorexit("Please read the documentation at http://wiki.ucis.nl/QuickTun"); + return successexit("Please read the documentation at http://wiki.ucis.nl/QuickTun"); } else if (!strcmp(a, "-v") || !strcmp(a, "--version")) { - return errorexit("UCIS QuickTun "QT_VERSION); + return successexit("UCIS QuickTun "QT_VERSION); } else if (!strcmp(a, "-c")) { gargc = argc; gargv = argv; @@ -433,7 +441,7 @@ int qtprocessargs(int argc, char** argv) { return errorexit("Unexpected command line argument"); } } - return 0; + return 1; } #endif diff --git a/src/run.combined.c b/src/run.combined.c index 934d4bf..8daa868 100755 --- a/src/run.combined.c +++ b/src/run.combined.c @@ -44,13 +44,14 @@ char* getenvdeb(const char* name) { #endif int main(int argc, char** argv) { - print_header(); #ifdef DEBIAN_BINARY getconf = getenvdeb; #else getconf = getenv; #endif - if (qtprocessargs(argc, argv) < 0) return -1; + int rc = qtprocessargs(argc, argv); + if (rc <= 0) return rc; + print_header(); char* envval; if ((envval = getconf("PROTOCOL"))) { if (strcmp(envval, "raw") == 0) {