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 #if defined(LIBC_SCCS) && !defined(lint)
00031
00032
00033
00034 static char *rcsid =
00035 "$Id$";
00036 #endif
00037
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041
00042 #ifdef _BSD
00043 extern char *__progname;
00044 #else
00045 #define __progname "getopt"
00046 #endif
00047
00048 int opterr = 1,
00049 optind = 1,
00050 optopt,
00051 optreset;
00052 char *optarg;
00053 char EMSG[] = "";
00054
00055 #define BADCH (int)'?'
00056 #define BADARG (int)':'
00057
00058
00059
00060
00061
00062 int
00063 getopt(int nargc, char *const *nargv, const char *ostr)
00064 {
00065 static char *place = EMSG;
00066 char *oli;
00067
00068 if (optreset || !*place) {
00069 optreset = 0;
00070 if (optind >= nargc || *(place = nargv[optind]) != '-') {
00071 place = EMSG;
00072 return (-1);
00073 }
00074 if (place[1] && *++place == '-') {
00075 ++optind;
00076 place = EMSG;
00077 return (-1);
00078 }
00079 }
00080 if ((optopt = (int) *place++) == (int) ':' ||
00081 !(oli = strchr(ostr, optopt))) {
00082
00083
00084
00085
00086 if (optopt == (int) '-')
00087 return (-1);
00088 if (!*place)
00089 ++optind;
00090 if (opterr && *ostr != ':')
00091 (void) fprintf(stderr,
00092 "%s: illegal option -- %c\n", __progname,
00093 optopt);
00094 return (BADCH);
00095 }
00096 if (*++oli != ':') {
00097 optarg = NULL;
00098 if (!*place)
00099 ++optind;
00100 } else {
00101 if (*place)
00102 optarg = place;
00103 else if (nargc <= ++optind) {
00104 place = EMSG;
00105 if (*ostr == ':')
00106 return (BADARG);
00107 if (opterr)
00108 (void) fprintf(stderr,
00109 "%s: option requires an argument -- %c\n",
00110 __progname, optopt);
00111 return (BADCH);
00112 } else
00113 optarg = nargv[optind];
00114 place = EMSG;
00115 ++optind;
00116 }
00117 return (optopt);
00118 }