xref: /aoo42x/main/soltools/cpp/_getopt.c (revision cdf0e10c)
1 #include    <stdio.h>
2 #include    <string.h>
3 
4 #define EPR                 fprintf(stderr,
5 #define ERR(str, chr)       if(opterr) { EPR "%s%c\n", str, chr); }
6 
7 int opterr = 1;
8 int optind = 1;
9 int optopt;
10 char *optarg;
11 
12 int
13     stgetopt(int argc, char *const argv[], const char *opts)
14 {
15     static int sp = 1;
16     register int c;
17     register char *cp;
18 
19     if (sp == 1)
20     {
21         if (optind >= argc ||
22             argv[optind][0] != '-' || argv[optind][1] == '\0')
23             return -1;
24         else if (strcmp(argv[optind], "--") == 0)
25             {
26                 optind++;
27                 return -1;
28             }
29         else if (strcmp(argv[optind], "-isysroot") == 0)
30             {
31                 // skip Mac OS X SDK selection flags
32                 optind++; optind++;
33             }
34     }
35     optopt = c = argv[optind][sp];
36     if (c == ':' || (cp = strchr(opts, c)) == 0)
37     {
38         ERR(": illegal option -- ", c);
39         if (argv[optind][++sp] == '\0')
40         {
41             optind++;
42             sp = 1;
43         }
44         return '?';
45     }
46     if (*++cp == ':')
47     {
48         if (argv[optind][sp + 1] != '\0')
49             optarg = &argv[optind++][sp + 1];
50         else
51             if (++optind >= argc)
52             {
53                 ERR(": option requires an argument -- ", c);
54                 sp = 1;
55                 return '?';
56             }
57             else
58                 optarg = argv[optind++];
59         sp = 1;
60     }
61     else
62     {
63         if (argv[optind][++sp] == '\0')
64         {
65             sp = 1;
66             optind++;
67         }
68         optarg = 0;
69     }
70     return c;
71 }
72