Skip to content

Commit 311da16

Browse files
committed
Add support for optional_argument to our own getopt_long() implementation.
07c8651 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
1 parent b4c28d1 commit 311da16

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/include/getopt_long.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct option
2323

2424
#define no_argument 0
2525
#define required_argument 1
26+
#define optional_argument 2
2627
#endif
2728

2829
#ifndef HAVE_GETOPT_LONG

src/port/getopt_long.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,14 @@ getopt_long(int argc, char *const argv[],
100100
if (strlen(longopts[i].name) == namelen
101101
&& strncmp(place, longopts[i].name, namelen) == 0)
102102
{
103-
if (longopts[i].has_arg)
103+
int has_arg = longopts[i].has_arg;
104+
105+
if (has_arg != no_argument)
104106
{
105107
if (place[namelen] == '=')
106108
optarg = place + namelen + 1;
107-
else if (optind < argc - 1)
109+
else if (optind < argc - 1 &&
110+
has_arg == required_argument)
108111
{
109112
optind++;
110113
optarg = argv[optind];
@@ -113,13 +116,18 @@ getopt_long(int argc, char *const argv[],
113116
{
114117
if (optstring[0] == ':')
115118
return BADARG;
116-
if (opterr)
119+
120+
if (opterr && has_arg == required_argument)
117121
fprintf(stderr,
118122
"%s: option requires an argument -- %s\n",
119123
argv[0], place);
124+
120125
place = EMSG;
121126
optind++;
122-
return BADCH;
127+
128+
if (has_arg == required_argument)
129+
return BADCH;
130+
optarg = NULL;
123131
}
124132
}
125133
else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy