]> The Tcpdump Group git mirrors - libpcap/blobdiff - missing/getopt.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / missing / getopt.c
index f5e62daa4cccf380ae1b1e06f3d96cb5b0b653c1..c535776d849ca2bc95ee348df050b404742f8750 100644 (file)
@@ -39,6 +39,8 @@ static char sccsid[] = "@(#)getopt.c  8.3 (Berkeley) 4/27/95";
 #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 */
@@ -54,19 +56,19 @@ char        *optarg;                /* argument associated with option */
  *     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]) != '-') {
@@ -78,9 +80,18 @@ getopt(nargc, nargv, ostr)
                        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.
@@ -112,7 +123,7 @@ getopt(nargc, nargv, ostr)
                                    __progname, optopt);
                        return (BADCH);
                }
-               else                            /* white space */
+               else                            /* white space */
                        optarg = nargv[optind];
                place = EMSG;
                ++optind;