summaryrefslogtreecommitdiff
path: root/source/fitz/getopt.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2013-06-19 15:29:44 +0200
committerTor Andersson <tor.andersson@artifex.com>2013-06-20 16:45:35 +0200
commit0a927854a10e1e6b9770a81e2e1d9f3093631757 (patch)
tree3d65d820d9fdba2d0d394d99c36290c851b78ca0 /source/fitz/getopt.c
parent1ae8f19179c5f0f8c6352b3c7855465325d5449a (diff)
downloadmupdf-0a927854a10e1e6b9770a81e2e1d9f3093631757.tar.xz
Rearrange source files.
Diffstat (limited to 'source/fitz/getopt.c')
-rw-r--r--source/fitz/getopt.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/source/fitz/getopt.c b/source/fitz/getopt.c
new file mode 100644
index 00000000..2a6e5ac4
--- /dev/null
+++ b/source/fitz/getopt.c
@@ -0,0 +1,66 @@
+/*
+ * This is a version of the public domain getopt implementation by
+ * Henry Spencer originally posted to net.sources.
+ *
+ * This file is in the public domain.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#define getopt fz_getopt
+#define optarg fz_optarg
+#define optind fz_optind
+
+char *optarg; /* Global argument pointer. */
+int optind = 0; /* Global argv index. */
+
+static char *scan = NULL; /* Private scan pointer. */
+
+int
+getopt(int argc, char *argv[], char *optstring)
+{
+ char c;
+ char *place;
+
+ optarg = NULL;
+
+ if (!scan || *scan == '\0') {
+ if (optind == 0)
+ optind++;
+
+ if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
+ return EOF;
+ if (argv[optind][1] == '-' && argv[optind][2] == '\0') {
+ optind++;
+ return EOF;
+ }
+
+ scan = argv[optind]+1;
+ optind++;
+ }
+
+ c = *scan++;
+ place = strchr(optstring, c);
+
+ if (!place || c == ':') {
+ fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
+ return '?';
+ }
+
+ place++;
+ if (*place == ':') {
+ if (*scan != '\0') {
+ optarg = scan;
+ scan = NULL;
+ } else if( optind < argc ) {
+ optarg = argv[optind];
+ optind++;
+ } else {
+ fprintf(stderr, "%s: option requires argument -%c\n", argv[0], c);
+ return ':';
+ }
+ }
+
+ return c;
+}