* [PATCH] getopt_long: Support abbreviated option matching
@ 2013-11-05 5:45 Michael Forney
2013-11-05 9:27 ` [PATCH v2] " Michael Forney
0 siblings, 1 reply; 2+ messages in thread
From: Michael Forney @ 2013-11-05 5:45 UTC (permalink / raw)
To: musl
From the getopt_long manpage:
Long option names may be abbreviated if the abbreviation is unique
or is an exact match for some defined option
---
This patch is fairly straightforward and provides more compatibility with
glibc's getopt_long.
I've run into at least one case where a script called getopt from util-linux
with the abbreviation --long instead of the full option name --longoptions.
src/misc/getopt_long.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c
index 4ef5a5c..65bc14c 100644
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -2,6 +2,7 @@
#include <stddef.h>
#include <getopt.h>
#include <stdio.h>
+#include <string.h>
extern int __optpos, __optreset;
@@ -16,14 +17,19 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
if ((longonly && argv[optind][1]) ||
(argv[optind][1] == '-' && argv[optind][2]))
{
- int i;
+ int i, j;
for (i=0; longopts[i].name; i++) {
const char *name = longopts[i].name;
- char *opt = argv[optind]+1;
+ char *opt = argv[optind]+1, *c;
if (*opt == '-') opt++;
- for (; *name && *name == *opt; name++, opt++);
- if (*name || (*opt && *opt != '=')) continue;
- if (*opt == '=') {
+ for (c = opt; *name && *name == *c; name++, c++);
+ if (*c && *c != '=') continue;
+ if (*name) {
+ if (name == longopts[i].name) continue;
+ for (j=i+1; longopts[j].name && strncmp(opt, longopts[j].name, c - opt); j++);
+ if (longopts[j].name) continue;
+ }
+ if (*c == '=') {
if (!longopts[i].has_arg) continue;
optarg = opt+1;
} else {
--
1.8.4.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH v2] getopt_long: Support abbreviated option matching
2013-11-05 5:45 [PATCH] getopt_long: Support abbreviated option matching Michael Forney
@ 2013-11-05 9:27 ` Michael Forney
0 siblings, 0 replies; 2+ messages in thread
From: Michael Forney @ 2013-11-05 9:27 UTC (permalink / raw)
To: musl
From the getopt_long manpage:
Long option names may be abbreviated if the abbreviation is unique
or is an exact match for some defined option
---
The previous version missed changing "optarg = opt+1;" to "optarg = c+1;",
breaking long options with =.
src/misc/getopt_long.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c
index 4ef5a5c..9bf8e87 100644
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -2,6 +2,7 @@
#include <stddef.h>
#include <getopt.h>
#include <stdio.h>
+#include <string.h>
extern int __optpos, __optreset;
@@ -16,16 +17,21 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
if ((longonly && argv[optind][1]) ||
(argv[optind][1] == '-' && argv[optind][2]))
{
- int i;
+ int i, j;
for (i=0; longopts[i].name; i++) {
const char *name = longopts[i].name;
- char *opt = argv[optind]+1;
+ char *opt = argv[optind]+1, *c;
if (*opt == '-') opt++;
- for (; *name && *name == *opt; name++, opt++);
- if (*name || (*opt && *opt != '=')) continue;
- if (*opt == '=') {
+ for (c = opt; *name && *name == *c; name++, c++);
+ if (*c && *c != '=') continue;
+ if (*name) {
+ if (name == longopts[i].name) continue;
+ for (j=i+1; longopts[j].name && strncmp(opt, longopts[j].name, c - opt); j++);
+ if (longopts[j].name) continue;
+ }
+ if (*c == '=') {
if (!longopts[i].has_arg) continue;
- optarg = opt+1;
+ optarg = c+1;
} else {
if (longopts[i].has_arg == required_argument) {
if (!(optarg = argv[++optind]))
--
1.8.4.2
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-11-05 9:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-05 5:45 [PATCH] getopt_long: Support abbreviated option matching Michael Forney
2013-11-05 9:27 ` [PATCH v2] " Michael Forney
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).