* Copyright (c) 1991 The Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)egetopt.c 5.1 (Berkeley) %G%";
* egetopt: get option letter from argument vector (an extended
* Non standard additions to the ostr specs are:
* 1) '?': immediate value following arg is optional (no white space
* between the arg and the value)
* 2) '#': +/- followed by a number (with an optional sign but
* no white space between the arg and the number). The - may be
* combined with other options, but the + cannot.
int eopterr
= 1; /* if error message should be printed */
int eoptind
= 1; /* index into parent argv vector */
int eoptopt
; /* character checked for validity */
char *eoptarg
; /* argument associated with option */
egetopt(int nargc
, char * const *nargv
, const char *ostr
)
egetopt(nargc
, nargv
, ostr
)
static char *place
= EMSG
; /* option letter processing */
register char *oli
; /* option letter list index */
static int delim
; /* which option delimeter */
static char savec
= '\0';
* update scanning pointer
if ((eoptind
>= nargc
) ||
((*(place
= nargv
[eoptind
]) != '-') && (*place
!= '+'))) {
if (place
[1] && *++place
== '-' && !place
[1]) {
if ((eoptopt
= (int)*place
++) == (int)':' || (eoptopt
== (int)'?') ||
!(oli
= index(ostr
, eoptopt
))) {
* if the user didn't specify '-' as an option,
* assume it means EOF when by itself.
if ((eoptopt
== (int)'-') && !*place
)
if (index(ostr
, '#') && (isdigit(eoptopt
) ||
(((eoptopt
== (int)'-') || (eoptopt
== (int)'+')) &&
* # option: +/- with a number is ok
for (p
= place
; *p
!= '\0'; ++p
) {
if (!(p
= rindex(*nargv
, '/')))
(void)fprintf(stderr
, "%s: illegal option -- %c\n",
* '+' is only allowed with numbers
if (!(p
= rindex(*nargv
, '/')))
"%s: illegal '+' delimiter with option -- %c\n",
if ((*oli
!= ':') && (*oli
!= '?')) {
} else if (*oli
== '?') {
* no arg, but NOT required
} else if (nargc
<= ++eoptind
) {
* no arg, but IS required
if (!(p
= rindex(*nargv
, '/')))
"%s: option requires an argument -- %c\n", p
,
eoptarg
= nargv
[eoptind
];