merge the formfield patch from ooo-build
[ooovba.git] / soltools / cpp / _getopt.c
blob3b1b1833734d1058bbcf3e8ee780976bc83a98d4
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 if (strcmp(argv[optind], "--") == 0)
26 optind++;
27 return -1;
29 else if (strcmp(argv[optind], "-isysroot") == 0)
31 // skip Mac OS X SDK selection flags
32 optind++; optind++;
35 optopt = c = argv[optind][sp];
36 if (c == ':' || (cp = strchr(opts, c)) == 0)
38 ERR(": illegal option -- ", c);
39 if (argv[optind][++sp] == '\0')
41 optind++;
42 sp = 1;
44 return '?';
46 if (*++cp == ':')
48 if (argv[optind][sp + 1] != '\0')
49 optarg = &argv[optind++][sp + 1];
50 else
51 if (++optind >= argc)
53 ERR(": option requires an argument -- ", c);
54 sp = 1;
55 return '?';
57 else
58 optarg = argv[optind++];
59 sp = 1;
61 else
63 if (argv[optind][++sp] == '\0')
65 sp = 1;
66 optind++;
68 optarg = 0;
70 return c;