* 'getopt-musl' of https://github.com/sgn/util-linux:
  getopt: explicitly ask for POSIX mode on POSIXLY_CORRECT
This commit is contained in:
Karel Zak 2021-01-07 12:09:57 +01:00
commit e7091237db
3 changed files with 21 additions and 4 deletions

View File

@ -205,6 +205,7 @@ usrbin_exec_PROGRAMS += getopt
dist_man_MANS += misc-utils/getopt.1
PATHFILES += misc-utils/getopt.1
getopt_SOURCES = misc-utils/getopt.c
getopt_LDADD = $(LDADD) libcommon.la
getoptexampledir = $(docdir)
dist_getoptexample_DATA = \
misc-utils/getopt-example.bash \

View File

@ -382,7 +382,11 @@ will be parsed. It will still do parameter shuffling (i.e., all
non\-option parameters are output at the end), unless the
environment variable
.B POSIXLY_CORRECT
is set.
is set, in which case,
.B getopt
will prepend a
.RB ' + '
before short options automatically.
.PP
The environment variable
.B GETOPT_COMPATIBLE

View File

@ -69,6 +69,7 @@
#include "closestream.h"
#include "nls.h"
#include "strutils.h"
#include "xalloc.h"
/* NON_OPT is the code that is returned getopt(3) when a non-option is
@ -275,6 +276,18 @@ static void add_longopt(struct getopt_control *ctl, const char *name, int has_ar
}
static void add_short_options(struct getopt_control *ctl, char *options)
{
free(ctl->optstr);
if (*options != '+' && getenv("POSIXLY_CORRECT"))
ctl->optstr = strappend("+", options);
else
ctl->optstr = xstrdup(options);
if (!ctl->optstr)
err_oom();
}
/*
* Register several long options. options is a string of long options,
* separated by commas or whitespace. This nukes options!
@ -414,8 +427,7 @@ int main(int argc, char *argv[])
getopt_long_fp = getopt_long_only;
break;
case 'o':
free(ctl.optstr);
ctl.optstr = xstrdup(optarg);
add_short_options(&ctl, optarg);
break;
case 'l':
add_long_options(&ctl, optarg);
@ -455,7 +467,7 @@ int main(int argc, char *argv[])
if (optind >= argc)
parse_error(_("missing optstring argument"));
else {
ctl.optstr = xstrdup(argv[optind]);
add_short_options(&ctl, argv[optind]);
optind++;
}
}