121397Smckusick /* 232677Sbostic * Copyright (c) 1987 Regents of the University of California. 332677Sbostic * All rights reserved. 432677Sbostic * 5*42635Sbostic * %sccs.include.redist.c% 621397Smckusick */ 721397Smckusick 826524Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*42635Sbostic static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 06/01/90"; 1034017Sbostic #endif /* LIBC_SCCS and not lint */ 1121397Smckusick 1221397Smckusick #include <stdio.h> 1321397Smckusick 1421397Smckusick /* 1521397Smckusick * get option letter from argument vector 1621397Smckusick */ 1725264Smckusick int opterr = 1, /* if error message should be printed */ 1821397Smckusick optind = 1, /* index into parent argv vector */ 1921397Smckusick optopt; /* character checked for validity */ 2021397Smckusick char *optarg; /* argument associated with option */ 2121397Smckusick 2234017Sbostic #define BADCH (int)'?' 2334017Sbostic #define EMSG "" 2421397Smckusick 2532111Sbostic getopt(nargc, nargv, ostr) 2634017Sbostic int nargc; 2734017Sbostic char **nargv, *ostr; 2821397Smckusick { 2934017Sbostic static char *place = EMSG; /* option letter processing */ 3034017Sbostic register char *oli; /* option letter list index */ 3138284Sbostic char *p, *index(), *rindex(); 3221397Smckusick 3332111Sbostic if (!*place) { /* update scanning pointer */ 3440166Sbostic if (optind >= nargc || *(place = nargv[optind]) != '-') { 3540166Sbostic place = EMSG; 3632111Sbostic return(EOF); 3740166Sbostic } 3834017Sbostic if (place[1] && *++place == '-') { /* found "--" */ 3921397Smckusick ++optind; 4040166Sbostic place = EMSG; 4121397Smckusick return(EOF); 4221397Smckusick } 4332111Sbostic } /* option letter okay? */ 4432111Sbostic if ((optopt = (int)*place++) == (int)':' || 4532111Sbostic !(oli = index(ostr, optopt))) { 4639712Sbostic /* 4739712Sbostic * if the user didn't specify '-' as an option, 4839712Sbostic * assume it means EOF. 4939712Sbostic */ 5039712Sbostic if (optopt == (int)'-') 5139712Sbostic return(EOF); 5232111Sbostic if (!*place) 5332111Sbostic ++optind; 5438284Sbostic if (opterr) { 5538284Sbostic if (!(p = rindex(*nargv, '/'))) 5638284Sbostic p = *nargv; 5738284Sbostic else 5838284Sbostic ++p; 5936960Sbostic (void)fprintf(stderr, "%s: illegal option -- %c\n", 6038284Sbostic p, optopt); 6138284Sbostic } 6236960Sbostic return(BADCH); 6321397Smckusick } 6432111Sbostic if (*++oli != ':') { /* don't need argument */ 6521397Smckusick optarg = NULL; 6632111Sbostic if (!*place) 6732111Sbostic ++optind; 6821397Smckusick } 6932111Sbostic else { /* need an argument */ 7032111Sbostic if (*place) /* no white space */ 7132111Sbostic optarg = place; 7221397Smckusick else if (nargc <= ++optind) { /* no arg */ 7321397Smckusick place = EMSG; 7438284Sbostic if (!(p = rindex(*nargv, '/'))) 7538284Sbostic p = *nargv; 7638284Sbostic else 7738284Sbostic ++p; 7836960Sbostic if (opterr) 7936960Sbostic (void)fprintf(stderr, 8036960Sbostic "%s: option requires an argument -- %c\n", 8138284Sbostic p, optopt); 8236960Sbostic return(BADCH); 8321397Smckusick } 8432111Sbostic else /* white space */ 8532111Sbostic optarg = nargv[optind]; 8621397Smckusick place = EMSG; 8721397Smckusick ++optind; 8821397Smckusick } 8932111Sbostic return(optopt); /* dump back option letter */ 9021397Smckusick } 91