~ubuntu-branches/ubuntu/trusty/emscripten/trusty-proposed

« back to all changes in this revision

Viewing changes to system/lib/libc/musl/src/misc/getopt_long.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2014-01-19 14:12:40 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20140119141240-jg1l42cc158j59tn
Tags: 1.9.0~20140119~7dc8c2f-1
* New snapshot release (Closes: #733714)
* Provide sources for javascript and flash. Done in orig-tar.sh
  Available in third_party/websockify/include/web-socket-js/src/
  (Closes: #735903)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _GNU_SOURCE
 
2
#include <stddef.h>
 
3
#include <getopt.h>
 
4
#include <stdio.h>
 
5
 
 
6
extern int __optpos, __optreset;
 
7
 
 
8
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
 
9
{
 
10
        if (!optind || __optreset) {
 
11
                __optreset = 0;
 
12
                __optpos = 0;
 
13
                optind = 1;
 
14
        }
 
15
        if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1;
 
16
        if ((longonly && argv[optind][1]) ||
 
17
                (argv[optind][1] == '-' && argv[optind][2]))
 
18
        {
 
19
                int i;
 
20
                for (i=0; longopts[i].name; i++) {
 
21
                        const char *name = longopts[i].name;
 
22
                        char *opt = argv[optind]+1;
 
23
                        if (*opt == '-') opt++;
 
24
                        for (; *name && *name == *opt; name++, opt++);
 
25
                        if (*name || (*opt && *opt != '=')) continue;
 
26
                        if (*opt == '=') {
 
27
                                if (!longopts[i].has_arg) continue;
 
28
                                optarg = opt+1;
 
29
                        } else {
 
30
                                if (longopts[i].has_arg == required_argument) {
 
31
                                        if (!(optarg = argv[++optind]))
 
32
                                                return ':';
 
33
                                } else optarg = NULL;
 
34
                        }
 
35
                        optind++;
 
36
                        if (idx) *idx = i;
 
37
                        if (longopts[i].flag) {
 
38
                                *longopts[i].flag = longopts[i].val;
 
39
                                return 0;
 
40
                        }
 
41
                        return longopts[i].val;
 
42
                }
 
43
                if (argv[optind][1] == '-') {
 
44
                        optind++;
 
45
                        return '?';
 
46
                }
 
47
        }
 
48
        return getopt(argc, argv, optstring);
 
49
}
 
50
 
 
51
int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
 
52
{
 
53
        return __getopt_long(argc, argv, optstring, longopts, idx, 0);
 
54
}
 
55
 
 
56
int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
 
57
{
 
58
        return __getopt_long(argc, argv, optstring, longopts, idx, 1);
 
59
}