[PATCH 1/3] libbb: GETOPT_RESET macro
Denys Vlasenko
vda.linux at googlemail.com
Wed Apr 12 18:14:07 UTC 2017
On Tue, Apr 11, 2017 at 11:58 PM, Kaarle Ritvanen
<kaarle.ritvanen at datakunkku.fi> wrote:
> Signed-off-by: Kaarle Ritvanen <kaarle.ritvanen at datakunkku.fi>
> ---
> include/libbb.h | 19 +++++++++++++++++++
> libbb/getopt32.c | 8 +-------
> libbb/vfork_daemon_rexec.c | 28 ++--------------------------
> runit/sv.c | 7 +------
> shell/shell_common.c | 8 +-------
> util-linux/getopt.c | 7 +------
> 6 files changed, 25 insertions(+), 52 deletions(-)
>
> diff --git a/include/libbb.h b/include/libbb.h
> index 0407163..eb78619 100644
> --- a/include/libbb.h
> +++ b/include/libbb.h
> @@ -1178,6 +1178,25 @@ extern uint32_t option_mask32;
> extern uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC;
>
>
> +/* BSD-derived getopt() functions require that optind be set to 1 in
> + * order to reset getopt() state. This used to be generally accepted
> + * way of resetting getopt(). However, glibc's getopt()
> + * has additional getopt() state beyond optind, and requires that
> + * optind be set to zero to reset its state. So the unfortunate state of
> + * affairs is that BSD-derived versions of getopt() misbehave if
> + * optind is set to 0 in order to reset getopt(), and glibc's getopt()
> + * will core dump if optind is set 1 in order to reset getopt().
> + *
> + * More modern versions of BSD require that optreset be set to 1 in
> + * order to reset getopt(). Sigh. Standards, anyone?
> + */
> +#ifdef __GLIBC__
> +#define GETOPT_RESET optind = 0;
> +#else /* BSD style */
> +#define GETOPT_RESET optind = 1;
> +#endif
I think function-style macro is better:
+#ifdef __GLIBC__
+#define GETOPT_RESET() (optind = 0)
+#else /* BSD style */
+#define GETOPT_RESET() (optind = 1)
+#endif
Applied with this edit. Thanks.
More information about the busybox
mailing list