#define _GNU_SOURCE
#include <stddef.h>
+#include <stdlib.h>
+#include <limits.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
+#include "stdio_impl.h"
extern int __optpos, __optreset;
av[dest] = tmp;
}
-void __getopt_msg(const char *, const char *, const char *, size_t);
-
static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
{
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
int i, cnt, match;
- char *opt;
+ char *arg, *opt, *start = argv[optind]+1;
for (cnt=i=0; longopts[i].name; i++) {
const char *name = longopts[i].name;
- opt = argv[optind]+1;
+ opt = start;
if (*opt == '-') opt++;
- for (; *name && *name == *opt; name++, opt++);
+ while (*opt && *opt != '=' && *opt == *name)
+ name++, opt++;
if (*opt && *opt != '=') continue;
+ arg = opt;
match = i;
if (!*name) {
cnt = 1;
}
cnt++;
}
+ if (cnt==1 && longonly && arg-start == mblen(start, MB_LEN_MAX)) {
+ int l = arg-start;
+ for (i=0; optstring[i]; i++) {
+ int j;
+ for (j=0; j<l && start[j]==optstring[i+j]; j++);
+ if (j==l) {
+ cnt++;
+ break;
+ }
+ }
+ }
if (cnt==1) {
i = match;
+ opt = arg;
optind++;
- optopt = longopts[i].val;
if (*opt == '=') {
if (!longopts[i].has_arg) {
+ optopt = longopts[i].val;
if (colon || !opterr)
return '?';
__getopt_msg(argv[0],
optarg = opt+1;
} else if (longopts[i].has_arg == required_argument) {
if (!(optarg = argv[optind])) {
+ optopt = longopts[i].val;
if (colon) return ':';
if (!opterr) return '?';
__getopt_msg(argv[0],
return longopts[i].val;
}
if (argv[optind][1] == '-') {
+ optopt = 0;
if (!colon && opterr)
__getopt_msg(argv[0], cnt ?
": option is ambiguous: " :