96b077
Kludge poptBadOption() to return something semi-meaningful on exec alias fail
96b077
96b077
- poptBadOption() is totally unaware of exec alias failures, and
96b077
  will return either the first argument or last option, giving
96b077
  wonderfully misleading error messages (#697435, #710267).
96b077
- Remember execvp() first argument on failure and return that
96b077
  from poptBadOption() if present to give the user a reasonable
96b077
  clue what exactly went wrong.
96b077
96b077
diff -up popt-1.13/popt.c.execfail popt-1.13/popt.c
96b077
--- popt-1.13/popt.c.execfail	2012-08-02 16:08:34.762315304 +0300
96b077
+++ popt-1.13/popt.c	2012-08-02 16:11:41.352683721 +0300
96b077
@@ -186,6 +186,7 @@ poptContext poptGetContext(const char *
96b077
     con->flags = flags;
96b077
     con->execs = NULL;
96b077
     con->numExecs = 0;
96b077
+    con->execFail = NULL;
96b077
     con->finalArgvAlloced = argc * 2;
96b077
     con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
96b077
     con->execAbsolute = 1;
96b077
@@ -234,6 +235,7 @@ void poptResetContext(poptContext con)
96b077
     con->nextLeftover = 0;
96b077
     con->restLeftover = 0;
96b077
     con->doExec = NULL;
96b077
+    con->execFail = _free(con->execFail);
96b077
 
96b077
     if (con->finalArgv != NULL)
96b077
     for (i = 0; i < con->finalArgvCount; i++) {
96b077
@@ -468,6 +470,7 @@ if (_popt_debug)
96b077
 /*@-nullstate@*/
96b077
     rc = execvp(argv[0], (char *const *)argv);
96b077
 /*@=nullstate@*/
96b077
+    con->execFail = xstrdup(argv[0]);
96b077
 
96b077
 exit:
96b077
     if (argv) {
96b077
@@ -1194,11 +1197,19 @@ int poptAddItem(poptContext con, poptIte
96b077
 const char * poptBadOption(poptContext con, unsigned int flags)
96b077
 {
96b077
     struct optionStackEntry * os = NULL;
96b077
+    const char *badOpt = NULL;
96b077
 
96b077
-    if (con != NULL)
96b077
-	os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
96b077
+    if (con != NULL) {
96b077
+	/* Stupid hack to return something semi-meaningful from exec failure */
96b077
+	if (con->execFail) {
96b077
+	    badOpt = con->execFail;
96b077
+	} else {
96b077
+	    os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
96b077
+	    badOpt = os->argv[os->next - 1];
96b077
+	}
96b077
+    }
96b077
 
96b077
-    return (os != NULL && os->argv != NULL ? os->argv[os->next - 1] : NULL);
96b077
+    return badOpt;
96b077
 }
96b077
 
96b077
 const char * poptStrerror(const int error)
96b077
diff -up popt-1.13/poptint.h.execfail popt-1.13/poptint.h
96b077
--- popt-1.13/poptint.h.execfail	2012-08-02 16:08:34.000000000 +0300
96b077
+++ popt-1.13/poptint.h	2012-08-02 16:12:35.796500122 +0300
96b077
@@ -78,6 +78,7 @@ struct poptContext_s {
96b077
 /*@owned@*/ /*@null@*/
96b077
     poptItem execs;
96b077
     int numExecs;
96b077
+    char * execFail;
96b077
 /*@only@*/ /*@null@*/
96b077
     const char ** finalArgv;
96b077
     int finalArgvCount;