Welcome to mirror list, hosted at ThFree Co, Russian Federation.

getoptlong.c « linux « sys « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 089f4cdeecd23d14c4645c2a84acdd9b58776b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <unistd.h>
#include <string.h>
#include <getopt.h>

/* Written 2000 by Werner Almesberger */

static const char *__resume;


int getopt_long(int argc,char *const argv[],const char *optstring,
  const struct option *longopts,int *longindex)
{
    char *here;

    optarg = NULL;
    if (!__resume) {
	if (argc == optind || *argv[optind] != '-') return -1;
	if (argv[optind][1] == '-') {
	    const struct option *opt;

	    optarg = strchr(argv[optind],'=');
	    if (optarg) optarg++;
	    for (opt = longopts; opt->name &&
	      (optarg || strcmp(opt->name,argv[optind]+2)) &&
	      (!optarg || strlen(opt->name) != optarg-argv[optind]-3 ||
	      strncmp(opt->name,argv[optind]+2,optarg-argv[optind]-3));
	      opt++);
	    optind++;
	    if (!opt->name) return '?';
	    if ((opt->has_arg == no_argument && optarg) ||
	      (opt->has_arg == required_argument && !optarg)) return ':';
	    if (longindex) *longindex = opt-longopts;
	    if (!opt->flag) return opt->val;
	    *opt->flag = opt->val;
	    return 0;
	}
	else {
	    __resume = argv[optind]+1;
	}
    }
    here = strchr(optstring,*__resume);
    if (!here) {
	optind++;
	__resume = NULL;
	return '?';
    }
    if (here[1] != ':') {
	if (!*++__resume) __resume = NULL;
    }
    else {
	if (__resume[1]) optarg = (char *) __resume+1;
	else {
	    optarg = (char *) argv[++optind];
	    if (optind == argc || *argv[optind] == '-') {
		optind++;
		__resume = NULL;
		return ':';
	    }
	}
	__resume = NULL;
    }
    if (!__resume) optind++;
    return *here;
}