commit 3084115d049713929599620ab7c530154e82008e
parent 8039190008e185d5df2e4e5483e5372a86e5a5b3
Author: Christian Grothoff <christian@grothoff.org>
Date: Thu, 12 Nov 2026 00:08:08 +0100
main: change the bundled getopt to C99
Apple clang rejects K&R definitions :-(
Diffstat:
3 files changed, 29 insertions(+), 62 deletions(-)
diff --git a/src/main/getopt.c b/src/main/getopt.c
@@ -208,13 +208,10 @@ static char *posixly_correct;
/* Avoid depending on library functions or files
whose names are inconsistent. */
-char *getenv ();
+#include <stdlib.h>
static char *
-my_index (str, chr)
-const char *str;
-
-int chr;
+my_index (const char *str, int chr)
{
while (*str)
{
@@ -225,6 +222,7 @@ int chr;
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__
@@ -285,12 +283,12 @@ store_args_and_env (int argc, char *const *argv)
text_set_element (__libc_subinit, store_args_and_env);
# 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; \
- }
+ 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 /* !_LIBC */
# define SWAP_FLAGS(ch1, ch2)
#endif /* _LIBC */
@@ -310,9 +308,7 @@ static void exchange (char **);
#endif
static void
-exchange (argv)
-char **argv;
-
+exchange (char **argv)
{
int bottom = first_nonopt;
int middle = last_nonopt;
@@ -390,6 +386,7 @@ char **argv;
last_nonopt = optind;
}
+
/* Initialize the internal data when the first call is made. */
#if defined (__STDC__) && __STDC__
@@ -397,11 +394,7 @@ 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;
+_getopt_initialize (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
@@ -466,6 +459,7 @@ const char *optstring;
return optstring;
}
+
/* Scan elements of ARGV (whose length is ARGC) for option characters
given in OPTSTRING.
@@ -523,14 +517,8 @@ const char *optstring;
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;
+_getopt_internal (int argc, char *const *argv, const char *optstring,
+ const struct option *longopts, int *longind, int long_only)
{
optarg = NULL;
@@ -980,17 +968,15 @@ int long_only;
}
}
-int
-getopt (argc, argv, optstring)
-int argc;
-char *const *argv;
-const char *optstring;
+int
+getopt (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
@@ -999,10 +985,7 @@ const char *optstring;
the above definition of `getopt'. */
int
-main (argc, argv)
-int argc;
-
-char **argv;
+main (int argc, char **argv)
{
int c;
int digit_optind = 0;
@@ -1064,4 +1047,5 @@ char **argv;
exit (0);
}
+
#endif /* TEST */
diff --git a/src/main/getopt.h b/src/main/getopt.h
@@ -101,16 +101,10 @@ struct option
#define optional_argument 2
#if defined (__STDC__) && __STDC__
-#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. */
+/* Since C23, "getopt ()" declares a function without parameters,
+ so prototype getopt as POSIX does, on every system. */
extern int getopt (int argc, char *const *argv, const char *shortopts);
-#else /* not __GNU_LIBRARY__ */
-extern int getopt ();
-
-#endif /* __GNU_LIBRARY__ */
extern int getopt_long (int argc, char *const *argv, const char *shortopts,
const struct option *longopts, int *longind);
diff --git a/src/main/getopt1.c b/src/main/getopt1.c
@@ -61,30 +61,21 @@ USA. */
#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;
+getopt_long (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;
+getopt_long_only (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);
}
@@ -97,10 +88,7 @@ int *opt_index;
#include <stdio.h>
int
-main (argc, argv)
-int argc;
-
-char **argv;
+main (int argc, char **argv)
{
int c;
int digit_optind = 0;
@@ -184,4 +172,5 @@ char **argv;
exit (0);
}
+
#endif /* TEST */