emergency commit
[cl-cudd.git] / distr / util / getopt.c
blob6ed10a8e7039802200aba999ad7c4733993fdfa5
1 /* LINTLIBRARY */
3 #include <stdio.h>
4 #include "util.h"
7 /* File : getopt.c
8 * Author : Henry Spencer, University of Toronto
9 * Updated: 28 April 1984
11 * Changes: (R Rudell)
12 * changed index() to strchr();
13 * added getopt_reset() to reset the getopt argument parsing
15 * Purpose: get option letter from argv.
18 char *util_optarg; /* Global argument pointer. */
19 int util_optind = 0; /* Global argv index. */
20 static char *scan;
23 void
24 util_getopt_reset()
26 util_optarg = 0;
27 util_optind = 0;
28 scan = 0;
33 int
34 util_getopt(int argc, char * const argv[], char const *optstring)
36 register int c;
37 register char *place;
39 util_optarg = NIL(char);
41 if (scan == NIL(char) || *scan == '\0') {
42 if (util_optind == 0) util_optind++;
43 if (util_optind >= argc) return EOF;
44 place = argv[util_optind];
45 if (place[0] != '-' || place[1] == '\0') return EOF;
46 util_optind++;
47 if (place[1] == '-' && place[2] == '\0') return EOF;
48 scan = place+1;
51 c = *scan++;
52 place = strchr(optstring, c);
53 if (place == NIL(char) || c == ':') {
54 (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
55 return '?';
57 if (*++place == ':') {
58 if (*scan != '\0') {
59 util_optarg = scan;
60 scan = NIL(char);
61 } else {
62 if (util_optind >= argc) {
63 (void) fprintf(stderr, "%s: %c requires an argument\n",
64 argv[0], c);
65 return '?';
67 util_optarg = argv[util_optind];
68 util_optind++;
71 return c;