summaryrefslogtreecommitdiff
path: root/host/commands/emugen/getopt.c
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@google.com>2016-11-17 16:31:28 +0100
committerGreg Hartman <ghartman@google.com>2018-08-23 17:24:53 -0700
commit62101ac9d3b7add327a1c8fcbf74b5c59aa027d9 (patch)
tree51597e77ff42e139e4c652b1e42a6bbc1c1b9324 /host/commands/emugen/getopt.c
parenta20a1baf99448665e1fb295d97470d4dfa28036a (diff)
downloadopengl-transport-62101ac9d3b7add327a1c8fcbf74b5c59aa027d9.tar.gz
Move android-emugl from distrib/ to android/
Since this is no longer a side-project, move it under android/ where it logically belong. distrib/ should now only contains sources and build files related to third-party libraries used by the emulator. Change-Id: I012c2d8c875d018b0c97891773fa5e8e2811087e
Diffstat (limited to 'host/commands/emugen/getopt.c')
-rw-r--r--host/commands/emugen/getopt.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/host/commands/emugen/getopt.c b/host/commands/emugen/getopt.c
new file mode 100644
index 000000000..35235386b
--- /dev/null
+++ b/host/commands/emugen/getopt.c
@@ -0,0 +1,76 @@
+#include "getopt.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#define _getprogname() nargv[0]
+
+int opterr = 1;
+int optind = 1;
+int optopt = 0;
+char* optarg;
+
+int getopt(int argc, char* const argv[], const char* ostr) {
+ static const char kEmpty[] = "";
+ static const char* place = kEmpty;
+ if (!*place) {
+ if (optind >= argc)
+ return -1;
+
+ const char* arg = argv[optind];
+ if (arg[0] != '-') {
+ // Not an option.
+ return -1;
+ }
+ if (arg[1] == '-' && !arg[2]) {
+ // '--' -> end of options.
+ return -1;
+ }
+ if (!arg[1]) {
+ // Single '-', If the program wants it, treat it as an option.
+ // Otherwise, it's the end of options.
+ if (!strchr(ostr, '-')) {
+ return -1;
+ }
+ optopt = '-';
+ place = arg + 1;
+ } else {
+ optopt = arg[1];
+ place = arg + 2;
+ }
+ };
+
+ char* oindex = strchr(ostr, optopt);
+ if (!oindex) {
+ // Unsupported option.
+ (void)fprintf(stderr, "%s: illegal option -- %c\n", argv[0]);
+ return '?';
+ }
+ if (oindex[1] != ':') {
+ // No argument needed.
+ optarg = NULL;
+ if (!*place)
+ optind++;
+ return optopt;
+ }
+
+ // This option needs an argument. Either after the option character,
+ // or the argument that follows.
+ if (*place) {
+ optarg = (char *)place;
+ } else if (argc > ++optind) {
+ optarg = (char *)argv[optind];
+ } else if (oindex[2] == ':') {
+ // Optional argument is missing.
+ place = kEmpty;
+ optarg = NULL;
+ return optopt;
+ } else {
+ // Missing argument.
+ place = kEmpty;
+ (void)fprintf(stderr, "%s: option requires an argument --%c\n",
+ argv[0], optopt);
+ return ':';
+ }
+ return optopt;
+}