]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Use getopt_long().
authorGuy Harris <[email protected]>
Wed, 30 Apr 2014 22:28:06 +0000 (15:28 -0700)
committerGuy Harris <[email protected]>
Wed, 30 Apr 2014 22:28:06 +0000 (15:28 -0700)
This requires us to check for it in the configure script and to include
a version of getopt_long() for the benefit of platforms that don't have
it; we pick up the FreeBSD version and tweak it a bit (eliminating some
features specific to the BSD version of getopt_long(), as we want to use
it only in a fashion portable to Linux/*BSD/Solaris/etc.)

We also get rid of the version of getopt() we supply for Windows in
favor of the version of getopt_long() we provide.

Makefile.in
config.h.in
configure
configure.in
tcpdump.c
win32/Include/getopt.h [deleted file]
win32/Src/getopt.c [deleted file]
win32/prj/GNUmakefile
win32/prj/WinDump.dsp
win32/prj/WinDump.vcproj

index e027970ad786c4337b99a4d0a3d3693643db0d93..2b6bd1b265617d9de4717fc705c3ebfbf84f1933 100644 (file)
@@ -236,6 +236,7 @@ HDR = \
        ether.h \
        ethertype.h \
        extract.h \
+       getopt.h \
        gmpls.h \
        gmt2local.h \
        interface.h \
@@ -312,6 +313,7 @@ EXTRA_DIST = \
        missing/dlnames.c \
        missing/datalinks.c \
        missing/getnameinfo.c \
+       missing/getopt_long.c \
        missing/inet_aton.c \
        missing/inet_ntop.c \
        missing/inet_pton.c \
@@ -342,9 +344,7 @@ EXTRA_DIST = \
        tcpdump.1.in \
        vfprintf.c \
        win32/Include/errno.h \
-       win32/Include/getopt.h \
        win32/Include/w32_fzs.h \
-       win32/Src/getopt.c \
        win32/prj/GNUmakefile \
        win32/prj/WinDump.dsp \
        win32/prj/WinDump.dsw
@@ -368,6 +368,8 @@ dlnames.o: $(srcdir)/missing/dlnames.c
        $(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/dlnames.c
 getnameinfo.o: $(srcdir)/missing/getnameinfo.c
        $(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/getnameinfo.c
+getopt_long.o: $(srcdir)/missing/getopt_long.c
+       $(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/getopt_long.c
 inet_pton.o: $(srcdir)/missing/inet_pton.c
        $(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/inet_pton.c
 inet_ntop.o: $(srcdir)/missing/inet_ntop.c
index 4cdb31b7fcfd36927626b431c1c66f0b09be26a8..7ac47c0d65abf210eae5ddeec34faab3205ffb6c 100644 (file)
@@ -28,6 +28,9 @@
 /* Define to 1 if you have the `getnameinfo' function. */
 #undef HAVE_GETNAMEINFO
 
+/* Define to 1 if you have the `getopt_long' function. */
+#undef HAVE_GETOPT_LONG
+
 /* define if you have getrpcbynumber() */
 #undef HAVE_GETRPCBYNUMBER
 
index 21ce37b8751e2c9580c0b6d32b758c0e8a3bad91..65d2f298b8788def2ae69c829b3b0d6e1b0bbbcc 100755 (executable)
--- a/configure
+++ b/configure
@@ -3779,6 +3779,7 @@ $as_echo "$ac_cv___attribute___format_function_pointer" >&6; }
 
        fi
 fi
+
 ac_ext=c
 ac_cpp='$CPP $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
@@ -5463,6 +5464,19 @@ esac
 
 fi
 
+ac_fn_c_check_func "$LINENO" "getopt_long" "ac_cv_func_getopt_long"
+if test "x$ac_cv_func_getopt_long" = xyes; then :
+  $as_echo "#define HAVE_GETOPT_LONG 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" getopt_long.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getopt_long.$ac_objext"
+ ;;
+esac
+
+fi
+
 
 for ac_func in fork vfork strftime
 do :
index dde10b380cc7699560d89f41e2fdbf8df30eded8..31d75112a23c90a99d6281b1cffbb3b7d0a709e4 100644 (file)
@@ -31,6 +31,7 @@ if test "$ac_cv___attribute__" = "yes"; then
                AC_C___ATTRIBUTE___FORMAT_FUNCTION_POINTER
        fi
 fi
+       
 AC_CHECK_HEADERS(fcntl.h rpc/rpc.h rpc/rpcent.h netdnet/dnetdb.h)
 AC_CHECK_HEADERS(net/pfvar.h, , , [#include <sys/types.h>
 #include <sys/socket.h>
@@ -535,7 +536,7 @@ if test "$ac_cv_namereqd" = no; then
        missing_includes=yes
 fi
 
-AC_REPLACE_FUNCS(vfprintf strcasecmp strlcat strlcpy strdup strsep)
+AC_REPLACE_FUNCS(vfprintf strcasecmp strlcat strlcpy strdup strsep getopt_long)
 AC_CHECK_FUNCS(fork vfork strftime)
 AC_CHECK_FUNCS(setlinebuf alarm)
 
index 2bc879244098f60a2bac549b23f07d525447261b..6bafec4e20cd589851f3b497363a127cf91bab61 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -46,7 +46,6 @@ The Regents of the University of California.  All rights reserved.\n";
 #include <tcpdump-stdinc.h>
 
 #ifdef WIN32
-#include "getopt.h"
 #include "w32_fzs.h"
 extern int strcasecmp (const char *__s1, const char *__s2);
 extern int SIZE_BUF;
@@ -58,6 +57,11 @@ extern int SIZE_BUF;
 #include <smi.h>
 #endif
 
+#ifdef HAVE_GETOPT_LONG
+#include <getopt.h>
+#else
+#include "getopt.h"
+#endif
 #include <pcap.h>
 #include <signal.h>
 #include <stdio.h>
@@ -599,6 +603,70 @@ show_devices_and_exit (void)
 #define Q_FLAG
 #endif
 
+static struct option longopts[] = {
+       { NULL, no_argument, NULL, 'a' },
+       { NULL, no_argument, NULL, 'A' },
+       { NULL, no_argument, NULL, 'b' },
+#if defined(HAVE_PCAP_CREATE) || defined(WIN32)
+       { NULL, required_argument, NULL, 'B' },
+#endif
+       { NULL, required_argument, NULL, 'c' },
+       { NULL, required_argument, NULL, 'C' },
+       { NULL, no_argument, NULL, 'd' },
+       { NULL, no_argument, NULL, 'D' },
+       { NULL, no_argument, NULL, 'L' },
+       { NULL, no_argument, NULL, 'e' },
+       { NULL, required_argument, NULL, 'E' },
+       { NULL, no_argument, NULL, 'f' },
+       { NULL, required_argument, NULL, 'F' },
+       { NULL, required_argument, NULL, 'G' },
+       { NULL, no_argument, NULL, 'h' },
+       { NULL, no_argument, NULL, 'H' },
+       { NULL, required_argument, NULL, 'i' },
+#ifdef HAVE_PCAP_CREATE
+       { NULL, no_argument, NULL, 'I' },
+#endif
+#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
+       { NULL, required_argument, NULL, 'j' },
+       { NULL, no_argument, NULL, 'J' },
+#endif
+       { NULL, no_argument, NULL, 'l' },
+       { NULL, no_argument, NULL, 'K' },
+       { NULL, required_argument, NULL, 'm' },
+       { NULL, required_argument, NULL, 'M' },
+       { NULL, no_argument, NULL, 'n' },
+       { NULL, no_argument, NULL, 'N' },
+       { NULL, no_argument, NULL, 'O' },
+       { NULL, no_argument, NULL, 'p' },
+       { NULL, no_argument, NULL, 'q' },
+#ifdef HAVE_PCAP_SETDIRECTION
+       { NULL, required_argument, NULL, 'Q' },
+#endif
+       { NULL, required_argument, NULL, 'r' },
+       { NULL, no_argument, NULL, 'R' },
+       { NULL, required_argument, NULL, 's' },
+       { NULL, no_argument, NULL, 'S' },
+       { NULL, no_argument, NULL, 't' },
+       { NULL, required_argument, NULL, 'T' },
+       { NULL, no_argument, NULL, 'u' },
+#ifdef HAVE_PCAP_DUMP_FLUSH
+       { NULL, no_argument, NULL, 'U' },
+#endif
+       { NULL, no_argument, NULL, 'v' },
+       { NULL, required_argument, NULL, 'V' },
+       { NULL, required_argument, NULL, 'w' },
+       { NULL, required_argument, NULL, 'W' },
+       { NULL, no_argument, NULL, 'x' },
+       { NULL, no_argument, NULL, 'X' },
+       { NULL, required_argument, NULL, 'y' },
+#if defined(HAVE_PCAP_DEBUG) || defined(HAVE_YYDEBUG)
+       { NULL, no_argument, NULL, 'Y' },
+#endif
+       { NULL, required_argument, NULL, 'z' },
+       { NULL, required_argument, NULL, 'Z' },
+       { NULL, 0, NULL, 0 }
+};
+
 #ifndef WIN32
 /* Drop root privileges and chroot if necessary */
 static void
@@ -817,7 +885,7 @@ main(int argc, char **argv)
 #endif
 
        while (
-           (op = getopt(argc, argv, "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:Rs:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:")) != -1)
+           (op = getopt_long(argc, argv, "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:Rs:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:", longopts, NULL)) != -1)
                switch (op) {
 
                case 'a':
diff --git a/win32/Include/getopt.h b/win32/Include/getopt.h
deleted file mode 100644 (file)
index b439ab1..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/* Declarations for getopt.
-   Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
-
-This file is part of the GNU C Library.
-
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
-
-#ifndef _GETOPT_H
-#define _GETOPT_H 1
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* For communication from `getopt' to the caller.
-   When `getopt' finds an option that takes an argument,
-   the argument value is returned here.
-   Also, when `ordering' is RETURN_IN_ORDER,
-   each non-option ARGV-element is returned here.  */
-
-#ifndef WIN32
-extern char *optarg;
-#endif
-
-/* Index in ARGV of the next element to be scanned.
-   This is used for communication to and from the caller
-   and for communication between successive calls to `getopt'.
-
-   On entry to `getopt', zero means this is the first call; initialize.
-
-   When `getopt' returns EOF, this is the index of the first of the
-   non-option elements that the caller should itself scan.
-
-   Otherwise, `optind' communicates from one call to the next
-   how much of ARGV has been scanned so far.  */
-
-#ifndef WIN32
-extern int optind;
-#endif
-
-/* Callers store zero here to inhibit the error message `getopt' prints
-   for unrecognized options.  */
-
-#ifndef WIN32
-extern int opterr;
-#endif
-
-/* Set to an option character which was unrecognized.  */
-
-extern int optopt;
-
-/* Describe the long-named options requested by the application.
-   The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
-   of `struct option' terminated by an element containing a name which is
-   zero.
-
-   The field `has_arg' is:
-   no_argument         (or 0) if the option does not take an argument,
-   required_argument   (or 1) if the option requires an argument,
-   optional_argument   (or 2) if the option takes an optional argument.
-
-   If the field `flag' is not NULL, it points to a variable that is set
-   to the value given in the field `val' when the option is found, but
-   left unchanged if the option is not found.
-
-   To have a long-named option do something other than set an `int' to
-   a compiled-in constant, such as set a value from `optarg', set the
-   option's `flag' field to zero and its `val' field to a nonzero
-   value (the equivalent single-letter option character, if there is
-   one).  For long options that have a zero `flag' field, `getopt'
-   returns the contents of the `val' field.  */
-
-struct option
-{
-#if    __STDC__
-  const char *name;
-#else
-  char *name;
-#endif
-  /* has_arg can't be an enum because some compilers complain about
-     type mismatches in all the code that assumes it is an int.  */
-  int has_arg;
-  int *flag;
-  int val;
-};
-
-/* Names for the values of the `has_arg' field of `struct option'.  */
-
-#define        no_argument             0
-#define required_argument      1
-#define optional_argument      2
-
-#if __STDC__
-#if defined(__GNU_LIBRARY__)
-/* Many other libraries have conflicting prototypes for getopt, with
-   differences in the consts, in stdlib.h.  To avoid compilation
-   errors, only prototype getopt for the GNU C library.  */
-extern int getopt (int argc, char *const *argv, const char *shortopts);
-#else /* not __GNU_LIBRARY__ */
-extern int getopt ();
-#endif /* not __GNU_LIBRARY__ */
-extern int getopt_long (int argc, char *const *argv, const char *shortopts,
-                       const struct option *longopts, int *longind);
-extern int getopt_long_only (int argc, char *const *argv,
-                            const char *shortopts,
-                            const struct option *longopts, int *longind);
-
-/* Internal only.  Users should not call this directly.  */
-extern int _getopt_internal (int argc, char *const *argv,
-                            const char *shortopts,
-                            const struct option *longopts, int *longind,
-                            int long_only);
-#else /* not __STDC__ */
-extern int getopt ();
-extern int getopt_long ();
-extern int getopt_long_only ();
-
-extern int _getopt_internal ();
-#endif /* not __STDC__ */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _GETOPT_H */
diff --git a/win32/Src/getopt.c b/win32/Src/getopt.c
deleted file mode 100644 (file)
index be74ef0..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-/*
- * Copyright (c) 1987, 1993, 1994
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)getopt.c   8.3 (Berkeley) 4/27/95";
-#endif /* LIBC_SCCS and not lint */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-int    opterr = 1,             /* if error message should be printed */
-       optind = 1,             /* index into parent argv vector */
-       optopt,                 /* character checked for validity */
-       optreset;               /* reset getopt */
-char   *optarg;                /* argument associated with option */
-
-#define        BADCH   (int)'?'
-#define        BADARG  (int)':'
-#define        EMSG    ""
-
-/*
- * getopt --
- *     Parse argc/argv argument vector.
- */
-int
-getopt(nargc, nargv, ostr)
-       int nargc;
-       char * const *nargv;
-       const char *ostr;
-{
-       extern char *program_name;
-       static char *place = EMSG;              /* option letter processing */
-       char *oli;                              /* option letter list index */
-
-       if (optreset || !*place) {              /* update scanning pointer */
-               optreset = 0;
-               if (optind >= nargc || *(place = nargv[optind]) != '-') {
-                       place = EMSG;
-                       return (-1);
-               }
-               if (place[1] && *++place == '-') {      /* found "--" */
-                       ++optind;
-                       place = EMSG;
-                       return (-1);
-               }
-       }                                       /* option letter okay? */
-       if ((optopt = (int)*place++) == (int)':' ||
-           !(oli = strchr(ostr, optopt))) {
-               /*
-                * if the user didn't specify '-' as an option,
-                * assume it means -1.
-                */
-               if (optopt == (int)'-')
-                       return (-1);
-               if (!*place)
-                       ++optind;
-               if (opterr && *ostr != ':')
-                       (void)fprintf(stderr,
-                           "%s: illegal option -- %c\n", program_name, optopt);
-               return (BADCH);
-       }
-       if (*++oli != ':') {                    /* don't need argument */
-               optarg = NULL;
-               if (!*place)
-                       ++optind;
-       }
-       else {                                  /* need an argument */
-               if (*place)                     /* no white space */
-                       optarg = place;
-               else if (nargc <= ++optind) {   /* no arg */
-                       place = EMSG;
-                       if (*ostr == ':')
-                               return (BADARG);
-                       if (opterr)
-                               (void)fprintf(stderr,
-                                   "%s: option requires an argument -- %c\n",
-                                   program_name, optopt);
-                       return (BADCH);
-               }
-               else                            /* white space */
-                       optarg = nargv[optind];
-               place = EMSG;
-               ++optind;
-       }
-       return (optopt);                        /* dump back option letter */
-}
index bffdaaafc0d47ab7c18f14185889be121af4498f..d0504e3bf2378ebe50d9791be9ca470e2ffe7a1f 100644 (file)
@@ -39,6 +39,7 @@ OBJS = \
        ../../missing/datalinks.o \
        ../../missing/strsep.o \
        ../../missing/inet_pton.o \
+       ../../missing/getopt_long.o \
        ../../machdep.o \
        ../../oui.o \
        ../../parsenfsfh.o \
@@ -146,7 +147,6 @@ OBJS = \
        ../../smbutil.o \
        ../../tcpdump.o \
        ../../util.o \
-       ../../Win32/src/getopt.o \
        ../../cpack.o \
        ../../ipproto.o \
        ../../l2vpn.o \
index f3b35132ffcf577e4f3d5512b86c330927dd9c2c..20694511eecec58b5c65e1a216ad15d3cbeab4ae 100644 (file)
@@ -113,7 +113,7 @@ SOURCE=..\..\missing\dlnames.c
 # End Source File
 # Begin Source File
 
-SOURCE=..\Src\getopt.c
+SOURCE=..\..\missing\getopt.c
 # End Source File
 # Begin Source File
 
index d42d4bc4e1282e7957ed2659f48e7e2ff479c78d..734cee8e53d9c00e21e226cd2833a48a7fb4c92b 100644 (file)
                        </FileConfiguration>
                </File>
                <File
-                       RelativePath="..\Src\getopt.c"
+                       RelativePath="..\..\missing\getopt_long.c"
                        >
                        <FileConfiguration
                                Name="Debug|Win32"