home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- extern char *index();
-
- /*
- * getopt - parse the arguments given.
- * retrieved from net.sources
- */
- int opterr = 1;
- int optind = 1;
- int optopt;
- char *optarg;
-
- #define BADCH (int)'?'
- #define EMSG ""
- #define TELL(s) fputs(*nargv, stderr); fputs(s, stderr);\
- fputc(optopt, stderr); fputc('\n', stderr);\
- return(BADCH);
-
- int getopt(nargc, nargv, ostr)
- int nargc;
- char **nargv;
- char *ostr;
- {
- register char *oli;
- static char *place = EMSG;
-
- if (!*place) {
- if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
- return(EOF);
- if (*place == '-') {
- ++optind;
- return(EOF);
- }
- }
- if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr, optopt))) {
- if (!*place)
- ++optind;
- TELL(": illegal option -- ");
- }
- if (*++oli != ':') {
- optarg = NULL;
- if (!*place)
- ++optind;
- } else {
- if (*place)
- optarg = place;
- else if (nargc <= ++optind) {
- place = EMSG;
- TELL(": option requires an argument -- ");
- } else
- optarg = nargv[optind];
- place = EMSG;
- ++optind;
- }
- return(optopt);
- }
-
-