Skip to content

Commit

Permalink
halcmd: Unportably reset getopt state
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Epler <[email protected]>
  • Loading branch information
jepler committed Jun 18, 2016
1 parent edfcb03 commit d9b80fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,23 @@ AC_MSG_RESULT([$sincos_ok])
AC_CHECK_FUNCS(__sincos)
LIBS=$LIBS_hold

AC_MSG_CHECKING([for optreset])
AC_TRY_LINK(
[
#include <unistd.h>
extern int optreset;
],
[ optreset = 1; ],
[
HAVE_OPTRESET=yes
AC_DEFINE([HAVE_OPTRESET], [], [Define to 1 if getopt has the BSD 'optreset' extension])
],
[HAVE_OPTRESET=no])
AC_MSG_RESULT($HAVE_OPTRESET)
AC_SUBST([HAVE_OPTRESET])



AH_BOTTOM(#endif
)

Expand Down
25 changes: 24 additions & 1 deletion src/hal/utils/halcmd_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,29 @@ static char *guess_comp_name(char *prog_name)
return name;
}

static void reset_getopt_state() {
/*
It turns out that it is not portable to reset the state of getopt, so that
a different argv list can be parsed.
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=192834
(though that thread ends with the bug being closed as fixed in lenny, it
is not fixed or has regressed by debian jessie)
*/

#ifdef __GNU_LIBRARY__
optind = 0;
#else
optind = 1;
#endif

#ifdef HAVE_OPTRESET
optreset = 1;
#endif
}

int do_loadusr_cmd(char *args[])
{
int wait_flag, wait_comp_flag, ignore_flag;
Expand All @@ -1406,7 +1429,7 @@ int do_loadusr_cmd(char *args[])
prog_name = NULL;

/* check for options (-w, -i, and/or -r) */
optind = 1;
reset_getopt_state();
while (1) {
int c = getopt(argc, args, "+wWin:");
if(c == -1) break;
Expand Down

0 comments on commit d9b80fc

Please sign in to comment.