From f033ca2882a1e30b4f03f10ef27840401a02cb4d Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 05:44:18 +0100 Subject: [PATCH 01/10] Put autoconf build tools in "build-aux". Require autoconf 2.69 or later. --- configure.ac | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index b43f04a..d21ef4e 100644 --- a/configure.ac +++ b/configure.ac @@ -1,9 +1,10 @@ dnl Process this file with autoconf to produce a configure script. AC_INIT([ike-scan],[1.9.6],[https://github.com/royhills/ike-scan]) -AC_PREREQ(2.61) +AC_PREREQ(2.69) AC_REVISION($Revision$) AC_CONFIG_SRCDIR([ike-scan.c]) +AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE AC_CONFIG_HEADERS([config.h]) From 3b6a40cae589b8a28e81b12b64caf1adf98e66ae Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 05:59:37 +0100 Subject: [PATCH 02/10] Require C99 compliant C compiler --- configure.ac | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index d21ef4e..1950953 100644 --- a/configure.ac +++ b/configure.ac @@ -27,8 +27,14 @@ AC_ARG_ENABLE(gcov, AC_MSG_RESULT(no) ] ) -dnl Check for programs. +# Checks for programs. AC_PROG_CC +# Ensure the C compiler supports the C99 standard. +if test "x$ac_cv_prog_cc_c99" = "xno"; then + AC_MSG_ERROR([C compiler does not support C99 standard]) +fi +# Add additional options if the C compiler identifies as GCC. +# This applies to Clang/LLVM in addition to GCC. if test -n "$GCC"; then AC_DEFINE([ATTRIBUTE_UNUSED], [__attribute__ ((__unused__))], [Define to the compiler's unused pragma]) From 0ea2e353f6a0a0e9f81576836322a5076fcd87f8 Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 06:33:22 +0100 Subject: [PATCH 03/10] Unconditionally include C89 & C99 standard headers and remove configure checks. Order header includes by C standard (C89, C99) and POSIX.1 issue number. --- configure.ac | 6 +++-- ike-scan.h | 70 +++++++++++++++++++++++++--------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/configure.ac b/configure.ac index 1950953..a489890 100644 --- a/configure.ac +++ b/configure.ac @@ -64,8 +64,10 @@ dnl We should only include these libraries if they are actually needed. AC_SEARCH_LIBS([gethostbyname], [nsl]) AC_SEARCH_LIBS([socket], [socket]) -dnl Checks for header files. -AC_CHECK_HEADERS([inttypes.h stdint.h arpa/inet.h netdb.h netinet/in.h netinet/tcp.h sys/socket.h sys/time.h unistd.h getopt.h signal.h sys/stat.h fcntl.h]) +# Check for C POSIX library header files +AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h netinet/tcp.h sys/socket.h sys/time.h unistd.h sys/stat.h fcntl.h]) +# Check for other required header files +AC_CHECK_HEADERS([getopt.h]) dnl Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T diff --git a/ike-scan.h b/ike-scan.h index f8021dd..0c34e21 100644 --- a/ike-scan.h +++ b/ike-scan.h @@ -57,75 +57,73 @@ #include #include #include +#include /* For TCP connect() timeout using alarm */ -#include /* FreeBSD needs explicit include for sys/types.h */ - -/* Integer types */ -#ifdef HAVE_INTTYPES_H -#include -#else -#ifdef HAVE_STDINT_H +/* C99 standard headers */ #include -#endif -#endif + +#include /* FreeBSD needs explicit include for sys/types.h */ #ifdef __CYGWIN__ #include /* Include windows.h if compiling under Cygwin */ #endif +/* headers first defined in POSIX-1 issue 1 */ #ifdef HAVE_UNISTD_H #include #endif -#ifdef HAVE_NETDB_H -#include +#ifdef HAVE_FCNTL_H +#include #endif -#ifdef HAVE_GETOPT_H -#include -#else -/* Include getopt.h for the sake of getopt_long. - We don't need the declaration of getopt, and it could conflict - with something from a system header file, so effectively nullify that. */ -#define getopt getopt_loser -#include "getopt.h" -#undef getopt +#ifdef HAVE_SYS_STAT_H +#include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif +/* headers first defined in POSIX.1 issue 4 */ -#ifdef HAVE_NETINET_TCP_H -#include +#ifdef HAVE_REGEX_H +#include /* Posix regular expression support */ #endif #ifdef HAVE_SYS_TIME_H #include #endif -#ifdef HAVE_SYS_SOCKET_H -#include /* For struct sockaddr */ +/* headers first defined in POSIX.1 issue 6 */ + +#ifdef HAVE_NETDB_H +#include +#endif + +#ifdef HAVE_NETINET_IN_H +#include #endif #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_SIGNAL_H -#include /* For TCP connect() timeout using alarm */ +#ifdef HAVE_NETINET_TCP_H +#include #endif -#ifdef HAVE_REGEX_H -#include /* Posix regular expression support */ +#ifdef HAVE_SYS_SOCKET_H +#include /* For struct sockaddr */ #endif -#ifdef HAVE_SYS_STAT_H -#include -#endif +/* Other system headers */ -#ifdef HAVE_FCNTL_H -#include +#ifdef HAVE_GETOPT_H +#include +#else +/* Include getopt.h for the sake of getopt_long. + We don't need the declaration of getopt, and it could conflict + with something from a system header file, so effectively nullify that. */ +#define getopt getopt_loser +#include "getopt.h" +#undef getopt #endif #ifdef HAVE_OPENSSL From 1a85003c262bee2ce262b9c9b4a4447b596178e5 Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 06:40:35 +0100 Subject: [PATCH 04/10] Removed sys/types.h include as required types should be in POSIX.1 unistd.h. --- ike-scan.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/ike-scan.h b/ike-scan.h index 0c34e21..2dc0b44 100644 --- a/ike-scan.h +++ b/ike-scan.h @@ -62,8 +62,6 @@ /* C99 standard headers */ #include -#include /* FreeBSD needs explicit include for sys/types.h */ - #ifdef __CYGWIN__ #include /* Include windows.h if compiling under Cygwin */ #endif From 1285c1732e8a0561157b1de23768b076684addcd Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 07:01:21 +0100 Subject: [PATCH 05/10] Use B .Sittler's getopt implementation for systems lacking getopt_long_only. Most systems support getopt_long_only, but NetBSD doesn't as of v9.3. --- configure.ac | 13 +- getopt.c | 1277 -------------------------------------------------- getopt.h | 181 ------- getopt1.c | 196 -------- my_getopt.c | 281 +++++++++++ my_getopt.h | 89 ++++ 6 files changed, 376 insertions(+), 1661 deletions(-) delete mode 100644 getopt.c delete mode 100644 getopt.h delete mode 100644 getopt1.c create mode 100644 my_getopt.c create mode 100644 my_getopt.h diff --git a/configure.ac b/configure.ac index a489890..1fecd63 100644 --- a/configure.ac +++ b/configure.ac @@ -154,13 +154,12 @@ else AC_DEFINE(HAVE_REGEX_H, 1, [Define to 1 if you have posix regex support]) fi -dnl GNU systems e.g. Linux have getopt_long_only, but many other systems -dnl e.g. FreeBSD 4.3 and Solaris 8 do not. For systems that don't have it, -dnl use the GNU getopt sources (obtained from glibc). -AC_CHECK_FUNC([getopt_long_only], , - [ AC_LIBOBJ(getopt) - AC_LIBOBJ(getopt1) - AC_LIBSOURCE(getopt.h) ]) +# Linux and most BSD systems have getopt_long_only, but NetBSD doesn't (as of v9.3). +# Use the my_getopt.c implementation for systems that don't have it. +AC_CHECK_FUNC([getopt_long_only], + [AC_DEFINE(HAVE_GETOPT_LONG_ONLY, 1, [Define to 1 if the C library includes getopt_long_only])], + [ AC_LIBOBJ([my_getopt]) + AC_LIBSOURCE([my_getopt.h]) ]) dnl Do we have inet_aton? Most systems do, but some e.g. Solaris don't dnl If we don't have it, then use Russ Allbery's implementation as a diff --git a/getopt.c b/getopt.c deleted file mode 100644 index 289d137..0000000 --- a/getopt.c +++ /dev/null @@ -1,1277 +0,0 @@ -/* Getopt for GNU. - NOTE: getopt is now part of the C library, so if you don't know what - "Keep this file name-space clean" means, talk to drepper@gnu.org - before changing it! - Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002 - 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 Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -/* This tells Alpha OSF/1 not to define a getopt prototype in . - Ditto for AIX 3.2 and . */ -#ifndef _NO_PROTO -# define _NO_PROTO -#endif - -#ifdef HAVE_CONFIG_H -# include -#endif - -#if !defined __STDC__ || !__STDC__ -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -# ifndef const -# define const -# endif -#endif - -#include - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#define GETOPT_INTERFACE_VERSION 2 -#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 -# include -# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION -# define ELIDE_CODE -# endif -#endif - -#ifndef ELIDE_CODE - - -/* This needs to come after some library #include - to get __GNU_LIBRARY__ defined. */ -#ifdef __GNU_LIBRARY__ -/* Don't include stdlib.h for non-GNU C libraries because some of them - contain conflicting prototypes for getopt. */ -# include -# include -#endif /* GNU C library. */ - -#ifdef VMS -# include -# if HAVE_STRING_H - 0 -# include -# endif -#endif - -#ifndef _ -/* This is for other GNU distributions with internationalized messages. */ -# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC -# include -# ifndef _ -# define _(msgid) gettext (msgid) -# endif -# else -# define _(msgid) (msgid) -# endif -# if defined _LIBC && defined USE_IN_LIBIO -# include -# endif -#endif - -#ifndef attribute_hidden -# define attribute_hidden -#endif - -/* This version of `getopt' appears to the caller like standard Unix `getopt' - but it behaves differently for the user, since it allows the user - to intersperse the options with the other arguments. - - As `getopt' works, it permutes the elements of ARGV so that, - when it is done, all the options precede everything else. Thus - all application programs are extended to handle flexible argument order. - - Setting the environment variable POSIXLY_CORRECT disables permutation. - Then the behavior is completely standard. - - GNU application programs can use a third alternative mode in which - they can distinguish the relative order of options and other arguments. */ - -#include "getopt.h" - -/* 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. */ - -char *optarg; - -/* 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 -1, 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. */ - -/* 1003.2 says this must be 1 before any call. */ -int optind = 1; - -/* Formerly, initialization of getopt depended on optind==0, which - causes problems with re-calling getopt as programs generally don't - know that. */ - -int __getopt_initialized attribute_hidden; - -/* The next char to be scanned in the option-element - in which the last option character we returned was found. - This allows us to pick up the scan where we left off. - - If this is zero, or a null string, it means resume the scan - by advancing to the next ARGV-element. */ - -static char *nextchar; - -/* Callers store zero here to inhibit the error message - for unrecognized options. */ - -int opterr = 1; - -/* Set to an option character which was unrecognized. - This must be initialized on some systems to avoid linking in the - system's own getopt implementation. */ - -int optopt = '?'; - -/* Describe how to deal with options that follow non-option ARGV-elements. - - If the caller did not specify anything, - the default is REQUIRE_ORDER if the environment variable - POSIXLY_CORRECT is defined, PERMUTE otherwise. - - REQUIRE_ORDER means don't recognize them as options; - stop option processing when the first non-option is seen. - This is what Unix does. - This mode of operation is selected by either setting the environment - variable POSIXLY_CORRECT, or using `+' as the first character - of the list of option characters. - - PERMUTE is the default. We permute the contents of ARGV as we scan, - so that eventually all the non-options are at the end. This allows options - to be given in any order, even with programs that were not written to - expect this. - - RETURN_IN_ORDER is an option available to programs that were written - to expect options and other ARGV-elements in any order and that care about - the ordering of the two. We describe each non-option ARGV-element - as if it were the argument of an option with character code 1. - Using `-' as the first character of the list of option characters - selects this mode of operation. - - The special argument `--' forces an end of option-scanning regardless - of the value of `ordering'. In the case of RETURN_IN_ORDER, only - `--' can cause `getopt' to return -1 with `optind' != ARGC. */ - -static enum -{ - REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER -} ordering; - -/* Value of POSIXLY_CORRECT environment variable. */ -static char *posixly_correct; - -#ifdef __GNU_LIBRARY__ -/* We want to avoid inclusion of string.h with non-GNU libraries - because there are many ways it can cause trouble. - On some systems, it contains special magic macros that don't work - in GCC. */ -# include -# define my_index strchr -#else - -# if HAVE_STRING_H -# include -# else -# include -# endif - -/* Avoid depending on library functions or files - whose names are inconsistent. */ - -#ifndef getenv -extern char *getenv (); -#endif - -static char * -my_index (str, chr) - const char *str; - int chr; -{ - while (*str) - { - if (*str == chr) - return (char *) str; - str++; - } - return 0; -} - -/* If using GCC, we can safely declare strlen this way. - If not using GCC, it is ok not to declare it. */ -#ifdef __GNUC__ -/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. - That was relevant to code that was here before. */ -# if (!defined __STDC__ || !__STDC__) && !defined strlen -/* gcc with -traditional declares the built-in strlen to return int, - and has done so at least since version 2.4.5. -- rms. */ -extern int strlen (const char *); -# endif /* not __STDC__ */ -#endif /* __GNUC__ */ - -#endif /* not __GNU_LIBRARY__ */ - -/* Handle permutation of arguments. */ - -/* Describe the part of ARGV that contains non-options that have - been skipped. `first_nonopt' is the index in ARGV of the first of them; - `last_nonopt' is the index after the last of them. */ - -static int first_nonopt; -static int last_nonopt; - -#ifdef _LIBC -/* Stored original parameters. - XXX This is no good solution. We should rather copy the args so - that we can compare them later. But we must not use malloc(3). */ -extern int __libc_argc; -extern char **__libc_argv; - -/* Bash 2.0 gives us an environment variable containing flags - indicating ARGV elements that should not be considered arguments. */ - -# ifdef USE_NONOPTION_FLAGS -/* Defined in getopt_init.c */ -extern char *__getopt_nonoption_flags; - -static int nonoption_flags_max_len; -static int nonoption_flags_len; -# endif - -# ifdef USE_NONOPTION_FLAGS -# define SWAP_FLAGS(ch1, ch2) \ - if (nonoption_flags_len > 0) \ - { \ - char __tmp = __getopt_nonoption_flags[ch1]; \ - __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ - __getopt_nonoption_flags[ch2] = __tmp; \ - } -# else -# define SWAP_FLAGS(ch1, ch2) -# endif -#else /* !_LIBC */ -# define SWAP_FLAGS(ch1, ch2) -#endif /* _LIBC */ - -/* Exchange two adjacent subsequences of ARGV. - One subsequence is elements [first_nonopt,last_nonopt) - which contains all the non-options that have been skipped so far. - The other is elements [last_nonopt,optind), which contains all - the options processed since those non-options were skipped. - - `first_nonopt' and `last_nonopt' are relocated so that they describe - the new indices of the non-options in ARGV after they are moved. */ - -#if defined __STDC__ && __STDC__ -static void exchange (char **); -#endif - -static void -exchange (argv) - char **argv; -{ - int bottom = first_nonopt; - int middle = last_nonopt; - int top = optind; - char *tem; - - /* Exchange the shorter segment with the far end of the longer segment. - That puts the shorter segment into the right place. - It leaves the longer segment in the right place overall, - but it consists of two parts that need to be swapped next. */ - -#if defined _LIBC && defined USE_NONOPTION_FLAGS - /* First make sure the handling of the `__getopt_nonoption_flags' - string can work normally. Our top argument must be in the range - of the string. */ - if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) - { - /* We must extend the array. The user plays games with us and - presents new arguments. */ - char *new_str = malloc (top + 1); - if (new_str == NULL) - nonoption_flags_len = nonoption_flags_max_len = 0; - else - { - memset (__mempcpy (new_str, __getopt_nonoption_flags, - nonoption_flags_max_len), - '\0', top + 1 - nonoption_flags_max_len); - nonoption_flags_max_len = top + 1; - __getopt_nonoption_flags = new_str; - } - } -#endif - - while (top > middle && middle > bottom) - { - if (top - middle > middle - bottom) - { - /* Bottom segment is the short one. */ - int len = middle - bottom; - register int i; - - /* Swap it with the top part of the top segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[top - (middle - bottom) + i]; - argv[top - (middle - bottom) + i] = tem; - SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); - } - /* Exclude the moved bottom segment from further swapping. */ - top -= len; - } - else - { - /* Top segment is the short one. */ - int len = top - middle; - register int i; - - /* Swap it with the bottom part of the bottom segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[middle + i]; - argv[middle + i] = tem; - SWAP_FLAGS (bottom + i, middle + i); - } - /* Exclude the moved top segment from further swapping. */ - bottom += len; - } - } - - /* Update records for the slots the non-options now occupy. */ - - first_nonopt += (optind - last_nonopt); - last_nonopt = optind; -} - -/* Initialize the internal data when the first call is made. */ - -#if defined __STDC__ && __STDC__ -static const char *_getopt_initialize (int, char *const *, const char *); -#endif -static const char * -_getopt_initialize (argc, argv, optstring) - int argc; - char *const *argv; - const char *optstring; -{ - /* Start processing options with ARGV-element 1 (since ARGV-element 0 - is the program name); the sequence of previously skipped - non-option ARGV-elements is empty. */ - - first_nonopt = last_nonopt = optind; - - nextchar = NULL; - - posixly_correct = getenv ("POSIXLY_CORRECT"); - - /* Determine how to handle the ordering of options and nonoptions. */ - - if (optstring[0] == '-') - { - ordering = RETURN_IN_ORDER; - ++optstring; - } - else if (optstring[0] == '+') - { - ordering = REQUIRE_ORDER; - ++optstring; - } - else if (posixly_correct != NULL) - ordering = REQUIRE_ORDER; - else - ordering = PERMUTE; - -#if defined _LIBC && defined USE_NONOPTION_FLAGS - if (posixly_correct == NULL - && argc == __libc_argc && argv == __libc_argv) - { - if (nonoption_flags_max_len == 0) - { - if (__getopt_nonoption_flags == NULL - || __getopt_nonoption_flags[0] == '\0') - nonoption_flags_max_len = -1; - else - { - const char *orig_str = __getopt_nonoption_flags; - int len = nonoption_flags_max_len = strlen (orig_str); - if (nonoption_flags_max_len < argc) - nonoption_flags_max_len = argc; - __getopt_nonoption_flags = - (char *) malloc (nonoption_flags_max_len); - if (__getopt_nonoption_flags == NULL) - nonoption_flags_max_len = -1; - else - memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), - '\0', nonoption_flags_max_len - len); - } - } - nonoption_flags_len = nonoption_flags_max_len; - } - else - nonoption_flags_len = 0; -#endif - - return optstring; -} - -/* Scan elements of ARGV (whose length is ARGC) for option characters - given in OPTSTRING. - - If an element of ARGV starts with '-', and is not exactly "-" or "--", - then it is an option element. The characters of this element - (aside from the initial '-') are option characters. If `getopt' - is called repeatedly, it returns successively each of the option characters - from each of the option elements. - - If `getopt' finds another option character, it returns that character, - updating `optind' and `nextchar' so that the next call to `getopt' can - resume the scan with the following option character or ARGV-element. - - If there are no more option characters, `getopt' returns -1. - Then `optind' is the index in ARGV of the first ARGV-element - that is not an option. (The ARGV-elements have been permuted - so that those that are not options now come last.) - - OPTSTRING is a string containing the legitimate option characters. - If an option character is seen that is not listed in OPTSTRING, - return '?' after printing an error message. If you set `opterr' to - zero, the error message is suppressed but we still return '?'. - - If a char in OPTSTRING is followed by a colon, that means it wants an arg, - so the following text in the same ARGV-element, or the text of the following - ARGV-element, is returned in `optarg'. Two colons mean an option that - wants an optional arg; if there is text in the current ARGV-element, - it is returned in `optarg', otherwise `optarg' is set to zero. - - If OPTSTRING starts with `-' or `+', it requests different methods of - handling the non-option ARGV-elements. - See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. - - Long-named options begin with `--' instead of `-'. - Their names may be abbreviated as long as the abbreviation is unique - or is an exact match for some defined option. If they have an - argument, it follows the option name in the same ARGV-element, separated - from the option name by a `=', or else the in next ARGV-element. - When `getopt' finds a long-named option, it returns 0 if that option's - `flag' field is nonzero, the value of the option's `val' field - if the `flag' field is zero. - - The elements of ARGV aren't really const, because we permute them. - But we pretend they're const in the prototype to be compatible - with other systems. - - LONGOPTS is a vector of `struct option' terminated by an - element containing a name which is zero. - - LONGIND returns the index in LONGOPT of the long-named option found. - It is only valid when a long-named option has been found by the most - recent call. - - If LONG_ONLY is nonzero, '-' as well as '--' can introduce - long-named options. */ - -int -_getopt_internal (argc, argv, optstring, longopts, longind, long_only) - int argc; - char *const *argv; - const char *optstring; - const struct option *longopts; - int *longind; - int long_only; -{ - int print_errors = opterr; - if (optstring[0] == ':') - print_errors = 0; - - if (argc < 1) - return -1; - - optarg = NULL; - - if (optind == 0 || !__getopt_initialized) - { - if (optind == 0) - optind = 1; /* Don't scan ARGV[0], the program name. */ - optstring = _getopt_initialize (argc, argv, optstring); - __getopt_initialized = 1; - } - - /* Test whether ARGV[optind] points to a non-option argument. - Either it does not have option syntax, or there is an environment flag - from the shell indicating it is not an option. The later information - is only used when the used in the GNU libc. */ -#if defined _LIBC && defined USE_NONOPTION_FLAGS -# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ - || (optind < nonoption_flags_len \ - && __getopt_nonoption_flags[optind] == '1')) -#else -# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') -#endif - - if (nextchar == NULL || *nextchar == '\0') - { - /* Advance to the next ARGV-element. */ - - /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been - moved back by the user (who may also have changed the arguments). */ - if (last_nonopt > optind) - last_nonopt = optind; - if (first_nonopt > optind) - first_nonopt = optind; - - if (ordering == PERMUTE) - { - /* If we have just processed some options following some non-options, - exchange them so that the options come first. */ - - if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange ((char **) argv); - else if (last_nonopt != optind) - first_nonopt = optind; - - /* Skip any additional non-options - and extend the range of non-options previously skipped. */ - - while (optind < argc && NONOPTION_P) - optind++; - last_nonopt = optind; - } - - /* The special ARGV-element `--' means premature end of options. - Skip it like a null option, - then exchange with previous non-options as if it were an option, - then skip everything else like a non-option. */ - - if (optind != argc && !strcmp (argv[optind], "--")) - { - optind++; - - if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange ((char **) argv); - else if (first_nonopt == last_nonopt) - first_nonopt = optind; - last_nonopt = argc; - - optind = argc; - } - - /* If we have done all the ARGV-elements, stop the scan - and back over any non-options that we skipped and permuted. */ - - if (optind == argc) - { - /* Set the next-arg-index to point at the non-options - that we previously skipped, so the caller will digest them. */ - if (first_nonopt != last_nonopt) - optind = first_nonopt; - return -1; - } - - /* If we have come to a non-option and did not permute it, - either stop the scan or describe it to the caller and pass it by. */ - - if (NONOPTION_P) - { - if (ordering == REQUIRE_ORDER) - return -1; - optarg = argv[optind++]; - return 1; - } - - /* We have found another option-ARGV-element. - Skip the initial punctuation. */ - - nextchar = (argv[optind] + 1 - + (longopts != NULL && argv[optind][1] == '-')); - } - - /* Decode the current option-ARGV-element. */ - - /* Check whether the ARGV-element is a long option. - - If long_only and the ARGV-element has the form "-f", where f is - a valid short option, don't consider it an abbreviated form of - a long option that starts with f. Otherwise there would be no - way to give the -f short option. - - On the other hand, if there's a long option "fubar" and - the ARGV-element is "-fu", do consider that an abbreviation of - the long option, just like "--fu", and not "-f" with arg "u". - - This distinction seems to be the most useful approach. */ - - if (longopts != NULL - && (argv[optind][1] == '-' - || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) - { - char *nameend; - const struct option *p; - const struct option *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound = -1; - int option_index; - - for (nameend = nextchar; *nameend && *nameend != '='; nameend++) - /* Do nothing. */ ; - - /* Test all long options for either exact match - or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, nextchar, nameend - nextchar)) - { - if ((unsigned int) (nameend - nextchar) - == (unsigned int) strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else if (long_only - || pfound->has_arg != p->has_arg - || pfound->flag != p->flag - || pfound->val != p->val) - /* Second or later nonexact match found. */ - ambig = 1; - } - - if (ambig && !exact) - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), - argv[0], argv[optind]) >= 0) - { - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, _("%s: option `%s' is ambiguous\n"), - argv[0], argv[optind]); -#endif - } - nextchar += strlen (nextchar); - optind++; - optopt = 0; - return '?'; - } - - if (pfound != NULL) - { - option_index = indfound; - optind++; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - allow it to be used on enums. */ - if (pfound->has_arg) - optarg = nameend + 1; - else - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - int n; -#endif - - if (argv[optind - 1][1] == '-') - { - /* --option */ -#if defined _LIBC && defined USE_IN_LIBIO - n = __asprintf (&buf, _("\ -%s: option `--%s' doesn't allow an argument\n"), - argv[0], pfound->name); -#else - fprintf (stderr, _("\ -%s: option `--%s' doesn't allow an argument\n"), - argv[0], pfound->name); -#endif - } - else - { - /* +option or -option */ -#if defined _LIBC && defined USE_IN_LIBIO - n = __asprintf (&buf, _("\ -%s: option `%c%s' doesn't allow an argument\n"), - argv[0], argv[optind - 1][0], - pfound->name); -#else - fprintf (stderr, _("\ -%s: option `%c%s' doesn't allow an argument\n"), - argv[0], argv[optind - 1][0], pfound->name); -#endif - } - -#if defined _LIBC && defined USE_IN_LIBIO - if (n >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#endif - } - - nextchar += strlen (nextchar); - - optopt = pfound->val; - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (optind < argc) - optarg = argv[optind++]; - else - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, _("\ -%s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]) >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, - _("%s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]); -#endif - } - nextchar += strlen (nextchar); - optopt = pfound->val; - return optstring[0] == ':' ? ':' : '?'; - } - } - nextchar += strlen (nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } - - /* Can't find it as a long option. If this is not getopt_long_only, - or the option starts with '--' or is not a valid short - option, then it's an error. - Otherwise interpret it as a short option. */ - if (!long_only || argv[optind][1] == '-' - || my_index (optstring, *nextchar) == NULL) - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - int n; -#endif - - if (argv[optind][1] == '-') - { - /* --option */ -#if defined _LIBC && defined USE_IN_LIBIO - n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), - argv[0], nextchar); -#else - fprintf (stderr, _("%s: unrecognized option `--%s'\n"), - argv[0], nextchar); -#endif - } - else - { - /* +option or -option */ -#if defined _LIBC && defined USE_IN_LIBIO - n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), - argv[0], argv[optind][0], nextchar); -#else - fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), - argv[0], argv[optind][0], nextchar); -#endif - } - -#if defined _LIBC && defined USE_IN_LIBIO - if (n >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#endif - } - nextchar = (char *) ""; - optind++; - optopt = 0; - return '?'; - } - } - - /* Look at and handle the next short option-character. */ - - { - char c = *nextchar++; - char *temp = my_index (optstring, c); - - /* Increment `optind' when we start to process its last character. */ - if (*nextchar == '\0') - ++optind; - - if (temp == NULL || c == ':') - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - int n; -#endif - - if (posixly_correct) - { - /* 1003.2 specifies the format of this message. */ -#if defined _LIBC && defined USE_IN_LIBIO - n = __asprintf (&buf, _("%s: illegal option -- %c\n"), - argv[0], c); -#else - fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); -#endif - } - else - { -#if defined _LIBC && defined USE_IN_LIBIO - n = __asprintf (&buf, _("%s: invalid option -- %c\n"), - argv[0], c); -#else - fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); -#endif - } - -#if defined _LIBC && defined USE_IN_LIBIO - if (n >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#endif - } - optopt = c; - return '?'; - } - /* Convenience. Treat POSIX -W foo same as long option --foo */ - if (temp[0] == 'W' && temp[1] == ';') - { - char *nameend; - const struct option *p; - const struct option *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound = 0; - int option_index; - - /* This is an option that requires an argument. */ - if (*nextchar != '\0') - { - optarg = nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - we must advance to the next element now. */ - optind++; - } - else if (optind == argc) - { - if (print_errors) - { - /* 1003.2 specifies the format of this message. */ -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, - _("%s: option requires an argument -- %c\n"), - argv[0], c) >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, _("%s: option requires an argument -- %c\n"), - argv[0], c); -#endif - } - optopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - return c; - } - else - /* We already incremented `optind' once; - increment it again when taking next ARGV-elt as argument. */ - optarg = argv[optind++]; - - /* optarg is now the argument, see if it's in the - table of longopts. */ - - for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) - /* Do nothing. */ ; - - /* Test all long options for either exact match - or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, nextchar, nameend - nextchar)) - { - if ((unsigned int) (nameend - nextchar) == strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else - /* Second or later nonexact match found. */ - ambig = 1; - } - if (ambig && !exact) - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), - argv[0], argv[optind]) >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), - argv[0], argv[optind]); -#endif - } - nextchar += strlen (nextchar); - optind++; - return '?'; - } - if (pfound != NULL) - { - option_index = indfound; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - allow it to be used on enums. */ - if (pfound->has_arg) - optarg = nameend + 1; - else - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, _("\ -%s: option `-W %s' doesn't allow an argument\n"), - argv[0], pfound->name) >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, _("\ -%s: option `-W %s' doesn't allow an argument\n"), - argv[0], pfound->name); -#endif - } - - nextchar += strlen (nextchar); - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (optind < argc) - optarg = argv[optind++]; - else - { - if (print_errors) - { -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, _("\ -%s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]) >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, - _("%s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]); -#endif - } - nextchar += strlen (nextchar); - return optstring[0] == ':' ? ':' : '?'; - } - } - nextchar += strlen (nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } - nextchar = NULL; - return 'W'; /* Let the application handle it. */ - } - if (temp[1] == ':') - { - if (temp[2] == ':') - { - /* This is an option that accepts an argument optionally. */ - if (*nextchar != '\0') - { - optarg = nextchar; - optind++; - } - else - optarg = NULL; - nextchar = NULL; - } - else - { - /* This is an option that requires an argument. */ - if (*nextchar != '\0') - { - optarg = nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - we must advance to the next element now. */ - optind++; - } - else if (optind == argc) - { - if (print_errors) - { - /* 1003.2 specifies the format of this message. */ -#if defined _LIBC && defined USE_IN_LIBIO - char *buf; - - if (__asprintf (&buf, _("\ -%s: option requires an argument -- %c\n"), - argv[0], c) >= 0) - { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); - } -#else - fprintf (stderr, - _("%s: option requires an argument -- %c\n"), - argv[0], c); -#endif - } - optopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - } - else - /* We already incremented `optind' once; - increment it again when taking next ARGV-elt as argument. */ - optarg = argv[optind++]; - nextchar = NULL; - } - } - return c; - } -} - -int -getopt (argc, argv, optstring) - int argc; - char *const *argv; - const char *optstring; -{ - return _getopt_internal (argc, argv, optstring, - (const struct option *) 0, - (int *) 0, - 0); -} - -#endif /* Not ELIDE_CODE. */ - -#ifdef TEST - -/* Compile with -DTEST to make an executable for use in testing - the above definition of `getopt'. */ - -int -main (argc, argv) - int argc; - char **argv; -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - - c = getopt (argc, argv, "abc:d:0123456789"); - if (c == -1) - break; - - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value `%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ diff --git a/getopt.h b/getopt.h deleted file mode 100644 index 4283c35..0000000 --- a/getopt.h +++ /dev/null @@ -1,181 +0,0 @@ -/* Declarations for getopt. - Copyright (C) 1989-1994, 1996-1999, 2001 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 Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#ifndef _GETOPT_H - -#ifndef __need_getopt -# define _GETOPT_H 1 -#endif - -/* If __GNU_LIBRARY__ is not already defined, either we are being used - standalone, or this is the first header included in the source file. - If we are being used with glibc, we need to include , but - that does not exist if we are standalone. So: if __GNU_LIBRARY__ is - not defined, include , which will pull in for us - if it's from glibc. (Why ctype.h? It's guaranteed to exist and it - doesn't flood the namespace with stuff the way some other headers do.) */ -#if !defined __GNU_LIBRARY__ -# include -#endif - -#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. */ - -extern char *optarg; - -/* 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 -1, 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. */ - -extern int optind; - -/* Callers store zero here to inhibit the error message `getopt' prints - for unrecognized options. */ - -extern int opterr; - -/* Set to an option character which was unrecognized. */ - -extern int optopt; - -#ifndef __need_getopt -/* 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 (defined __STDC__ && __STDC__) || defined __cplusplus - 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 -#endif /* need getopt */ - - -/* Get definitions and prototypes for functions to process the - arguments in ARGV (ARGC of them, minus the program name) for - options given in OPTS. - - Return the option character from OPTS just read. Return -1 when - there are no more options. For unrecognized options, or options - missing arguments, `optopt' is set to the option letter, and '?' is - returned. - - The OPTS string is a list of characters which are recognized option - letters, optionally followed by colons, specifying that that letter - takes an argument, to be placed in `optarg'. - - If a letter in OPTS is followed by two colons, its argument is - optional. This behavior is specific to the GNU `getopt'. - - The argument `--' causes premature termination of argument - scanning, explicitly telling `getopt' that there are no more - options. - - If OPTS begins with `--', then non-option arguments are treated as - arguments to the option '\0'. This behavior is specific to the GNU - `getopt'. */ - -#if (defined __STDC__ && __STDC__) || defined __cplusplus -# ifdef __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 /* __GNU_LIBRARY__ */ - -# ifndef __need_getopt -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); -# endif -#else /* not __STDC__ */ -extern int getopt (); -# ifndef __need_getopt -extern int getopt_long (); -extern int getopt_long_only (); - -extern int _getopt_internal (); -# endif -#endif /* __STDC__ */ - -#ifdef __cplusplus -} -#endif - -/* Make sure we later can get all the definitions and declarations. */ -#undef __need_getopt - -#endif /* getopt.h */ diff --git a/getopt1.c b/getopt1.c deleted file mode 100644 index ad06cc7..0000000 --- a/getopt1.c +++ /dev/null @@ -1,196 +0,0 @@ -/* getopt_long and getopt_long_only entry points for GNU getopt. - Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 - 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 Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef _LIBC -# include -#else -# include "getopt.h" -#endif - -#if !defined __STDC__ || !__STDC__ -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -#ifndef const -#define const -#endif -#endif - -#include - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#define GETOPT_INTERFACE_VERSION 2 -#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 -#include -#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION -#define ELIDE_CODE -#endif -#endif - -#ifndef ELIDE_CODE - - -/* This needs to come after some library #include - to get __GNU_LIBRARY__ defined. */ -#ifdef __GNU_LIBRARY__ -#include -#endif - -#ifndef NULL -#define NULL 0 -#endif - -int -getopt_long (argc, argv, options, long_options, opt_index) - int argc; - char *const *argv; - const char *options; - const struct option *long_options; - int *opt_index; -{ - return _getopt_internal (argc, argv, options, long_options, opt_index, 0); -} - -/* Like getopt_long, but '-' as well as '--' can indicate a long option. - If an option that starts with '-' (not '--') doesn't match a long option, - but does match a short option, it is parsed as a short option - instead. */ - -int -getopt_long_only (argc, argv, options, long_options, opt_index) - int argc; - char *const *argv; - const char *options; - const struct option *long_options; - int *opt_index; -{ - return _getopt_internal (argc, argv, options, long_options, opt_index, 1); -} - -# ifdef _LIBC -libc_hidden_def (getopt_long) -libc_hidden_def (getopt_long_only) -# endif - -#endif /* Not ELIDE_CODE. */ - -#ifdef TEST - -#include - -int -main (argc, argv) - int argc; - char **argv; -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - int option_index = 0; - static struct option long_options[] = - { - {"add", 1, 0, 0}, - {"append", 0, 0, 0}, - {"delete", 1, 0, 0}, - {"verbose", 0, 0, 0}, - {"create", 0, 0, 0}, - {"file", 1, 0, 0}, - {0, 0, 0, 0} - }; - - c = getopt_long (argc, argv, "abc:d:0123456789", - long_options, &option_index); - if (c == -1) - break; - - switch (c) - { - case 0: - printf ("option %s", long_options[option_index].name); - if (optarg) - printf (" with arg %s", optarg); - printf ("\n"); - break; - - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value `%s'\n", optarg); - break; - - case 'd': - printf ("option d with value `%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ diff --git a/my_getopt.c b/my_getopt.c new file mode 100644 index 0000000..5e9c214 --- /dev/null +++ b/my_getopt.c @@ -0,0 +1,281 @@ +/* + * my_getopt.c - my re-implementation of getopt. + * Copyright 1997, 2000, 2001, 2002, 2006, Benjamin Sittler + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include "my_getopt.h" + +int my_optind=1, my_opterr=1, my_optopt=0; +char *my_optarg=0; + +/* reset argument parser to start-up values */ +int my_getopt_reset(void) +{ + my_optind = 1; + my_opterr = 1; + my_optopt = 0; + my_optarg = 0; + return 0; +} + +/* this is the plain old UNIX getopt, with GNU-style extensions. */ +/* if you're porting some piece of UNIX software, this is all you need. */ +/* this supports GNU-style permution and optional arguments */ + +int my_getopt(int argc, char * argv[], const char *opts) +{ + static int charind=0; + const char *s; + char mode, colon_mode; + int off = 0, opt = -1; + + if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+'; + else { + if((colon_mode = *opts) == ':') off ++; + if(((mode = opts[off]) == '+') || (mode == '-')) { + off++; + if((colon_mode != ':') && ((colon_mode = opts[off]) == ':')) + off ++; + } + } + my_optarg = 0; + if(charind) { + my_optopt = argv[my_optind][charind]; + for(s=opts+off; *s; s++) if(my_optopt == *s) { + charind++; + if((*(++s) == ':') || ((my_optopt == 'W') && (*s == ';'))) { + if(argv[my_optind][charind]) { + my_optarg = &(argv[my_optind++][charind]); + charind = 0; + } else if(*(++s) != ':') { + charind = 0; + if(++my_optind >= argc) { + if(my_opterr) fprintf(stderr, + "%s: option requires an argument -- %c\n", + argv[0], my_optopt); + opt = (colon_mode == ':') ? ':' : '?'; + goto my_getopt_ok; + } + my_optarg = argv[my_optind++]; + } + } + opt = my_optopt; + goto my_getopt_ok; + } + if(my_opterr) fprintf(stderr, + "%s: illegal option -- %c\n", + argv[0], my_optopt); + opt = '?'; + if(argv[my_optind][++charind] == '\0') { + my_optind++; + charind = 0; + } + my_getopt_ok: + if(charind && ! argv[my_optind][charind]) { + my_optind++; + charind = 0; + } + } else if((my_optind >= argc) || + ((argv[my_optind][0] == '-') && + (argv[my_optind][1] == '-') && + (argv[my_optind][2] == '\0'))) { + my_optind++; + opt = -1; + } else if((argv[my_optind][0] != '-') || + (argv[my_optind][1] == '\0')) { + char *tmp; + int i, j, k; + + if(mode == '+') opt = -1; + else if(mode == '-') { + my_optarg = argv[my_optind++]; + charind = 0; + opt = 1; + } else { + for(i=j=my_optind; i j) { + tmp=argv[--i]; + for(k=i; k+1 argc) my_optind = argc; + return opt; +} + +/* this is the extended getopt_long{,_only}, with some GNU-like + * extensions. Implements _getopt_internal in case any programs + * expecting GNU libc getopt call it. + */ + +int _my_getopt_internal(int argc, char * argv[], const char *shortopts, + const struct option *longopts, int *longind, + int long_only) +{ + char mode, colon_mode = *shortopts; + int shortoff = 0, opt = -1; + + if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+'; + else { + if((colon_mode = *shortopts) == ':') shortoff ++; + if(((mode = shortopts[shortoff]) == '+') || (mode == '-')) { + shortoff++; + if((colon_mode != ':') && ((colon_mode = shortopts[shortoff]) == ':')) + shortoff ++; + } + } + my_optarg = 0; + if((my_optind >= argc) || + ((argv[my_optind][0] == '-') && + (argv[my_optind][1] == '-') && + (argv[my_optind][2] == '\0'))) { + my_optind++; + opt = -1; + } else if((argv[my_optind][0] != '-') || + (argv[my_optind][1] == '\0')) { + char *tmp; + int i, j, k; + + opt = -1; + if(mode == '+') return -1; + else if(mode == '-') { + my_optarg = argv[my_optind++]; + return 1; + } + for(i=j=my_optind; i j) { + tmp=argv[--i]; + for(k=i; k+1= argc) { + opt = (colon_mode == ':') ? ':' : '?'; + if(my_opterr) fprintf(stderr, + "%s: option `--%s' requires an argument\n", + argv[0], longopts[found].name); + } else my_optarg = argv[my_optind]; + } + if(!opt) { + if (longind) *longind = found; + if(!longopts[found].flag) opt = longopts[found].val; + else *(longopts[found].flag) = longopts[found].val; + } + my_optind++; + } else if(!hits) { + if(offset == 1) opt = my_getopt(argc, argv, shortopts); + else { + opt = '?'; + if(my_opterr) fprintf(stderr, + "%s: unrecognized option `%s'\n", + argv[0], argv[my_optind++]); + } + } else { + opt = '?'; + if(my_opterr) fprintf(stderr, + "%s: option `%s' is ambiguous\n", + argv[0], argv[my_optind++]); + } + } + if (my_optind > argc) my_optind = argc; + return opt; +} + +int my_getopt_long(int argc, char * argv[], const char *shortopts, + const struct option *longopts, int *longind) +{ + return _my_getopt_internal(argc, argv, shortopts, longopts, longind, 0); +} + +int my_getopt_long_only(int argc, char * argv[], const char *shortopts, + const struct option *longopts, int *longind) +{ + return _my_getopt_internal(argc, argv, shortopts, longopts, longind, 1); +} diff --git a/my_getopt.h b/my_getopt.h new file mode 100644 index 0000000..fca42e5 --- /dev/null +++ b/my_getopt.h @@ -0,0 +1,89 @@ +/* + * my_getopt.h - interface to my re-implementation of getopt. + * Copyright 1997, 2000, 2001, 2002, 2006, Benjamin Sittler + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef MY_GETOPT_H_INCLUDED +#define MY_GETOPT_H_INCLUDED + +#ifdef __cplusplus +extern "C" { +#endif + +/* reset argument parser to start-up values */ +extern int my_getopt_reset(void); + +/* UNIX-style short-argument parser */ +extern int my_getopt(int argc, char * argv[], const char *opts); + +extern int my_optind, my_opterr, my_optopt; +extern char *my_optarg; + +struct option { + const char *name; + int has_arg; + int *flag; + int val; +}; + +/* human-readable values for has_arg */ +#undef no_argument +#define no_argument 0 +#undef required_argument +#define required_argument 1 +#undef optional_argument +#define optional_argument 2 + +/* GNU-style long-argument parsers */ +extern int my_getopt_long(int argc, char * argv[], const char *shortopts, + const struct option *longopts, int *longind); + +extern int my_getopt_long_only(int argc, char * argv[], const char *shortopts, + const struct option *longopts, int *longind); + +extern int _my_getopt_internal(int argc, char * argv[], const char *shortopts, + const struct option *longopts, int *longind, + int long_only); + +#undef getopt +#define getopt my_getopt +#undef getopt_long +#define getopt_long my_getopt_long +#undef getopt_long_only +#define getopt_long_only my_getopt_long_only +#undef _getopt_internal +#define _getopt_internal _my_getopt_internal +#undef opterr +#define opterr my_opterr +#undef optind +#define optind my_optind +#undef optopt +#define optopt my_optopt +#undef optarg +#define optarg my_optarg + +#ifdef __cplusplus +} +#endif + +#endif /* MY_GETOPT_H_INCLUDED */ From 7f71c384ff0ac5b81e9cfa73db3180a8f73440af Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 08:26:22 +0100 Subject: [PATCH 06/10] Use C99 fixed width integer types and formats instead of defining our own. Stripped out autoconf type checks and use uint{8,16,32,64}_t and PRIu64 in code. --- configure.ac | 58 ---------------------------------------------------- ike-scan.c | 14 ++++++------- psk-crack.c | 14 ++++++------- 3 files changed, 14 insertions(+), 72 deletions(-) diff --git a/configure.ac b/configure.ac index 1fecd63..66dd74f 100644 --- a/configure.ac +++ b/configure.ac @@ -72,64 +72,6 @@ AC_CHECK_HEADERS([getopt.h]) dnl Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T -dnl Check for the uint{8,16,32}_t types and, if we don't have them, define -dnl them using types which will work on most systems. -dnl We use these fixed-width types for constructing the IKE packet payloads. -AC_NTA_CHECK_TYPE(uint8_t, unsigned char) -AC_NTA_CHECK_TYPE(uint16_t, unsigned short) -AC_NTA_CHECK_TYPE(uint32_t, unsigned int) - -dnl Checks for 64-bit integer types. These checks are from postgresql. -dnl Check to see if we have a working 64-bit integer type. -dnl This breaks down into two steps: -dnl (1) figure out if the compiler has a 64-bit int type with working -dnl arithmetic, and if so -dnl (2) see whether snprintf() can format the type correctly. - -PGAC_TYPE_64BIT_INT([long int]) - -if test x"$HAVE_LONG_INT_64" = x"yes" ; then - INT64_TYPE="long int" - UINT64_TYPE="unsigned long int" -else - PGAC_TYPE_64BIT_INT([long long int]) - if test x"$HAVE_LONG_LONG_INT_64" = x"yes" ; then - INT64_TYPE="long long int" - UINT64_TYPE="unsigned long long int" - else - AC_MSG_ERROR([cannot determine 64-bit integer type]) - fi -fi - -AC_DEFINE_UNQUOTED(IKE_INT64, $INT64_TYPE, - [Define to the appropriate type for 64-bit ints.]) -AC_DEFINE_UNQUOTED(IKE_UINT64, $UINT64_TYPE, - [Define to the appropriate type for unsigned 64-bit ints.]) - -dnl If we found "long int" is 64 bits, assume snprintf handles it. If -dnl we found we need to use "long long int", better check. We cope with -dnl snprintfs that use %lld, %qd, or %I64d as the format. -dnl -if test "$HAVE_LONG_LONG_INT_64" = yes ; then - PGAC_FUNC_SNPRINTF_LONG_LONG_INT_FORMAT - if test "$LONG_LONG_INT_FORMAT" = ""; then - AC_MSG_ERROR([cannot determine snprintf format string for long long int]) - fi - LONG_LONG_UINT_FORMAT=`echo "$LONG_LONG_INT_FORMAT" | sed 's/d$/u/'` - INT64_FORMAT="\"$LONG_LONG_INT_FORMAT\"" - UINT64_FORMAT="\"$LONG_LONG_UINT_FORMAT\"" -else - # Here if we are not using 'long long int' at all - INT64_FORMAT='"%ld"' - UINT64_FORMAT='"%lu"' -fi - -AC_DEFINE_UNQUOTED(IKE_INT64_FORMAT, $INT64_FORMAT, - [Define to the appropriate snprintf format for 64-bit ints.]) - -AC_DEFINE_UNQUOTED(IKE_UINT64_FORMAT, $UINT64_FORMAT, - [Define to the appropriate snprintf format for unsigned 64-bit ints.]) - dnl Checks for library functions. AC_CHECK_FUNCS([malloc gethostbyname gettimeofday inet_ntoa memset select socket strerror]) diff --git a/ike-scan.c b/ike-scan.c index b14c82a..4799536 100644 --- a/ike-scan.c +++ b/ike-scan.c @@ -238,8 +238,8 @@ main(int argc, char *argv[]) { int n; host_entry *temp_cursor; struct timeval diff; /* Difference between two timevals */ - IKE_UINT64 loop_timediff; /* Time since last packet sent in us */ - IKE_UINT64 host_timediff; /* Time since last packet sent to this host */ + uint64_t loop_timediff; /* Time since last packet sent in us */ + uint64_t host_timediff; /* Time since last packet sent to this host */ unsigned long end_timediff=0; /* Time since last packet received in ms */ int req_interval; /* Requested per-packet interval */ int select_timeout; /* Select timeout */ @@ -840,7 +840,7 @@ main(int argc, char *argv[]) { * bandwidth unless an interval was specified. */ if (!interval) { - interval = ((IKE_UINT64)(packet_out_len+PACKET_OVERHEAD) * 8 * 1000000) / + interval = ((uint64_t)(packet_out_len+PACKET_OVERHEAD) * 8 * 1000000) / bandwidth; if (verbose) { warn_msg("DEBUG: pkt len=%zu bytes, bandwidth=%u bps, int=%u us", @@ -885,7 +885,7 @@ main(int argc, char *argv[]) { * potentially send a packet to the current host. */ timeval_diff(&now, &last_packet_time, &diff); - loop_timediff = (IKE_UINT64)1000000*diff.tv_sec + diff.tv_usec; + loop_timediff = (uint64_t)1000000*diff.tv_sec + diff.tv_usec; if (loop_timediff >= (unsigned)req_interval) { /* * If the last packet to this host was sent more than the current @@ -893,7 +893,7 @@ main(int argc, char *argv[]) { * to it. */ timeval_diff(&now, &((*cursor)->last_send_time), &diff); - host_timediff = (IKE_UINT64)1000000*diff.tv_sec + diff.tv_usec; + host_timediff = (uint64_t)1000000*diff.tv_sec + diff.tv_usec; if (host_timediff >= (*cursor)->timeout && (*cursor)->live) { if (reset_cum_err) { cum_err = 0; @@ -924,7 +924,7 @@ main(int argc, char *argv[]) { remove_host(cursor, &live_count, num_hosts); /* Automatically calls advance_cursor() */ if (first_timeout) { timeval_diff(&now, &((*cursor)->last_send_time), &diff); - host_timediff = (IKE_UINT64)1000000*diff.tv_sec + + host_timediff = (uint64_t)1000000*diff.tv_sec + diff.tv_usec; while (host_timediff >= (*cursor)->timeout && live_count) { if ((*cursor)->live) { @@ -936,7 +936,7 @@ main(int argc, char *argv[]) { advance_cursor(live_count, num_hosts); } timeval_diff(&now, &((*cursor)->last_send_time), &diff); - host_timediff = (IKE_UINT64)1000000*diff.tv_sec + + host_timediff = (uint64_t)1000000*diff.tv_sec + diff.tv_usec; } first_timeout=0; diff --git a/psk-crack.c b/psk-crack.c index 3134bbc..10d46fd 100644 --- a/psk-crack.c +++ b/psk-crack.c @@ -72,7 +72,7 @@ main (int argc, char *argv[]) { char dict_file_name[MAXLINE]; /* Dictionary file name */ char *nortel_user = NULL; /* For cracking Nortel Contivity passwords only */ FILE *dictionary_file=NULL; /* Dictionary file */ - IKE_UINT64 iterations=0; + uint64_t iterations=0; struct timeval start_time; /* Program start time */ struct timeval end_time; /* Program end time */ struct timeval elapsed_time; /* Elapsed time as timeval */ @@ -167,19 +167,19 @@ main (int argc, char *argv[]) { */ psk_uncracked = psk_count; if (brute_len) { /* Brute force cracking */ - IKE_UINT64 max; + uint64_t max; unsigned base; unsigned i; - IKE_UINT64 loop; - IKE_UINT64 val; + uint64_t loop; + uint64_t val; unsigned digit; base = strlen(charset); max = base; for (i=1; i Date: Fri, 13 Sep 2024 08:43:03 +0100 Subject: [PATCH 07/10] Removed depreciated --enable-lookup configure option. The associated code was removed in 9024c3a dated Jul 1 2006. --- configure.ac | 7 ------- 1 file changed, 7 deletions(-) diff --git a/configure.ac b/configure.ac index 66dd74f..9617b4e 100644 --- a/configure.ac +++ b/configure.ac @@ -108,13 +108,6 @@ dnl If we don't have it, then use Russ Allbery's implementation as a dnl replacement function. AC_CHECK_FUNC(inet_aton, , [ AC_LIBOBJ(inet_aton) ]) -dnl Do we want to disable the initial gethostbyname() call? -dnl The default is for it to be enabled. -AC_ARG_ENABLE(lookup, - AS_HELP_STRING([--enable-lookup],[Legacy option, present for compatibility]), - AC_MSG_NOTICE([The --enable-lookup option is depreciated]) -) - dnl The big OpenSSL hunt. dnl dnl Check for OpenSSL headers and libraries if the --with-openssl[=PATH] From e2935f6addace94745a2cd8412f3eb641b75f941 Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 09:22:29 +0100 Subject: [PATCH 08/10] Move local autoconf macros from acinclude.m4 to seperate files under m4 directory Removed unused macros that used to check fixed width integer types and formats. Require autoconf 2.70 or later. --- acinclude.m4 | 334 -------------------------------------- configure.ac | 3 +- m4/ac-nta-net-size-t.m4 | 49 ++++++ m4/gcc-format-security.m4 | 30 ++++ m4/gcc-fortify-source.m4 | 31 ++++ m4/gcc-stack-protect.m4 | 51 ++++++ m4/gcc-wextra.m4 | 18 ++ 7 files changed, 181 insertions(+), 335 deletions(-) delete mode 100644 acinclude.m4 create mode 100644 m4/ac-nta-net-size-t.m4 create mode 100644 m4/gcc-format-security.m4 create mode 100644 m4/gcc-fortify-source.m4 create mode 100644 m4/gcc-stack-protect.m4 create mode 100644 m4/gcc-wextra.m4 diff --git a/acinclude.m4 b/acinclude.m4 deleted file mode 100644 index 2b2bd9e..0000000 --- a/acinclude.m4 +++ /dev/null @@ -1,334 +0,0 @@ -dnl NTA Monitor autoconf macros - -dnl AC_NTA_CHECK_TYPE -- See if a type exists using reasonable includes -dnl -dnl Although there is a standard macro AC_CHECK_TYPE, we can't always -dnl use this because it doesn't include enough header files. -dnl -AC_DEFUN([AC_NTA_CHECK_TYPE], - [AC_MSG_CHECKING([for $1 using $CC]) - AC_CACHE_VAL(ac_cv_nta_have_$1, - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ -# include "confdefs.h" -# include -# if HAVE_SYS_TYPES_H -# include -# endif -# if HAVE_SYS_STAT_H -# include -# endif -# ifdef STDC_HEADERS -# include -# include -# endif -# if HAVE_INTTYPES_H -# include -# else -# if HAVE_STDINT_H -# include -# endif -# endif -# if HAVE_UNISTD_H -# include -# endif -# ifdef HAVE_ARPA_INET_H -# include -# endif -# ifdef HAVE_NETDB_H -# include -# endif -# ifdef HAVE_NETINET_IN_H -# include -# endif -# ifdef SYS_SOCKET_H -# include -# endif - ]], [[$1 i]])],[ac_cv_nta_have_$1=yes],[ac_cv_nta_have_$1=no])) - AC_MSG_RESULT($ac_cv_nta_have_$1) - if test $ac_cv_nta_have_$1 = no ; then - AC_DEFINE($1, $2, [Define to required type if we don't have $1]) - fi]) - -dnl AC_NTA_NET_SIZE_T -- Determine type of 3rd argument to accept -dnl -dnl This type is normally socklen_t but is sometimes size_t or int instead. -dnl We try, in order: socklen_t, int, size_t until we find one that compiles -dnl -AC_DEFUN([AC_NTA_NET_SIZE_T], - [AC_MSG_CHECKING([for socklen_t or equivalent using $CC]) - ac_nta_net_size_t=no - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ -# include "confdefs.h" -# include -# ifdef HAVE_SYS_SOCKET_H -# include -# endif]], [[int s; - struct sockaddr addr; - socklen_t addrlen; - int result; - result=accept(s, &addr, &addrlen)]])],[ac_nta_net_size_t=socklen_t],[ac_nta_net_size_t=no]) - if test $ac_nta_net_size_t = no; then - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ -# include "confdefs.h" -# include -# ifdef HAVE_SYS_SOCKET_H -# include -# endif]], [[int s; - struct sockaddr addr; - int addrlen; - int result; - result=accept(s, &addr, &addrlen)]])],[ac_nta_net_size_t=int],[ac_nta_net_size_t=no]) - fi - if test $ac_nta_net_size_t = no; then - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ -# include "confdefs.h" -# include -# ifdef HAVE_SYS_SOCKET_H -# include -# endif]], [[int s; - struct sockaddr addr; - size_t addrlen; - int result; - result=accept(s, &addr, &addrlen)]])],[ac_nta_net_size_t=size_t],[ac_nta_net_size_t=no]) - fi - if test $ac_nta_net_size_t = no; then - AC_MSG_ERROR([Cannot find acceptable type for 3rd arg to accept()]) - else - AC_MSG_RESULT($ac_nta_net_size_t) - AC_DEFINE_UNQUOTED(NET_SIZE_T, $ac_nta_net_size_t, [Define required type for 3rd arg to accept()]) - fi - ]) - -dnl PGAC_TYPE_64BIT_INT(TYPE) -dnl ------------------------- -dnl Check if TYPE is a working 64 bit integer type. Set HAVE_TYPE_64 to -dnl yes or no respectively, and define HAVE_TYPE_64 if yes. -dnl -dnl This function comes from the Postgresql file: -dnl pgsql/config/c-compiler.m4,v 1.13 -dnl -AC_DEFUN([PGAC_TYPE_64BIT_INT], -[define([Ac_define], [translit([have_$1_64], [a-z *], [A-Z_P])])dnl -define([Ac_cachevar], [translit([pgac_cv_type_$1_64], [ *], [_p])])dnl -AC_CACHE_CHECK([whether $1 is 64 bits], [Ac_cachevar], -[AC_RUN_IFELSE([AC_LANG_SOURCE([[typedef $1 int64; - -/* - * These are globals to discourage the compiler from folding all the - * arithmetic tests down to compile-time constants. - */ -int64 a = 20000001; -int64 b = 40000005; - -int does_int64_work() -{ - int64 c,d; - - if (sizeof(int64) != 8) - return 0; /* definitely not the right size */ - - /* Do perfunctory checks to see if 64-bit arithmetic seems to work */ - c = a * b; - d = (c + b) / b; - if (d != a+1) - return 0; - return 1; -} -int main() { - return ! does_int64_work(); -}]])],[Ac_cachevar=yes],[Ac_cachevar=no],[# If cross-compiling, check the size reported by the compiler and -# trust that the arithmetic works. -AC_COMPILE_IFELSE([AC_LANG_BOOL_COMPILE_TRY([], [sizeof($1) == 8])], - Ac_cachevar=yes, - Ac_cachevar=no)])]) - -Ac_define=$Ac_cachevar -if test x"$Ac_cachevar" = xyes ; then - AC_DEFINE(Ac_define,, [Define to 1 if `]$1[' works and is 64 bits.]) -fi -undefine([Ac_define])dnl -undefine([Ac_cachevar])dnl -])# PGAC_TYPE_64BIT_INT - -dnl PGAC_FUNC_SNPRINTF_LONG_LONG_INT_FORMAT -dnl --------------------------------------- -dnl Determine which format snprintf uses for long long int. We handle -dnl %lld, %qd, %I64d. The result is in shell variable -dnl LONG_LONG_INT_FORMAT. -dnl -dnl MinGW uses '%I64d', though gcc throws an warning with -Wall, -dnl while '%lld' doesn't generate a warning, but doesn't work. -dnl -dnl This function comes from the Postgresql file: -dnl pgsql/config/c-library.m4,v 1.28 -dnl -AC_DEFUN([PGAC_FUNC_SNPRINTF_LONG_LONG_INT_FORMAT], -[AC_MSG_CHECKING([snprintf format for long long int]) -AC_CACHE_VAL(pgac_cv_snprintf_long_long_int_format, -[for pgac_format in '%lld' '%qd' '%I64d'; do -AC_RUN_IFELSE([AC_LANG_SOURCE([[#include -#include -typedef long long int int64; -#define INT64_FORMAT "$pgac_format" - -int64 a = 20000001; -int64 b = 40000005; - -int does_int64_snprintf_work() -{ - int64 c; - char buf[100]; - - if (sizeof(int64) != 8) - return 0; /* doesn't look like the right size */ - - c = a * b; - snprintf(buf, 100, INT64_FORMAT, c); - if (strcmp(buf, "800000140000005") != 0) - return 0; /* either multiply or snprintf is busted */ - return 1; -} -int main() { - return ! does_int64_snprintf_work(); -}]])],[pgac_cv_snprintf_long_long_int_format=$pgac_format; break],[],[pgac_cv_snprintf_long_long_int_format=cross; break]) -done])dnl AC_CACHE_VAL - -LONG_LONG_INT_FORMAT='' - -case $pgac_cv_snprintf_long_long_int_format in - cross) AC_MSG_RESULT([cannot test (not on host machine)]);; - ?*) AC_MSG_RESULT([$pgac_cv_snprintf_long_long_int_format]) - LONG_LONG_INT_FORMAT=$pgac_cv_snprintf_long_long_int_format;; - *) AC_MSG_RESULT(none);; -esac])# PGAC_FUNC_SNPRINTF_LONG_LONG_INT_FORMAT - -dnl -dnl Useful macros for autoconf to check for ssp-patched gcc -dnl 1.0 - September 2003 - Tiago Sousa -dnl -dnl About ssp: -dnl GCC extension for protecting applications from stack-smashing attacks -dnl http://www.research.ibm.com/trl/projects/security/ssp/ -dnl -dnl Usage: -dnl After calling the correct AC_LANG_*, use the corresponding macro: -dnl -dnl GCC_STACK_PROTECT_CC -dnl checks -fstack-protector with the C compiler, if it exists then updates -dnl CFLAGS and defines ENABLE_SSP_CC -dnl -dnl GCC_STACK_PROTECT_CXX -dnl checks -fstack-protector with the C++ compiler, if it exists then updates -dnl CXXFLAGS and defines ENABLE_SSP_CXX -dnl -AC_DEFUN([GCC_STACK_PROTECT_CC],[ - ssp_cc=yes - if test "X$CC" != "X"; then - AC_MSG_CHECKING([whether ${CC} accepts -fstack-protector]) - ssp_old_cflags="$CFLAGS" - CFLAGS="$CFLAGS -fstack-protector" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cc=no]) - echo $ssp_cc - if test "X$ssp_cc" = "Xno"; then - CFLAGS="$ssp_old_cflags" - else - AC_DEFINE([ENABLE_SSP_CC], 1, [Define if SSP C support is enabled.]) - fi - fi -]) - -AC_DEFUN([GCC_STACK_PROTECT_CXX],[ - ssp_cxx=yes - if test "X$CXX" != "X"; then - AC_MSG_CHECKING([whether ${CXX} accepts -fstack-protector]) - ssp_old_cxxflags="$CXXFLAGS" - CXXFLAGS="$CXXFLAGS -fstack-protector" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cxx=no]) - echo $ssp_cxx - if test "X$ssp_cxx" = "Xno"; then - CXXFLAGS="$ssp_old_cxxflags" - else - AC_DEFINE([ENABLE_SSP_CXX], 1, [Define if SSP C++ support is enabled.]) - fi - fi -]) - -dnl Check whether GCC accepts -D_FORTIFY_SOURCE -dnl -dnl This was introduced in GCC 4.1 and glibc 2.4, but was present in earlier -dnl versions on redhat systems (specifically GCC 3.4.3 and above). -dnl -dnl We define the GNUC_PREREQ macro to the same definition as __GNUC_PREREQ -dnl in . We don't use __GNUC_PREREQ directly because -dnl is not present on all the operating systems that we support, e.g. OpenBSD. -dnl -AC_DEFUN([GCC_FORTIFY_SOURCE],[ - if test "X$CC" != "X"; then - AC_MSG_CHECKING([whether ${CC} accepts -D_FORTIFY_SOURCE]) - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[ - #define GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) - #if !(GNUC_PREREQ (4, 1) \ - || (defined __GNUC_RH_RELEASE__ && GNUC_PREREQ (4, 0)) \ - || (defined __GNUC_RH_RELEASE__ && GNUC_PREREQ (3, 4) \ - && __GNUC_MINOR__ == 4 \ - && (__GNUC_PATCHLEVEL__ > 2 \ - || (__GNUC_PATCHLEVEL__ == 2 && __GNUC_RH_RELEASE__ >= 8)))) - #error No FORTIFY_SOURCE support - #endif - ]])],[ - AC_MSG_RESULT(yes) - CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2" - ],[ - AC_MSG_RESULT(no) - ]) - fi -]) - -dnl Check for support of the GCC -Wformat-security option. -dnl This option was introduced in GCC 3.0. -dnl -dnl Note that in this test, the test compilation fails if the option is -dnl supported, and succeeds if it is not supported. -dnl -dnl If this option is supported, then the test program will produce a -dnl warning like "format not a string literal and no format arguments". -dnl If it is not supported, then the test program will compile without -dnl warnings. -dnl -AC_DEFUN([GCC_FORMAT_SECURITY],[ - if test "X$CC" != "X"; then - AC_MSG_CHECKING([whether ${CC} accepts -Wformat-security]) - wfs_old_cflags="$CFLAGS" - CFLAGS="$CFLAGS -Wall -Werror -Wformat -Wformat-security" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], [[ - char *fmt=NULL; - printf(fmt); - return 0; - ]])],[ - AC_MSG_RESULT(no) - CFLAGS="$wfs_old_cflags" - ],[ - AC_MSG_RESULT(yes) - CFLAGS="$wfs_old_cflags -Wformat -Wformat-security" - ]) - fi -]) - -dnl Check for support of the GCC -Wextra option, which enables extra warnings. -dnl Support for this option was added in gcc 3.4.0. -dnl -AC_DEFUN([GCC_WEXTRA],[ - gcc_wextra=yes - if test "X$CC" != "X"; then - AC_MSG_CHECKING([whether ${CC} accepts -Wextra]) - gcc_old_cflags="$CFLAGS" - CFLAGS="$CFLAGS -Wextra" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[ - AC_MSG_RESULT(yes) - ],[ - AC_MSG_RESULT(no) - gcc_wextra=no - CFLAGS="$ssp_old_cflags" - ]) - fi -]) diff --git a/configure.ac b/configure.ac index 9617b4e..221ef25 100644 --- a/configure.ac +++ b/configure.ac @@ -1,10 +1,11 @@ dnl Process this file with autoconf to produce a configure script. AC_INIT([ike-scan],[1.9.6],[https://github.com/royhills/ike-scan]) -AC_PREREQ(2.69) +AC_PREREQ(2.70) AC_REVISION($Revision$) AC_CONFIG_SRCDIR([ike-scan.c]) AC_CONFIG_AUX_DIR([build-aux]) +AC_CONFIG_MACRO_DIRS([m4]) AM_INIT_AUTOMAKE AC_CONFIG_HEADERS([config.h]) diff --git a/m4/ac-nta-net-size-t.m4 b/m4/ac-nta-net-size-t.m4 new file mode 100644 index 0000000..f26f6d3 --- /dev/null +++ b/m4/ac-nta-net-size-t.m4 @@ -0,0 +1,49 @@ +dnl AC_NTA_NET_SIZE_T -- Determine type of 3rd argument to accept +dnl +dnl This type is normally socklen_t but is sometimes size_t or int instead. +dnl We try, in order: socklen_t, int, size_t until we find one that compiles +dnl +AC_DEFUN([AC_NTA_NET_SIZE_T], + [AC_MSG_CHECKING([for socklen_t or equivalent using $CC]) + ac_nta_net_size_t=no + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ +# include "confdefs.h" +# include +# ifdef HAVE_SYS_SOCKET_H +# include +# endif]], [[int s; + struct sockaddr addr; + socklen_t addrlen; + int result; + result=accept(s, &addr, &addrlen)]])],[ac_nta_net_size_t=socklen_t],[ac_nta_net_size_t=no]) + if test $ac_nta_net_size_t = no; then + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ +# include "confdefs.h" +# include +# ifdef HAVE_SYS_SOCKET_H +# include +# endif]], [[int s; + struct sockaddr addr; + int addrlen; + int result; + result=accept(s, &addr, &addrlen)]])],[ac_nta_net_size_t=int],[ac_nta_net_size_t=no]) + fi + if test $ac_nta_net_size_t = no; then + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ +# include "confdefs.h" +# include +# ifdef HAVE_SYS_SOCKET_H +# include +# endif]], [[int s; + struct sockaddr addr; + size_t addrlen; + int result; + result=accept(s, &addr, &addrlen)]])],[ac_nta_net_size_t=size_t],[ac_nta_net_size_t=no]) + fi + if test $ac_nta_net_size_t = no; then + AC_MSG_ERROR([Cannot find acceptable type for 3rd arg to accept()]) + else + AC_MSG_RESULT($ac_nta_net_size_t) + AC_DEFINE_UNQUOTED(NET_SIZE_T, $ac_nta_net_size_t, [Define required type for 3rd arg to accept()]) + fi + ]) diff --git a/m4/gcc-format-security.m4 b/m4/gcc-format-security.m4 new file mode 100644 index 0000000..7a6a17c --- /dev/null +++ b/m4/gcc-format-security.m4 @@ -0,0 +1,30 @@ +dnl Check for support of the GCC -Wformat-security option. +dnl This option was introduced in GCC 3.0. +dnl +dnl Note that in this test, the test compilation fails if the option is +dnl supported, and succeeds if it is not supported. +dnl +dnl If this option is supported, then the test program will produce a +dnl warning like "format not a string literal and no format arguments". +dnl If it is not supported, then the test program will compile without +dnl warnings. +dnl +AC_DEFUN([GCC_FORMAT_SECURITY],[ + if test "X$CC" != "X"; then + AC_MSG_CHECKING([whether ${CC} accepts -Wformat-security]) + wfs_old_cflags="$CFLAGS" + CFLAGS="$CFLAGS -Wall -Werror -Wformat -Wformat-security" + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], [[ + char *fmt=NULL; + printf(fmt); + return 0; + ]])],[ + AC_MSG_RESULT(no) + CFLAGS="$wfs_old_cflags" + ],[ + AC_MSG_RESULT(yes) + CFLAGS="$wfs_old_cflags -Wformat -Wformat-security" + ]) + fi +]) + diff --git a/m4/gcc-fortify-source.m4 b/m4/gcc-fortify-source.m4 new file mode 100644 index 0000000..7dd385c --- /dev/null +++ b/m4/gcc-fortify-source.m4 @@ -0,0 +1,31 @@ +dnl Check whether GCC accepts -D_FORTIFY_SOURCE +dnl +dnl This was introduced in GCC 4.1 and glibc 2.4, but was present in earlier +dnl versions on redhat systems (specifically GCC 3.4.3 and above). +dnl +dnl We define the GNUC_PREREQ macro to the same definition as __GNUC_PREREQ +dnl in . We don't use __GNUC_PREREQ directly because +dnl is not present on all the operating systems that we support, e.g. OpenBSD. +dnl +AC_DEFUN([GCC_FORTIFY_SOURCE],[ + if test "X$CC" != "X"; then + AC_MSG_CHECKING([whether ${CC} accepts -D_FORTIFY_SOURCE]) + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[ + #define GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) + #if !(GNUC_PREREQ (4, 1) \ + || (defined __GNUC_RH_RELEASE__ && GNUC_PREREQ (4, 0)) \ + || (defined __GNUC_RH_RELEASE__ && GNUC_PREREQ (3, 4) \ + && __GNUC_MINOR__ == 4 \ + && (__GNUC_PATCHLEVEL__ > 2 \ + || (__GNUC_PATCHLEVEL__ == 2 && __GNUC_RH_RELEASE__ >= 8)))) + #error No FORTIFY_SOURCE support + #endif + ]])],[ + AC_MSG_RESULT(yes) + CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2" + ],[ + AC_MSG_RESULT(no) + ]) + fi +]) + diff --git a/m4/gcc-stack-protect.m4 b/m4/gcc-stack-protect.m4 new file mode 100644 index 0000000..f375eea --- /dev/null +++ b/m4/gcc-stack-protect.m4 @@ -0,0 +1,51 @@ +dnl +dnl Useful macros for autoconf to check for ssp-patched gcc +dnl 1.0 - September 2003 - Tiago Sousa +dnl +dnl About ssp: +dnl GCC extension for protecting applications from stack-smashing attacks +dnl http://www.research.ibm.com/trl/projects/security/ssp/ +dnl +dnl Usage: +dnl After calling the correct AC_LANG_*, use the corresponding macro: +dnl +dnl GCC_STACK_PROTECT_CC +dnl checks -fstack-protector with the C compiler, if it exists then updates +dnl CFLAGS and defines ENABLE_SSP_CC +dnl +dnl GCC_STACK_PROTECT_CXX +dnl checks -fstack-protector with the C++ compiler, if it exists then updates +dnl CXXFLAGS and defines ENABLE_SSP_CXX +dnl +AC_DEFUN([GCC_STACK_PROTECT_CC],[ + ssp_cc=yes + if test "X$CC" != "X"; then + AC_MSG_CHECKING([whether ${CC} accepts -fstack-protector]) + ssp_old_cflags="$CFLAGS" + CFLAGS="$CFLAGS -fstack-protector" + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cc=no]) + echo $ssp_cc + if test "X$ssp_cc" = "Xno"; then + CFLAGS="$ssp_old_cflags" + else + AC_DEFINE([ENABLE_SSP_CC], 1, [Define if SSP C support is enabled.]) + fi + fi +]) + +AC_DEFUN([GCC_STACK_PROTECT_CXX],[ + ssp_cxx=yes + if test "X$CXX" != "X"; then + AC_MSG_CHECKING([whether ${CXX} accepts -fstack-protector]) + ssp_old_cxxflags="$CXXFLAGS" + CXXFLAGS="$CXXFLAGS -fstack-protector" + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cxx=no]) + echo $ssp_cxx + if test "X$ssp_cxx" = "Xno"; then + CXXFLAGS="$ssp_old_cxxflags" + else + AC_DEFINE([ENABLE_SSP_CXX], 1, [Define if SSP C++ support is enabled.]) + fi + fi +]) + diff --git a/m4/gcc-wextra.m4 b/m4/gcc-wextra.m4 new file mode 100644 index 0000000..8bfaf01 --- /dev/null +++ b/m4/gcc-wextra.m4 @@ -0,0 +1,18 @@ +dnl Check for support of the GCC -Wextra option, which enables extra warnings. +dnl Support for this option was added in gcc 3.4.0. +dnl +AC_DEFUN([GCC_WEXTRA],[ + gcc_wextra=yes + if test "X$CC" != "X"; then + AC_MSG_CHECKING([whether ${CC} accepts -Wextra]) + gcc_old_cflags="$CFLAGS" + CFLAGS="$CFLAGS -Wextra" + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[ + AC_MSG_RESULT(yes) + ],[ + AC_MSG_RESULT(no) + gcc_wextra=no + CFLAGS="$ssp_old_cflags" + ]) + fi +]) From 43ddb731122b0b2ce35eae7397e0377dc319f733 Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 09:35:45 +0100 Subject: [PATCH 09/10] Renamed autoconf macro ac-nta-net-size-t to ike-net-size-t Modified autoconf macro gcc-stack-protect to link instead of compile --- configure.ac | 2 +- m4/gcc-stack-protect.m4 | 10 ++++++---- m4/{ac-nta-net-size-t.m4 => ike-net-size-t.m4} | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) rename m4/{ac-nta-net-size-t.m4 => ike-net-size-t.m4} (94%) diff --git a/configure.ac b/configure.ac index 221ef25..0f78ab0 100644 --- a/configure.ac +++ b/configure.ac @@ -78,7 +78,7 @@ AC_CHECK_FUNCS([malloc gethostbyname gettimeofday inet_ntoa memset select socket dnl Determine type for 3rd arg to accept() dnl This is normally socklen_t, but can sometimes be size_t or int. -AC_NTA_NET_SIZE_T +IKE_NET_SIZE_T dnl Check if the Posix regular expression functions "regcomp" and "regexec" dnl and the header file "regex.h" are present. diff --git a/m4/gcc-stack-protect.m4 b/m4/gcc-stack-protect.m4 index f375eea..899d853 100644 --- a/m4/gcc-stack-protect.m4 +++ b/m4/gcc-stack-protect.m4 @@ -1,7 +1,10 @@ -dnl dnl Useful macros for autoconf to check for ssp-patched gcc dnl 1.0 - September 2003 - Tiago Sousa dnl +dnl Modified by ffontaine pull request: use AC_LINK_IFELSE instead of +dnl AC_COMPILE_IFELSE because some systems may be missing the libssp library +dnl even though the compiler accepts the option. +dnl dnl About ssp: dnl GCC extension for protecting applications from stack-smashing attacks dnl http://www.research.ibm.com/trl/projects/security/ssp/ @@ -23,7 +26,7 @@ AC_DEFUN([GCC_STACK_PROTECT_CC],[ AC_MSG_CHECKING([whether ${CC} accepts -fstack-protector]) ssp_old_cflags="$CFLAGS" CFLAGS="$CFLAGS -fstack-protector" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cc=no]) + AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cc=no]) echo $ssp_cc if test "X$ssp_cc" = "Xno"; then CFLAGS="$ssp_old_cflags" @@ -39,7 +42,7 @@ AC_DEFUN([GCC_STACK_PROTECT_CXX],[ AC_MSG_CHECKING([whether ${CXX} accepts -fstack-protector]) ssp_old_cxxflags="$CXXFLAGS" CXXFLAGS="$CXXFLAGS -fstack-protector" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cxx=no]) + AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[],[ssp_cxx=no]) echo $ssp_cxx if test "X$ssp_cxx" = "Xno"; then CXXFLAGS="$ssp_old_cxxflags" @@ -48,4 +51,3 @@ AC_DEFUN([GCC_STACK_PROTECT_CXX],[ fi fi ]) - diff --git a/m4/ac-nta-net-size-t.m4 b/m4/ike-net-size-t.m4 similarity index 94% rename from m4/ac-nta-net-size-t.m4 rename to m4/ike-net-size-t.m4 index f26f6d3..b084d5a 100644 --- a/m4/ac-nta-net-size-t.m4 +++ b/m4/ike-net-size-t.m4 @@ -1,9 +1,9 @@ -dnl AC_NTA_NET_SIZE_T -- Determine type of 3rd argument to accept +dnl IKE_NET_SIZE_T -- Determine type of 3rd argument to accept dnl dnl This type is normally socklen_t but is sometimes size_t or int instead. dnl We try, in order: socklen_t, int, size_t until we find one that compiles dnl -AC_DEFUN([AC_NTA_NET_SIZE_T], +AC_DEFUN([IKE_NET_SIZE_T], [AC_MSG_CHECKING([for socklen_t or equivalent using $CC]) ac_nta_net_size_t=no AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ From c63a9e1a5531698861f4711edba24077b4cf3d63 Mon Sep 17 00:00:00 2001 From: Roy Hills Date: Fri, 13 Sep 2024 10:13:35 +0100 Subject: [PATCH 10/10] Add -Wextra and -Wformat-security options to CFLAGS without autoconf tests. All modern versions of GCC and Clang support these options. --- configure.ac | 4 +--- m4/gcc-format-security.m4 | 30 ------------------------------ m4/gcc-wextra.m4 | 18 ------------------ 3 files changed, 1 insertion(+), 51 deletions(-) delete mode 100644 m4/gcc-format-security.m4 delete mode 100644 m4/gcc-wextra.m4 diff --git a/configure.ac b/configure.ac index 0f78ab0..24fe376 100644 --- a/configure.ac +++ b/configure.ac @@ -39,10 +39,8 @@ fi if test -n "$GCC"; then AC_DEFINE([ATTRIBUTE_UNUSED], [__attribute__ ((__unused__))], [Define to the compiler's unused pragma]) - CFLAGS="$CFLAGS -Wall -Wshadow -Wwrite-strings" - GCC_WEXTRA + CFLAGS="$CFLAGS -Wall -Wextra -Wformat-security -Wshadow -Wwrite-strings" GCC_STACK_PROTECT_CC - GCC_FORMAT_SECURITY GCC_FORTIFY_SOURCE dnl Uncomment the lines below for testing with stricter warnings. dnl CFLAGS="$CFLAGS -pedantic -Wpointer-arith -Wcast-qual -Wcast-align -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Winline " diff --git a/m4/gcc-format-security.m4 b/m4/gcc-format-security.m4 deleted file mode 100644 index 7a6a17c..0000000 --- a/m4/gcc-format-security.m4 +++ /dev/null @@ -1,30 +0,0 @@ -dnl Check for support of the GCC -Wformat-security option. -dnl This option was introduced in GCC 3.0. -dnl -dnl Note that in this test, the test compilation fails if the option is -dnl supported, and succeeds if it is not supported. -dnl -dnl If this option is supported, then the test program will produce a -dnl warning like "format not a string literal and no format arguments". -dnl If it is not supported, then the test program will compile without -dnl warnings. -dnl -AC_DEFUN([GCC_FORMAT_SECURITY],[ - if test "X$CC" != "X"; then - AC_MSG_CHECKING([whether ${CC} accepts -Wformat-security]) - wfs_old_cflags="$CFLAGS" - CFLAGS="$CFLAGS -Wall -Werror -Wformat -Wformat-security" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], [[ - char *fmt=NULL; - printf(fmt); - return 0; - ]])],[ - AC_MSG_RESULT(no) - CFLAGS="$wfs_old_cflags" - ],[ - AC_MSG_RESULT(yes) - CFLAGS="$wfs_old_cflags -Wformat -Wformat-security" - ]) - fi -]) - diff --git a/m4/gcc-wextra.m4 b/m4/gcc-wextra.m4 deleted file mode 100644 index 8bfaf01..0000000 --- a/m4/gcc-wextra.m4 +++ /dev/null @@ -1,18 +0,0 @@ -dnl Check for support of the GCC -Wextra option, which enables extra warnings. -dnl Support for this option was added in gcc 3.4.0. -dnl -AC_DEFUN([GCC_WEXTRA],[ - gcc_wextra=yes - if test "X$CC" != "X"; then - AC_MSG_CHECKING([whether ${CC} accepts -Wextra]) - gcc_old_cflags="$CFLAGS" - CFLAGS="$CFLAGS -Wextra" - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[ - AC_MSG_RESULT(yes) - ],[ - AC_MSG_RESULT(no) - gcc_wextra=no - CFLAGS="$ssp_old_cflags" - ]) - fi -])