6 #define _getprogname() nargv[0]
13 int getopt(int argc, char* const argv[], const char* ostr) {
14 static const char kEmpty[] = "";
15 static const char* place = kEmpty;
20 const char* arg = argv[optind];
25 if (arg[1] == '-' && !arg[2]) {
26 // '--' -> end of options.
30 // Single '-', If the program wants it, treat it as an option.
31 // Otherwise, it's the end of options.
32 if (!strchr(ostr, '-')) {
43 char* oindex = strchr(ostr, optopt);
45 // Unsupported option.
46 (void)fprintf(stderr, "%s: illegal option -- %c\n", argv[0]);
49 if (oindex[1] != ':') {
50 // No argument needed.
57 // This option needs an argument. Either after the option character,
58 // or the argument that follows.
60 optarg = (char *)place;
61 } else if (argc > ++optind) {
62 optarg = (char *)argv[optind];
63 } else if (oindex[2] == ':') {
64 // Optional argument is missing.
71 (void)fprintf(stderr, "%s: option requires an argument --%c\n",