00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include "util.h"
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 char *util_optarg;
00025 int util_optind = 0;
00026 static char *scan;
00027
00028
00029 void
00030 util_getopt_reset(void)
00031 {
00032 util_optarg = 0;
00033 util_optind = 0;
00034 scan = 0;
00035 }
00036
00037
00038
00039 int
00040 util_getopt(int argc, char * const argv[], char const *optstring)
00041 {
00042 register int c;
00043 register char *place;
00044
00045 util_optarg = NIL(char);
00046
00047 if (scan == NIL(char) || *scan == '\0') {
00048 if (util_optind == 0) util_optind++;
00049 if (util_optind >= argc) return EOF;
00050 place = argv[util_optind];
00051 if (place[0] != '-' || place[1] == '\0') return EOF;
00052 util_optind++;
00053 if (place[1] == '-' && place[2] == '\0') return EOF;
00054 scan = place+1;
00055 }
00056
00057 c = *scan++;
00058 place = strchr(optstring, c);
00059 if (place == NIL(char) || c == ':') {
00060 (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
00061 return '?';
00062 }
00063 if (*++place == ':') {
00064 if (*scan != '\0') {
00065 util_optarg = scan;
00066 scan = NIL(char);
00067 } else {
00068 if (util_optind >= argc) {
00069 (void) fprintf(stderr, "%s: %c requires an argument\n",
00070 argv[0], c);
00071 return '?';
00072 }
00073 util_optarg = argv[util_optind];
00074 util_optind++;
00075 }
00076 }
00077 return c;
00078 }