#include <stdlib.h>
#include <string.h>
+#include "getopt.h"
+
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt, /* character checked for validity */
* Parse argc/argv argument vector.
*/
int
-getopt(nargc, nargv, ostr)
- int nargc;
- char * const *nargv;
- const char *ostr;
+getopt(int nargc, char * const *nargv, const char *ostr)
{
-#ifdef _WIN32
- char *__progname="windump";
-#else
- extern char *__progname;
-#endif
+ char *cp;
+ static char *__progname;
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
+ if (__progname == NULL) {
+ if ((cp = strrchr(nargv[0], '/')) != NULL)
+ __progname = cp + 1;
+ else
+ __progname = nargv[0];
+ }
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (-1);
}
- } /* option letter okay? */
- if ((optopt = (int)*place++) == (int)':' ||
- !(oli = strchr(ostr, optopt))) {
+ }
+ optopt = (int)*place++;
+ if (optopt == (int)':') { /* option letter okay? */
+ if (!*place)
+ ++optind;
+ if (opterr && *ostr != ':')
+ (void)fprintf(stderr,
+ "%s: illegal option -- %c\n", __progname, optopt);
+ return (BADCH);
+ }
+ oli = strchr(ostr, optopt);
+ if (!oli) {
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
__progname, optopt);
return (BADCH);
}
- else /* white space */
+ else /* white space */
optarg = nargv[optind];
place = EMSG;
++optind;