00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 #include <stdio.h>
00034 #include <string.h>
00035 
00036 static int opterr = 1;
00037 static int optind = 1;
00038 static int optopt;
00039 static char *optarg;
00040 
00041 #undef fprintf
00042 
00043 static int getopt(int argc, char *argv[], char *opts)
00044 {
00045     static int sp = 1;
00046     int c;
00047     char *cp;
00048 
00049     if (sp == 1)
00050         if (optind >= argc ||
00051             argv[optind][0] != '-' || argv[optind][1] == '\0')
00052             return EOF;
00053         else if (!strcmp(argv[optind], "--")) {
00054             optind++;
00055             return EOF;
00056         }
00057     optopt = c = argv[optind][sp];
00058     if (c == ':' || (cp = strchr(opts, c)) == NULL) {
00059         fprintf(stderr, ": illegal option -- %c\n", c);
00060         if (argv[optind][++sp] == '\0') {
00061             optind++;
00062             sp = 1;
00063         }
00064         return '?';
00065     }
00066     if (*++cp == ':') {
00067         if (argv[optind][sp+1] != '\0')
00068             optarg = &argv[optind++][sp+1];
00069         else if(++optind >= argc) {
00070             fprintf(stderr, ": option requires an argument -- %c\n", c);
00071             sp = 1;
00072             return '?';
00073         } else
00074             optarg = argv[optind++];
00075         sp = 1;
00076     } else {
00077         if (argv[optind][++sp] == '\0') {
00078             sp = 1;
00079             optind++;
00080         }
00081         optarg = NULL;
00082     }
00083 
00084     return c;
00085 }