Add Apache License version 2.0.
[pbc.git] / pbc / XGetopt.win32.c
blob616bc784fb082929d3189a3844869f48320e71e9
1 // Adapted from: XGetopt.cpp Version 1.2 by Hans Dietrich
3 #include "XGetopt.win32.h"
4 #include <string.h>
6 char *optarg; // global argument pointer
7 int optind = 0; // global argv index
9 int getopt(int argc, char *argv[], char *optstring)
11 static char *next = NULL;
12 if (optind == 0)
13 next = NULL;
15 optarg = NULL;
17 if (next == NULL || *next == '\0')
19 if (optind == 0)
20 optind++;
22 if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
24 optarg = NULL;
25 if (optind < argc)
26 optarg = argv[optind];
27 return -1;
30 if (strcmp(argv[optind], "--") == 0)
32 optind++;
33 optarg = NULL;
34 if (optind < argc)
35 optarg = argv[optind];
36 return -1;
39 next = argv[optind];
40 next++; // skip past -
41 optind++;
44 char c = *next++;
45 char *cp = strchr(optstring, c);
47 if (cp == NULL || c == ':')
48 return '?';
50 cp++;
51 if (*cp == ':')
53 if (*next != '\0')
55 optarg = next;
56 next = NULL;
58 else if (optind < argc)
60 optarg = argv[optind];
61 optind++;
63 else
65 return '?';
69 return c;