Commit 311da164 authored by Andres Freund's avatar Andres Freund

Add support for optional_argument to our own getopt_long() implementation.

07c8651d currently causes compilation errors on mscv (and
probably some other) compilers because our getopt_long()
implementation doesn't have support for optional_argument.

Thus implement optional_argument in our fallback implemenation. It's
quite possibly also useful in other cases.

Arguably this needs a configure check for optional_argument, but it
has existed pretty much since getopt_long() was introduced and thus
doesn't seem worth the configure runtime.

Normally I'd would not push a patch this fast, but this allows msvc to
build again and has low risk as only optional_argument behaviour has
changed.

Author: Michael Paquier and Andres Freund

Discussion: CAB7nPqS5VeedSCxrK=QouokbawgGKLpyc1Q++RRFCa_sjcSVrg@mail.gmail.com
parent b4c28d1b
...@@ -23,6 +23,7 @@ struct option ...@@ -23,6 +23,7 @@ struct option
#define no_argument 0 #define no_argument 0
#define required_argument 1 #define required_argument 1
#define optional_argument 2
#endif #endif
#ifndef HAVE_GETOPT_LONG #ifndef HAVE_GETOPT_LONG
......
...@@ -100,11 +100,14 @@ getopt_long(int argc, char *const argv[], ...@@ -100,11 +100,14 @@ getopt_long(int argc, char *const argv[],
if (strlen(longopts[i].name) == namelen if (strlen(longopts[i].name) == namelen
&& strncmp(place, longopts[i].name, namelen) == 0) && strncmp(place, longopts[i].name, namelen) == 0)
{ {
if (longopts[i].has_arg) int has_arg = longopts[i].has_arg;
if (has_arg != no_argument)
{ {
if (place[namelen] == '=') if (place[namelen] == '=')
optarg = place + namelen + 1; optarg = place + namelen + 1;
else if (optind < argc - 1) else if (optind < argc - 1 &&
has_arg == required_argument)
{ {
optind++; optind++;
optarg = argv[optind]; optarg = argv[optind];
...@@ -113,13 +116,18 @@ getopt_long(int argc, char *const argv[], ...@@ -113,13 +116,18 @@ getopt_long(int argc, char *const argv[],
{ {
if (optstring[0] == ':') if (optstring[0] == ':')
return BADARG; return BADARG;
if (opterr)
if (opterr && has_arg == required_argument)
fprintf(stderr, fprintf(stderr,
"%s: option requires an argument -- %s\n", "%s: option requires an argument -- %s\n",
argv[0], place); argv[0], place);
place = EMSG; place = EMSG;
optind++; optind++;
if (has_arg == required_argument)
return BADCH; return BADCH;
optarg = NULL;
} }
} }
else else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment