8 * Author : Henry Spencer, University of Toronto
9 * Updated: 28 April 1984
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. */
34 util_getopt(int argc
, char * const argv
[], char const *optstring
)
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
;
47 if (place
[1] == '-' && place
[2] == '\0') return EOF
;
52 place
= strchr(optstring
, c
);
53 if (place
== NIL(char) || c
== ':') {
54 (void) fprintf(stderr
, "%s: unknown option %c\n", argv
[0], c
);
57 if (*++place
== ':') {
62 if (util_optind
>= argc
) {
63 (void) fprintf(stderr
, "%s: %c requires an argument\n",
67 util_optarg
= argv
[util_optind
];