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 }
00058 optopt = c = argv[optind][sp];
00059 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
00060 fprintf(stderr, ": illegal option -- %c\n", c);
00061 if (argv[optind][++sp] == '\0') {
00062 optind++;
00063 sp = 1;
00064 }
00065 return '?';
00066 }
00067 if (*++cp == ':') {
00068 if (argv[optind][sp+1] != '\0')
00069 optarg = &argv[optind++][sp+1];
00070 else if(++optind >= argc) {
00071 fprintf(stderr, ": option requires an argument -- %c\n", c);
00072 sp = 1;
00073 return '?';
00074 } else
00075 optarg = argv[optind++];
00076 sp = 1;
00077 } else {
00078 if (argv[optind][++sp] == '\0') {
00079 sp = 1;
00080 optind++;
00081 }
00082 optarg = NULL;
00083 }
00084
00085 return c;
00086 }