summaryrefslogtreecommitdiff
path: root/lib/getopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/getopt.c')
-rw-r--r--lib/getopt.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/lib/getopt.c b/lib/getopt.c
index e2951f74601..f66f119ec50 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -21,7 +21,7 @@
# include <config.h>
#endif
-#include "getopt.h"
+#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
@@ -223,8 +223,9 @@ process_long_option (int argc, char **argv, const char *optstring,
{
/* Didn't find an exact match, so look for abbreviations. */
unsigned char *ambig_set = NULL;
- int ambig_malloced = 0;
- int ambig_fallback = 0;
+ /* Use simpler fallback diagnostic if ambig_set == &ambig_fallback. */
+ unsigned char ambig_fallback;
+ void *ambig_malloced = NULL;
int indfound = -1;
for (p = longopts, option_index = 0; p->name; p++, option_index++)
@@ -242,39 +243,42 @@ process_long_option (int argc, char **argv, const char *optstring,
|| pfound->val != p->val)
{
/* Second or later nonexact match found. */
- if (!ambig_fallback)
+ if (ambig_set != &ambig_fallback)
{
if (!print_errors)
/* Don't waste effort tracking the ambig set if
we're not going to print it anyway. */
- ambig_fallback = 1;
+ ambig_set = &ambig_fallback;
else if (!ambig_set)
{
if (__libc_use_alloca (n_options))
ambig_set = alloca (n_options);
- else if ((ambig_set = malloc (n_options)) == NULL)
- /* Fall back to simpler error message. */
- ambig_fallback = 1;
else
- ambig_malloced = 1;
+ {
+ ambig_malloced = malloc (n_options);
+ /* Fall back to simpler diagnostic if
+ memory allocation fails. */
+ ambig_set = (ambig_malloced ? ambig_malloced
+ : &ambig_fallback);
+ }
- if (ambig_set)
+ if (ambig_set != &ambig_fallback)
{
memset (ambig_set, 0, n_options);
ambig_set[indfound] = 1;
}
}
- if (ambig_set)
+ if (ambig_set && ambig_set != &ambig_fallback)
ambig_set[option_index] = 1;
}
}
}
- if (ambig_set || ambig_fallback)
+ if (ambig_set)
{
if (print_errors)
{
- if (ambig_fallback)
+ if (ambig_set == &ambig_fallback)
fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
argv[0], prefix, d->__nextchar);
else
@@ -296,8 +300,7 @@ process_long_option (int argc, char **argv, const char *optstring,
funlockfile (stderr);
}
}
- if (ambig_malloced)
- free (ambig_set);
+ free (ambig_malloced);
d->__nextchar += strlen (d->__nextchar);
d->optind++;
d->optopt = 0;