3 * Copyright (c) 1992-2001 by Mike Gleason.
13 int gOptErr
= 1; /* if error message should be printed */
14 int gOptInd
= 1; /* index into parent argv vector */
15 int gOptOpt
; /* character checked for validity */
16 const char *gOptArg
; /* argument associated with option */
17 const char *gOptPlace
= kGetoptErrMsg
; /* saved position in an arg */
19 /* This must be called before each Getopt. */
24 gOptPlace
= kGetoptErrMsg
;
31 NextOption(const char *const ostr
)
33 if ((gOptOpt
= (int) *gOptPlace
++) == (int) ':')
35 return strchr(ostr
, gOptOpt
);
42 Getopt(int nargc
, const char **const nargv
, const char *const ostr
)
44 const char *oli
; /* Option letter list index */
46 if (!*gOptPlace
) { /* update scanning pointer */
47 if (gOptInd
>= nargc
|| *(gOptPlace
= nargv
[gOptInd
]) != '-')
49 if (gOptPlace
[1] && *++gOptPlace
== '-') { /* found "--" */
53 } /* Option letter okay? */
54 oli
= NextOption(ostr
);
59 (void) fprintf(stderr
, "%s%s%c\n", *nargv
, ": illegal option -- ", gOptOpt
);
60 return(kGetoptBadChar
);
62 if (*++oli
!= ':') { /* don't need argument */
66 } else { /* need an argument */
67 if (*gOptPlace
) /* no white space */
69 else if (nargc
<= ++gOptInd
) { /* no arg */
70 gOptPlace
= kGetoptErrMsg
;
72 (void) fprintf(stderr
, "%s%s%c\n", *nargv
, ": option requires an argument -- ", gOptOpt
);
73 return(kGetoptBadChar
);
74 } else /* white space */
75 gOptArg
= nargv
[gOptInd
];
76 gOptPlace
= kGetoptErrMsg
;
79 return (gOptOpt
); /* dump back Option letter */