|
|
Message-Id: <1383630343-2787-1-git-send-email-mforney@mforney.org>
Date: Mon, 4 Nov 2013 21:45:43 -0800
From: Michael Forney <mforney@...rney.org>
To: musl@...ts.openwall.com
Subject: [PATCH] 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
---
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
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.