update dev300-m58
[ooovba.git] / soltools / cpp / _getopt.c
bloba27d0f2962f1a83ffa7358bb1ca0b3fbd71e84cd
1 #include <stdio.h>
2 #include <string.h>
4 #define EPR fprintf(stderr,
5 #define ERR(str, chr) if(opterr) { EPR "%s%c\n", str, chr); }
7 int opterr = 1;
8 int optind = 1;
9 int optopt;
10 char *optarg;
12 int
13 stgetopt(int argc, char *const argv[], const char *opts)
15 static int sp = 1;
16 register int c;
17 register char *cp;
19 if (sp == 1)
21 if (optind >= argc ||
22 argv[optind][0] != '-' || argv[optind][1] == '\0')
23 return -1;
24 else
25 if (strcmp(argv[optind], "--") == 0)
27 optind++;
28 return -1;
31 optopt = c = argv[optind][sp];
32 if (c == ':' || (cp = strchr(opts, c)) == 0)
34 ERR(": illegal option -- ", c);
35 if (argv[optind][++sp] == '\0')
37 optind++;
38 sp = 1;
40 return '?';
42 if (*++cp == ':')
44 if (argv[optind][sp + 1] != '\0')
45 optarg = &argv[optind++][sp + 1];
46 else
47 if (++optind >= argc)
49 ERR(": option requires an argument -- ", c);
50 sp = 1;
51 return '?';
53 else
54 optarg = argv[optind++];
55 sp = 1;
57 else
59 if (argv[optind][++sp] == '\0')
61 sp = 1;
62 optind++;
64 optarg = 0;
66 return c;