]> The Tcpdump Group git mirrors - tcpdump/blobdiff - missing/getopt_long.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[tcpdump] / missing / getopt_long.c
index 3706240487bf3407703fc040a7e8de29968a4b7b..e9b1ef5770a132fb546c09ddde2bf29c3c5b1f96 100644 (file)
@@ -57,6 +57,8 @@
 #include <string.h>
 #include <stdarg.h>
 
+#include "diag-control.h"
+
 #define GNU_COMPATIBLE         /* Be more compatible, configure's use us! */
 
 #define PRINT_ERROR    ((opterr) && (*options != ':'))
@@ -68,7 +70,7 @@
 /* return values */
 #define        BADCH           (int)'?'
 #define        BADARG          ((*options == ':') ? (int)':' : (int)'?')
-#define        INORDER         (int)1
+#define        INORDER         (int)1
 
 #define        EMSG            ""
 
@@ -158,11 +160,29 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
                                pos -= nnonopts;
                        else
                                pos += nopts;
+                       /*
+                        * This is annoying - I guess the
+                        * "char * const argv[]" in the declaration
+                        * of getopt() - and thus getopt_long() -
+                        * means that it makes a promise not to
+                        * shuffle the arguments, but here we are,
+                        * shuffling the arguments.
+                        *
+                        * (No, it's not a promise that it won't
+                        * modify any of the argument strings.
+                        * It's a promise that it won't modify
+                        * the array of pointers to the argument
+                        * strings.)
+                        *
+                        * So squelch the cast warnings.
+                        */
                        swap = nargv[pos];
+DIAG_OFF_CAST_QUAL
                        /* LINTED const cast */
                        ((char **) nargv)[pos] = nargv[cstart];
                        /* LINTED const cast */
                        ((char **)nargv)[cstart] = swap;
+DIAG_ON_CAST_QUAL
                }
        }
 }
@@ -291,6 +311,7 @@ parse_long_options(char * const *nargv, const char *options,
                }
                if (long_options[match].has_arg == required_argument ||
                    long_options[match].has_arg == optional_argument) {
+DIAG_OFF_CAST_QUAL
                        if (has_equal)
                                optarg = (char *)has_equal;
                        else if (long_options[match].has_arg ==
@@ -300,6 +321,7 @@ parse_long_options(char * const *nargv, const char *options,
                                 */
                                optarg = nargv[optind++];
                        }
+DIAG_ON_CAST_QUAL
                }
                if ((long_options[match].has_arg == required_argument)
                    && (optarg == NULL)) {
@@ -397,8 +419,7 @@ start:
                                permute_args(nonopt_start, nonopt_end,
                                    optind, nargv);
                                optind -= nonopt_end - nonopt_start;
-                       }
-                       else if (nonopt_start != -1) {
+                       } else if (nonopt_start != -1) {
                                /*
                                 * If we skipped non-options, set optind
                                 * to the first of them.
@@ -495,32 +516,16 @@ start:
                }
        }
 
-       optchar = (int)*place++;
-       /*
-        * If the user specified "-" and  '-' isn't listed in
-        * options, return -1 (non-option) as per POSIX.
-        */
-       if (optchar == (int)'-' && *place == '\0')
-               return (-1);
-       if (optchar == (int)':') {
-               if (!*place)
-                       ++optind;
-#ifdef GNU_COMPATIBLE
-               if (PRINT_ERROR)
-                       warnx(posixly_correct ? illoptchar : gnuoptchar,
-                             optchar);
-#else
-               if (PRINT_ERROR)
-                       warnx(illoptchar, optchar);
-#endif
-               optopt = optchar;
-               return (BADCH);
-       }
-       oli = strchr(options, optchar);
-       if (oli == NULL) {
+       if ((optchar = (int)*place++) == (int)':' ||
+           (optchar == (int)'-' && *place != '\0') ||
+           (oli = strchr(options, optchar)) == NULL) {
                /*
-                * Unknown option character.
+                * If the user specified "-" and  '-' isn't listed in
+                * options, return -1 (non-option) as per POSIX.
+                * Otherwise, it is an unknown option character (or ':').
                 */
+               if (optchar == (int)'-' && *place == '\0')
+                       return (-1);
                if (!*place)
                        ++optind;
 #ifdef GNU_COMPATIBLE
@@ -559,9 +564,11 @@ start:
                        ++optind;
        } else {                                /* takes (optional) argument */
                optarg = NULL;
-               if (*place)                     /* no white space */
+               if (*place) {                   /* no white space */
+DIAG_OFF_CAST_QUAL
                        optarg = (char *)place;
-               else if (oli[1] != ':') {       /* arg not optional */
+DIAG_ON_CAST_QUAL
+               } else if (oli[1] != ':') {     /* arg not optional */
                        if (++optind >= nargc) {        /* no arg */
                                place = EMSG;
                                if (PRINT_ERROR)