|
|
Message-Id: <1383643626-6690-1-git-send-email-mforney@mforney.org>
Date: Tue, 5 Nov 2013 01:27:06 -0800
From: Michael Forney <mforney@...rney.org>
To: musl@...ts.openwall.com
Subject: [PATCH v2] getopt_long: Support abbreviated option matching
>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
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.