From 5cc0bcf5e4689e1fb0c23b8368c46fdae0402028 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 13 Apr 2010 14:30:37 -0600 Subject: [PATCH] getopt-posix: detect MacOS bug On MacOS 10.5 (Darwin 9.8.0), optind is incremented by 2 instead of 1 in the case of a missing requirement on condensed options. * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Reject MacOS botch of optind when missing a required argument. * doc/posix-functions/getopt.texi (getopt): Document the bug. * doc/glibc-functions/getopt_long.texi (getopt_long): Likewise. * doc/glibc-functions/getopt_long_only.texi (getopt_long_only): Likewise. Signed-off-by: Eric Blake --- ChangeLog | 8 ++++++++ doc/glibc-functions/getopt_long.texi | 4 ++++ doc/glibc-functions/getopt_long_only.texi | 4 ++++ doc/posix-functions/getopt.texi | 4 ++++ m4/getopt.m4 | 17 ++++++++++++++++- 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 7de81f46e..c46bf3135 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2010-04-13 Eric Blake + getopt-posix: detect MacOS bug + * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Reject MacOS botch of + optind when missing a required argument. + * doc/posix-functions/getopt.texi (getopt): Document the bug. + * doc/glibc-functions/getopt_long.texi (getopt_long): Likewise. + * doc/glibc-functions/getopt_long_only.texi (getopt_long_only): + Likewise. + getopt-posix: avoid spurious failure on Solaris * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Check for getopt_clip as an indicator that setting optind=1 is sufficient for reset. diff --git a/doc/glibc-functions/getopt_long.texi b/doc/glibc-functions/getopt_long.texi index 0472514b5..3cf17f025 100644 --- a/doc/glibc-functions/getopt_long.texi +++ b/doc/glibc-functions/getopt_long.texi @@ -11,6 +11,10 @@ The function @code{getopt_long} does not support the @samp{+} flag in the options string on some platforms: MacOS X 10.5, AIX 5.2, OSF/1 5.1, Solaris 10. @item +The value of @code{optind} after a missing required argument is wrong +on some platforms: +MacOS 10.5. +@item The function @code{getopt_long} does not obey the @samp{-} flag in the options string when @env{POSIXLY_CORRECT} is set on some platforms: Cygwin 1.7.0. diff --git a/doc/glibc-functions/getopt_long_only.texi b/doc/glibc-functions/getopt_long_only.texi index 13edfb42e..edc832be2 100644 --- a/doc/glibc-functions/getopt_long_only.texi +++ b/doc/glibc-functions/getopt_long_only.texi @@ -11,6 +11,10 @@ The function @code{getopt_long_only} does not support the @samp{+} flag in the options string on some platforms: MacOS X 10.5, AIX 5.2, OSF/1 5.1, Solaris 10. @item +The value of @code{optind} after a missing required argument is wrong +on some platforms: +MacOS 10.5. +@item The function @code{getopt_long_only} does not obey the @samp{-} flag in the options string when @env{POSIXLY_CORRECT} is set on some platforms: Cygwin 1.7.0. diff --git a/doc/posix-functions/getopt.texi b/doc/posix-functions/getopt.texi index 822dbb3ea..d4d03f0c2 100644 --- a/doc/posix-functions/getopt.texi +++ b/doc/posix-functions/getopt.texi @@ -16,6 +16,10 @@ Portability problems fixed by either Gnulib module @code{getopt-posix} or @code{ The @code{getopt} function keeps some internal state that cannot be explicitly reset on some platforms: mingw. +@item +The value of @code{optind} after a missing required argument is wrong +on some platforms: +MacOS 10.5. @end itemize Portability problems fixed by Gnulib module @code{getopt-gnu}: diff --git a/m4/getopt.m4 b/m4/getopt.m4 index 8d3d8b641..2cd74992c 100644 --- a/m4/getopt.m4 +++ b/m4/getopt.m4 @@ -1,4 +1,4 @@ -# getopt.m4 serial 26 +# getopt.m4 serial 27 dnl Copyright (C) 2002-2006, 2008-2010 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -172,6 +172,20 @@ main () if (!(optind == 1)) return 12; } + /* Detect MacOS 10.5 bug. */ + { + char *argv[3] = { "program", "-ab", NULL }; + optind = OPTIND_MIN; + opterr = 0; + if (getopt (2, argv, "ab:") != 'a') + return 13; + if (getopt (2, argv, "ab:") != '?') + return 14; + if (optopt != 'b') + return 15; + if (optind != 2) + return 16; + } return 0; } @@ -179,6 +193,7 @@ main () [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no], [case "$host_os" in mingw*) gl_cv_func_getopt_posix="guessing no";; + darwin*) gl_cv_func_getopt_posix="guessing no";; *) gl_cv_func_getopt_posix="guessing yes";; esac ]) -- 2.11.0