diff options
author | Tor Andersson <tor@ghostscript.com> | 2010-07-09 02:47:51 +0200 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2010-07-09 02:47:51 +0200 |
commit | 8d68dec4b460ac292ce0021717160a8cd9fcc8dd (patch) | |
tree | 322b0a32d97fa288fad5d0114561047bc1c4bf08 /fitz/base_getopt.c | |
parent | 3a4c396334bc500de4c9b1e957f030835b5df65e (diff) | |
download | mupdf-8d68dec4b460ac292ce0021717160a8cd9fcc8dd.tar.xz |
Rearrange and merge some files in the fitz directory.
Diffstat (limited to 'fitz/base_getopt.c')
-rw-r--r-- | fitz/base_getopt.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/fitz/base_getopt.c b/fitz/base_getopt.c new file mode 100644 index 00000000..2c198ba1 --- /dev/null +++ b/fitz/base_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 == NULL || *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 == NULL || 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; +} |