emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 0227559: Merge from gnulib


From: Paul Eggert
Subject: [Emacs-diffs] master 0227559: Merge from gnulib
Date: Thu, 6 Apr 2017 19:31:47 -0400 (EDT)

branch: master
commit 022755953fc63ef70b72576e14e94324d055ef60
Author: Paul Eggert <address@hidden>
Commit: Paul Eggert <address@hidden>

    Merge from gnulib
    
    This merges some getopt fixes from Zack Weinberg, and affects only
    non-GNUish platforms.  It incorporates:
    2017-04-06 getopt-gnu: omit some duplicate code
    2017-04-06 getopt-posix: use angle-bracket include
    2017-04-06 getopt: annotate files with relationship to glibc
    2017-04-06 getopt: split up getopt.in.h and eliminate __need_getopt
    2017-04-06 getopt: better handling of ambiguous options
    2017-04-06 getopt: refactor long-option handling
    2017-04-06 getopt: tidy up _getopt_initialize a bit
    2017-04-06 getopt: merge from glibc: repetition reduction
    2017-04-06 getopt: clean up error reporting
    2017-04-06 getopt: fix fencepost error in ambiguous-W-option handling
    2017-04-06 getopt: clean up getopt.c and getopt1.c file headers
    2017-04-06 getopt: harmonize comments with glibc
    2017-04-06 getopt: remove USE_NONOPTION_FLAGS
    2017-04-06 getopt: tabify, in preparation for merge with glibc
    2017-04-06 md5, sha1, sha256, sha512: Add comments re correctness
    * build-aux/config.sub, doc/misc/texinfo.tex, lib/getopt.c:
    * lib/getopt.in.h, lib/getopt1.c, lib/getopt_int.h, lib/md5.c:
    * lib/md5.h, lib/sha1.c, lib/sha1.h, lib/sha256.c, lib/sha256.h:
    * lib/sha512.c, lib/sha512.h, lib/unistd.in.h, m4/getopt.m4:
    Copy from gnulib.
    * lib/getopt_cdefs.in.h, lib/getopt_core.h, lib/getopt_ext.h:
    * lib/getopt_pfx_core.h, lib/getopt_pfx_ext.h:
    New files, taken from gnulib.
    * lib/gnulib.mk.in, m4/gnulib-comp.m4:
    Regenerate.
---
 build-aux/config.sub  |    6 +-
 doc/misc/texinfo.tex  |   11 +-
 lib/getopt.c          | 1445 +++++++++++++++++--------------------------------
 lib/getopt.in.h       |  275 +---------
 lib/getopt1.c         |  157 +++---
 lib/getopt_cdefs.in.h |   75 +++
 lib/getopt_core.h     |   96 ++++
 lib/getopt_ext.h      |   77 +++
 lib/getopt_int.h      |   69 +--
 lib/getopt_pfx_core.h |   54 ++
 lib/getopt_pfx_ext.h  |   64 +++
 lib/gnulib.mk.in      |   21 +-
 lib/md5.c             |    5 +-
 lib/md5.h             |    4 +-
 lib/sha1.c            |    5 +-
 lib/sha1.h            |    4 +-
 lib/sha256.c          |    5 +-
 lib/sha256.h          |    4 +-
 lib/sha512.c          |    5 +-
 lib/sha512.h          |    4 +-
 lib/unistd.in.h       |    4 +-
 m4/getopt.m4          |   22 +-
 m4/gnulib-comp.m4     |   14 +-
 23 files changed, 1049 insertions(+), 1377 deletions(-)

diff --git a/build-aux/config.sub b/build-aux/config.sub
index 7203bf1..40ea5df 100755
--- a/build-aux/config.sub
+++ b/build-aux/config.sub
@@ -2,7 +2,7 @@
 # Configuration validation subroutine script.
 #   Copyright 1992-2017 Free Software Foundation, Inc.
 
-timestamp='2017-03-21'
+timestamp='2017-04-02'
 
 # This file is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License as published by
@@ -263,7 +263,7 @@ case $basic_machine in
        | fido | fr30 | frv | ft32 \
        | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \
        | hexagon \
-       | i370 | i860 | i960 | ia64 \
+       | i370 | i860 | i960 | ia16 | ia64 \
        | ip2k | iq2000 \
        | k1om \
        | le32 | le64 \
@@ -389,7 +389,7 @@ case $basic_machine in
        | h8300-* | h8500-* \
        | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \
        | hexagon-* \
-       | i*86-* | i860-* | i960-* | ia64-* \
+       | i*86-* | i860-* | i960-* | ia16-* | ia64-* \
        | ip2k-* | iq2000-* \
        | k1om-* \
        | le32-* | le64-* \
diff --git a/doc/misc/texinfo.tex b/doc/misc/texinfo.tex
index 970d878..7bcb27b 100644
--- a/doc/misc/texinfo.tex
+++ b/doc/misc/texinfo.tex
@@ -3,11 +3,11 @@
 % Load plain if necessary, i.e., if running under initex.
 \expandafter\ifx\csname fmtname\endcsname\relax\input plain\fi
 %
-\def\texinfoversion{2017-03-07.20}
+\def\texinfoversion{2017-03-25.14}
 %
 % Copyright 1985, 1986, 1988, 1990, 1991, 1992, 1993, 1994, 1995,
 % 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
-% 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
+% 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
 % Free Software Foundation, Inc.
 %
 % This texinfo.tex file is free software: you can redistribute it and/or
@@ -11312,7 +11312,6 @@ directory should work if nowhere else does.}
   %
   \lispnarrowing = 0.3in
   \tolerance = 700
-  \hfuzz = 1pt
   \contentsrightmargin = 0pt
   \defbodyindent = .5cm
 }}
@@ -11330,7 +11329,6 @@ directory should work if nowhere else does.}
   %
   \lispnarrowing = 0.25in
   \tolerance = 700
-  \hfuzz = 1pt
   \contentsrightmargin = 0pt
   \defbodyindent = .4cm
 }}
@@ -11356,7 +11354,6 @@ directory should work if nowhere else does.}
                     {297mm}{210mm}%
   %
   \tolerance = 700
-  \hfuzz = 1pt
   \contentsrightmargin = 0pt
   \defbodyindent = 5mm
 }}
@@ -11375,7 +11372,6 @@ directory should work if nowhere else does.}
   %
   \lispnarrowing = 0.2in
   \tolerance = 800
-  \hfuzz = 1.2pt
   \contentsrightmargin = 0pt
   \defbodyindent = 2mm
   \tableindent = 12mm
@@ -11431,6 +11427,9 @@ directory should work if nowhere else does.}
 %
 \letterpaper
 
+% Default value of \hfuzz, for suppressing warnings about overfull hboxes.
+\hfuzz = 1pt
+
 
 \message{and turning on texinfo input format.}
 
diff --git a/lib/getopt.c b/lib/getopt.c
index 7afd0dc..a7db39b 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -1,9 +1,7 @@
 /* Getopt for GNU.
-   NOTE: getopt is part of the C library, so if you don't know what
-   "Keep this file name-space clean" means, talk to address@hidden
-   before changing it!
    Copyright (C) 1987-2017 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
+   This file is part of the GNU C Library and is also part of gnulib.
+   Patches to this file should be submitted to both projects.
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
@@ -27,34 +25,57 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef _LIBC
+/* When used as part of glibc, error printing must be done differently
+   for standards compliance.  getopt is not a cancellation point, so
+   it must not call functions that are, and it is specified by an
+   older standard than stdio locking, so it must not refer to
+   functions in the "user namespace" related to stdio locking.
+   Finally, it must use glibc's internal message translation so that
+   the messages are looked up in the proper text domain.  */
 # include <libintl.h>
+# define fprintf __fxprintf_nocancel
+# define flockfile(fp) _IO_flockfile (fp)
+# define funlockfile(fp) _IO_funlockfile (fp)
 #else
 # include "gettext.h"
 # define _(msgid) gettext (msgid)
+/* When used standalone, flockfile and funlockfile might not be
+   available.  */
+# ifndef _POSIX_THREAD_SAFE_FUNCTIONS
+#  define flockfile(fp) /* nop */
+#  define funlockfile(fp) /* nop */
+# endif
+/* When used standalone, do not attempt to use alloca.  */
+# define __libc_use_alloca(size) 0
+# undef alloca
+# define alloca(size) (abort (), (void *)0)
 #endif
 
-#if defined _LIBC
-# include <wchar.h>
-#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_long' 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.
-
-   Using 'getopt' or setting the environment variable POSIXLY_CORRECT
+/* This implementation of 'getopt' has three modes for handling
+   options interspersed with non-option arguments.  It can stop
+   scanning for options at the first non-option argument encountered,
+   as POSIX specifies.  It can continue scanning for options after the
+   first non-option argument, but permute 'argv' as it goes so that,
+   after 'getopt' is done, all the options precede all the non-option
+   arguments and 'optind' points to the first non-option argument.
+   Or, it can report non-option arguments as if they were arguments to
+   the option character '\x01'.
+
+   The default behavior of 'getopt_long' is to permute the argument list.
+   When this implementation is used standalone, the default behavior of
+   'getopt' is to stop at the first non-option argument, but when it is
+   used as part of GNU libc it also permutes the argument list.  In both
+   cases, setting the environment variable POSIXLY_CORRECT to any value
    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.  */
+   If the first character of the OPTSTRING argument to 'getopt' or
+   'getopt_long' is '+', both functions will stop at the first
+   non-option argument.  If it is '-', both functions will report
+   non-option arguments as arguments to the option character '\x01'.  */
 
 #include "getopt_int.h"
 
@@ -95,42 +116,7 @@ int optopt = '?';
 /* Keep a global copy of all internal members of getopt_data.  */
 
 static struct _getopt_data getopt_data;
-
-
-#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
-extern char *getenv ();
-#endif
 
-#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;
-# endif
-
-# ifdef USE_NONOPTION_FLAGS
-#  define SWAP_FLAGS(ch1, ch2) \
-  if (d->__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.
@@ -153,64 +139,40 @@ exchange (char **argv, struct _getopt_data *d)
      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 (d->__nonoption_flags_len > 0 && top >= d->__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)
-        d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
-      else
-        {
-          memset (__mempcpy (new_str, __getopt_nonoption_flags,
-                             d->__nonoption_flags_max_len),
-                  '\0', top + 1 - d->__nonoption_flags_max_len);
-          d->__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;
-          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;
-        }
+       {
+         /* Bottom segment is the short one.  */
+         int len = middle - bottom;
+         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;
+           }
+         /* Exclude the moved bottom segment from further swapping.  */
+         top -= len;
+       }
       else
-        {
-          /* Top segment is the short one.  */
-          int len = top - middle;
-          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;
-        }
+       {
+         /* Top segment is the short one.  */
+         int len = top - middle;
+         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;
+           }
+         /* Exclude the moved top segment from further swapping.  */
+         bottom += len;
+       }
     }
 
   /* Update records for the slots the non-options now occupy.  */
@@ -219,25 +181,216 @@ exchange (char **argv, struct _getopt_data *d)
   d->__last_nonopt = d->optind;
 }
 
-/* Initialize the internal data when the first call is made.  */
+/* Process the argument starting with d->__nextchar as a long option.
+   d->optind should *not* have been advanced over this argument.
+
+   If the value returned is -1, it was not actually a long option, the
+   state is unchanged, and the argument should be processed as a set
+   of short options (this can only happen when long_only is true).
+   Otherwise, the option (and its argument, if any) have been consumed
+   and the return value is the value to return from _getopt_internal_r.  */
+static int
+process_long_option (int argc, char **argv, const char *optstring,
+                    const struct option *longopts, int *longind,
+                    int long_only, struct _getopt_data *d,
+                    int print_errors, const char *prefix)
+{
+  char *nameend;
+  size_t namelen;
+  const struct option *p;
+  const struct option *pfound = NULL;
+  int n_options;
+  int option_index;
+
+  for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
+    /* Do nothing.  */ ;
+  namelen = nameend - d->__nextchar;
+
+  /* First look for an exact match, counting the options as a side
+     effect.  */
+  for (p = longopts, n_options = 0; p->name; p++, n_options++)
+    if (!strncmp (p->name, d->__nextchar, namelen)
+       && namelen == strlen (p->name))
+      {
+       /* Exact match found.  */
+       pfound = p;
+       option_index = n_options;
+       break;
+      }
+
+  if (pfound == NULL)
+    {
+      /* Didn't find an exact match, so look for abbreviations.  */
+      unsigned char *ambig_set = NULL;
+      int ambig_malloced = 0;
+      int ambig_fallback = 0;
+      int indfound = -1;
+
+      for (p = longopts, option_index = 0; p->name; p++, option_index++)
+       if (!strncmp (p->name, d->__nextchar, namelen))
+         {
+           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.  */
+               if (!ambig_fallback)
+                 {
+                   if (!print_errors)
+                     /* Don't waste effort tracking the ambig set if
+                        we're not going to print it anyway.  */
+                     ambig_fallback = 1;
+                   else if (!ambig_set)
+                     {
+                       if (__libc_use_alloca (n_options))
+                         ambig_set = alloca (n_options);
+                       else if ((ambig_set = malloc (n_options)) == NULL)
+                         /* Fall back to simpler error message.  */
+                         ambig_fallback = 1;
+                       else
+                         ambig_malloced = 1;
+
+                       if (ambig_set)
+                         {
+                           memset (ambig_set, 0, n_options);
+                           ambig_set[indfound] = 1;
+                         }
+                     }
+                   if (ambig_set)
+                     ambig_set[option_index] = 1;
+                 }
+             }
+         }
+
+      if (ambig_set || ambig_fallback)
+       {
+         if (print_errors)
+           {
+             if (ambig_fallback)
+               fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
+                        argv[0], prefix, d->__nextchar);
+             else
+               {
+                 flockfile (stderr);
+                 fprintf (stderr,
+                          _("%s: option '%s%s' is ambiguous; possibilities:"),
+                          argv[0], prefix, d->__nextchar);
+
+                 for (option_index = 0; option_index < n_options; 
option_index++)
+                   if (ambig_set[option_index])
+                     fprintf (stderr, " '%s%s'",
+                              prefix, longopts[option_index].name);
+
+                 /* This must use 'fprintf' even though it's only
+                    printing a single character, so that it goes through
+                    __fxprintf_nocancel when compiled as part of glibc.  */
+                 fprintf (stderr, "\n");
+                 funlockfile (stderr);
+               }
+           }
+         if (ambig_malloced)
+           free (ambig_set);
+         d->__nextchar += strlen (d->__nextchar);
+         d->optind++;
+         d->optopt = 0;
+         return '?';
+       }
+
+      option_index = indfound;
+    }
+
+  if (pfound == NULL)
+    {
+      /* 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.  */
+      if (!long_only || argv[d->optind][1] == '-'
+         || strchr (optstring, *d->__nextchar) == NULL)
+       {
+         if (print_errors)
+           fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
+                    argv[0], prefix, d->__nextchar);
+
+         d->__nextchar = NULL;
+         d->optind++;
+         d->optopt = 0;
+         return '?';
+       }
+
+      /* Otherwise interpret it as a short option.  */
+      return -1;
+    }
+
+  /* We have found a matching long option.  Consume it.  */
+  d->optind++;
+  d->__nextchar = NULL;
+  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)
+       d->optarg = nameend + 1;
+      else
+       {
+         if (print_errors)
+           fprintf (stderr,
+                    _("%s: option '%s%s' doesn't allow an argument\n"),
+                    argv[0], prefix, pfound->name);
+
+         d->optopt = pfound->val;
+         return '?';
+       }
+    }
+  else if (pfound->has_arg == 1)
+    {
+      if (d->optind < argc)
+       d->optarg = argv[d->optind++];
+      else
+       {
+         if (print_errors)
+           fprintf (stderr,
+                    _("%s: option '%s%s' requires an argument\n"),
+                    argv[0], prefix, pfound->name);
+
+         d->optopt = pfound->val;
+         return optstring[0] == ':' ? ':' : '?';
+       }
+    }
+
+  if (longind != NULL)
+    *longind = option_index;
+  if (pfound->flag)
+    {
+      *(pfound->flag) = pfound->val;
+      return 0;
+    }
+  return pfound->val;
+}
+
+/* Initialize internal data upon the first call to getopt.  */
 
 static const char *
 _getopt_initialize (int argc _GL_UNUSED,
-                    char **argv _GL_UNUSED, const char *optstring,
-                    struct _getopt_data *d, int posixly_correct)
+                   char **argv _GL_UNUSED, const char *optstring,
+                   struct _getopt_data *d, int posixly_correct)
 {
   /* 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.  */
+  if (d->optind == 0)
+    d->optind = 1;
 
   d->__first_nonopt = d->__last_nonopt = d->optind;
-
   d->__nextchar = NULL;
 
-  d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
-
   /* Determine how to handle the ordering of options and nonoptions.  */
-
   if (optstring[0] == '-')
     {
       d->__ordering = RETURN_IN_ORDER;
@@ -248,41 +401,12 @@ _getopt_initialize (int argc _GL_UNUSED,
       d->__ordering = REQUIRE_ORDER;
       ++optstring;
     }
-  else if (d->__posixly_correct)
+  else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
     d->__ordering = REQUIRE_ORDER;
   else
     d->__ordering = PERMUTE;
 
-#if defined _LIBC && defined USE_NONOPTION_FLAGS
-  if (!d->__posixly_correct
-      && argc == __libc_argc && argv == __libc_argv)
-    {
-      if (d->__nonoption_flags_max_len == 0)
-        {
-          if (__getopt_nonoption_flags == NULL
-              || __getopt_nonoption_flags[0] == '\0')
-            d->__nonoption_flags_max_len = -1;
-          else
-            {
-              const char *orig_str = __getopt_nonoption_flags;
-              int len = d->__nonoption_flags_max_len = strlen (orig_str);
-              if (d->__nonoption_flags_max_len < argc)
-                d->__nonoption_flags_max_len = argc;
-              __getopt_nonoption_flags =
-                (char *) malloc (d->__nonoption_flags_max_len);
-              if (__getopt_nonoption_flags == NULL)
-                d->__nonoption_flags_max_len = -1;
-              else
-                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
-                        '\0', d->__nonoption_flags_max_len - len);
-            }
-        }
-      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
-    }
-  else
-    d->__nonoption_flags_len = 0;
-#endif
-
+  d->__initialized = 1;
   return optstring;
 }
 
@@ -344,8 +468,8 @@ _getopt_initialize (int argc _GL_UNUSED,
 
 int
 _getopt_internal_r (int argc, char **argv, const char *optstring,
-                    const struct option *longopts, int *longind,
-                    int long_only, struct _getopt_data *d, int posixly_correct)
+                   const struct option *longopts, int *longind,
+                   int long_only, struct _getopt_data *d, int posixly_correct)
 {
   int print_errors = d->opterr;
 
@@ -355,461 +479,129 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
   d->optarg = NULL;
 
   if (d->optind == 0 || !d->__initialized)
-    {
-      if (d->optind == 0)
-        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
-      optstring = _getopt_initialize (argc, argv, optstring, d,
-                                      posixly_correct);
-      d->__initialized = 1;
-    }
+    optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
   else if (optstring[0] == '-' || optstring[0] == '+')
     optstring++;
+
   if (optstring[0] == ':')
     print_errors = 0;
 
-  /* 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[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
-                      || (d->optind < d->__nonoption_flags_len                \
-                          && __getopt_nonoption_flags[d->optind] == '1'))
-#else
-# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
-#endif
+  /* Test whether ARGV[optind] points to a non-option argument.  */
+#define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
 
   if (d->__nextchar == NULL || *d->__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).  */
+        moved back by the user (who may also have changed the arguments).  */
       if (d->__last_nonopt > d->optind)
-        d->__last_nonopt = d->optind;
+       d->__last_nonopt = d->optind;
       if (d->__first_nonopt > d->optind)
-        d->__first_nonopt = d->optind;
+       d->__first_nonopt = d->optind;
 
       if (d->__ordering == PERMUTE)
-        {
-          /* If we have just processed some options following some non-options,
-             exchange them so that the options come first.  */
+       {
+         /* If we have just processed some options following some non-options,
+            exchange them so that the options come first.  */
 
-          if (d->__first_nonopt != d->__last_nonopt
-              && d->__last_nonopt != d->optind)
-            exchange ((char **) argv, d);
-          else if (d->__last_nonopt != d->optind)
-            d->__first_nonopt = d->optind;
+         if (d->__first_nonopt != d->__last_nonopt
+             && d->__last_nonopt != d->optind)
+           exchange (argv, d);
+         else if (d->__last_nonopt != d->optind)
+           d->__first_nonopt = d->optind;
 
-          /* Skip any additional non-options
-             and extend the range of non-options previously skipped.  */
+         /* Skip any additional non-options
+            and extend the range of non-options previously skipped.  */
 
-          while (d->optind < argc && NONOPTION_P)
-            d->optind++;
-          d->__last_nonopt = d->optind;
-        }
+         while (d->optind < argc && NONOPTION_P)
+           d->optind++;
+         d->__last_nonopt = d->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.  */
+        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 (d->optind != argc && !strcmp (argv[d->optind], "--"))
-        {
-          d->optind++;
+       {
+         d->optind++;
 
-          if (d->__first_nonopt != d->__last_nonopt
-              && d->__last_nonopt != d->optind)
-            exchange ((char **) argv, d);
-          else if (d->__first_nonopt == d->__last_nonopt)
-            d->__first_nonopt = d->optind;
-          d->__last_nonopt = argc;
+         if (d->__first_nonopt != d->__last_nonopt
+             && d->__last_nonopt != d->optind)
+           exchange (argv, d);
+         else if (d->__first_nonopt == d->__last_nonopt)
+           d->__first_nonopt = d->optind;
+         d->__last_nonopt = argc;
 
-          d->optind = argc;
-        }
+         d->optind = argc;
+       }
 
       /* If we have done all the ARGV-elements, stop the scan
-         and back over any non-options that we skipped and permuted.  */
+        and back over any non-options that we skipped and permuted.  */
 
       if (d->optind == argc)
-        {
-          /* Set the next-arg-index to point at the non-options
-             that we previously skipped, so the caller will digest them.  */
-          if (d->__first_nonopt != d->__last_nonopt)
-            d->optind = d->__first_nonopt;
-          return -1;
-        }
+       {
+         /* Set the next-arg-index to point at the non-options
+            that we previously skipped, so the caller will digest them.  */
+         if (d->__first_nonopt != d->__last_nonopt)
+           d->optind = d->__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.  */
+        either stop the scan or describe it to the caller and pass it by.  */
 
       if (NONOPTION_P)
-        {
-          if (d->__ordering == REQUIRE_ORDER)
-            return -1;
-          d->optarg = argv[d->optind++];
-          return 1;
-        }
+       {
+         if (d->__ordering == REQUIRE_ORDER)
+           return -1;
+         d->optarg = argv[d->optind++];
+         return 1;
+       }
 
       /* We have found another option-ARGV-element.
-         Skip the initial punctuation.  */
-
-      d->__nextchar = (argv[d->optind] + 1
-                  + (longopts != NULL && argv[d->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[d->optind][1] == '-'
-          || (long_only && (argv[d->optind][2]
-                            || !strchr (optstring, argv[d->optind][1])))))
-    {
-      char *nameend;
-      unsigned int namelen;
-      const struct option *p;
-      const struct option *pfound = NULL;
-      struct option_list
-      {
-        const struct option *p;
-        struct option_list *next;
-      } *ambig_list = NULL;
-#ifdef _LIBC
-/* malloc() not used for _LIBC to simplify failure messages.  */
-# define free_option_list(l)
-#else
-# define free_option_list(l)                   \
-      while (l != NULL)                                \
-        {                                      \
-          struct option_list *pn = l->next;    \
-          free (l);                            \
-          l = pn;                              \
-        }
-#endif
-      int exact = 0;
-      int ambig = 0;
-      int indfound = -1;
-      int option_index;
-
-      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
-        /* Do nothing.  */ ;
-      namelen = nameend - d->__nextchar;
-
-      /* 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, d->__nextchar, namelen))
-          {
-            if (namelen == (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 (ambig)
-              ; /* Taking simpler path to handling ambiguities.  */
-            else if (long_only
-                     || pfound->has_arg != p->has_arg
-                     || pfound->flag != p->flag
-                     || pfound->val != p->val)
-              {
-                /* Second or later nonexact match found.  */
-#ifdef _LIBC
-                struct option_list *newp = alloca (sizeof (*newp));
-#else
-                struct option_list *newp = malloc (sizeof (*newp));
-                if (newp == NULL)
-                  {
-                    free_option_list (ambig_list);
-                    ambig_list = NULL;
-                    ambig = 1; /* Use simpler fallback message.  */
-                  }
-                else
-#endif
-                  {
-                    newp->p = p;
-                    newp->next = ambig_list;
-                    ambig_list = newp;
-                  }
-              }
-          }
-
-      if ((ambig || ambig_list) && !exact)
-        {
-          if (print_errors && ambig_list)
-            {
-              struct option_list first;
-              first.p = pfound;
-              first.next = ambig_list;
-              ambig_list = &first;
-
-#if defined _LIBC
-              char *buf = NULL;
-              size_t buflen = 0;
-
-              FILE *fp = __open_memstream (&buf, &buflen);
-              if (fp != NULL)
-                {
-                  fprintf (fp,
-                           _("%s: option '%s' is ambiguous; possibilities:"),
-                           argv[0], argv[d->optind]);
-
-                  do
-                    {
-                      fprintf (fp, " '--%s'", ambig_list->p->name);
-                      ambig_list = ambig_list->next;
-                    }
-                  while (ambig_list != NULL);
-
-                  fputc_unlocked ('\n', fp);
-
-                  if (__glibc_likely (fclose (fp) != EOF))
-                    {
-                      _IO_flockfile (stderr);
-
-                      int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                      ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
-
-                      __fxprintf (NULL, "%s", buf);
-
-                      ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                      _IO_funlockfile (stderr);
-
-                      free (buf);
-                    }
-                }
-#else
-              fprintf (stderr,
-                       _("%s: option '%s' is ambiguous; possibilities:"),
-                       argv[0], argv[d->optind]);
-              do
-                {
-                  fprintf (stderr, " '--%s'", ambig_list->p->name);
-                  ambig_list = ambig_list->next;
-                }
-              while (ambig_list != NULL);
-
-              fputc ('\n', stderr);
-#endif
-            }
-          else if (print_errors && ambig)
-            {
-              fprintf (stderr,
-                       _("%s: option '%s' is ambiguous\n"),
-                       argv[0], argv[d->optind]);
-            }
-          d->__nextchar += strlen (d->__nextchar);
-          d->optind++;
-          d->optopt = 0;
-          free_option_list (ambig_list);
-          return '?';
-        }
-
-      free_option_list (ambig_list);
-
-      if (pfound != NULL)
-        {
-          option_index = indfound;
-          d->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)
-                d->optarg = nameend + 1;
-              else
-                {
-                  if (print_errors)
-                    {
-#if defined _LIBC
-                      char *buf;
-                      int n;
-#endif
-
-                      if (argv[d->optind - 1][1] == '-')
-                        {
-                          /* --option */
-#if defined _LIBC
-                          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
-                          n = __asprintf (&buf, _("\
-%s: option '%c%s' doesn't allow an argument\n"),
-                                          argv[0], argv[d->optind - 1][0],
-                                          pfound->name);
-#else
-                          fprintf (stderr, _("\
-%s: option '%c%s' doesn't allow an argument\n"),
-                                   argv[0], argv[d->optind - 1][0],
-                                   pfound->name);
-#endif
-                        }
-
-#if defined _LIBC
-                      if (n >= 0)
-                        {
-                          _IO_flockfile (stderr);
-
-                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                          ((_IO_FILE *) stderr)->_flags2
-                            |= _IO_FLAGS2_NOTCANCEL;
-
-                          __fxprintf (NULL, "%s", buf);
-
-                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                          _IO_funlockfile (stderr);
-
-                          free (buf);
-                        }
-#endif
-                    }
-
-                  d->__nextchar += strlen (d->__nextchar);
-
-                  d->optopt = pfound->val;
-                  return '?';
-                }
-            }
-          else if (pfound->has_arg == 1)
-            {
-              if (d->optind < argc)
-                d->optarg = argv[d->optind++];
-              else
-                {
-                  if (print_errors)
-                    {
-#if defined _LIBC
-                      char *buf;
-
-                      if (__asprintf (&buf, _("\
-%s: option '--%s' requires an argument\n"),
-                                      argv[0], pfound->name) >= 0)
-                        {
-                          _IO_flockfile (stderr);
-
-                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                          ((_IO_FILE *) stderr)->_flags2
-                            |= _IO_FLAGS2_NOTCANCEL;
-
-                          __fxprintf (NULL, "%s", buf);
-
-                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                          _IO_funlockfile (stderr);
-
-                          free (buf);
-                        }
-#else
-                      fprintf (stderr,
-                               _("%s: option '--%s' requires an argument\n"),
-                               argv[0], pfound->name);
-#endif
-                    }
-                  d->__nextchar += strlen (d->__nextchar);
-                  d->optopt = pfound->val;
-                  return optstring[0] == ':' ? ':' : '?';
-                }
-            }
-          d->__nextchar += strlen (d->__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[d->optind][1] == '-'
-          || strchr (optstring, *d->__nextchar) == NULL)
-        {
-          if (print_errors)
-            {
-#if defined _LIBC
-              char *buf;
-              int n;
-#endif
-
-              if (argv[d->optind][1] == '-')
-                {
-                  /* --option */
-#if defined _LIBC
-                  n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
-                                  argv[0], d->__nextchar);
-#else
-                  fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
-                           argv[0], d->__nextchar);
-#endif
-                }
-              else
-                {
-                  /* +option or -option */
-#if defined _LIBC
-                  n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
-                                  argv[0], argv[d->optind][0], d->__nextchar);
-#else
-                  fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
-                           argv[0], argv[d->optind][0], d->__nextchar);
-#endif
-                }
-
-#if defined _LIBC
-              if (n >= 0)
-                {
-                  _IO_flockfile (stderr);
-
-                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
-
-                  __fxprintf (NULL, "%s", buf);
-
-                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                  _IO_funlockfile (stderr);
-
-                  free (buf);
-                }
-#endif
-            }
-          d->__nextchar = (char *) "";
-          d->optind++;
-          d->optopt = 0;
-          return '?';
-        }
+        Check whether it might be a long option.  */
+      if (longopts)
+       {
+         if (argv[d->optind][1] == '-')
+           {
+             /* "--foo" is always a long option.  The special option
+                "--" was handled above.  */
+             d->__nextchar = argv[d->optind] + 2;
+             return process_long_option (argc, argv, optstring, longopts,
+                                         longind, long_only, d,
+                                         print_errors, "--");
+           }
+
+         /* 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 (long_only && (argv[d->optind][2]
+                           || !strchr (optstring, argv[d->optind][1])))
+           {
+             int code;
+             d->__nextchar = argv[d->optind] + 1;
+             code = process_long_option (argc, argv, optstring, longopts,
+                                         longind, long_only, d,
+                                         print_errors, "-");
+             if (code != -1)
+               return code;
+           }
+       }
+
+      /* It is not a long option.  Skip the initial punctuation.  */
+      d->__nextchar = argv[d->optind] + 1;
     }
 
   /* Look at and handle the next short option-character.  */
@@ -824,331 +616,83 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
 
     if (temp == NULL || c == ':' || c == ';')
       {
-        if (print_errors)
-          {
-#if defined _LIBC
-              char *buf;
-              int n;
-#endif
-
-#if defined _LIBC
-              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
-            if (n >= 0)
-              {
-                _IO_flockfile (stderr);
-
-                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
-
-                __fxprintf (NULL, "%s", buf);
-
-                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                _IO_funlockfile (stderr);
-
-                free (buf);
-              }
-#endif
-          }
-        d->optopt = c;
-        return '?';
+       if (print_errors)
+         fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
+       d->optopt = c;
+       return '?';
       }
+
     /* Convenience. Treat POSIX -W foo same as long option --foo */
-    if (temp[0] == 'W' && temp[1] == ';')
+    if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
       {
-        char *nameend;
-        const struct option *p;
-        const struct option *pfound = NULL;
-        int exact = 0;
-        int ambig = 0;
-        int indfound = 0;
-        int option_index;
-
-        if (longopts == NULL)
-          goto no_longs;
-
-        /* This is an option that requires an argument.  */
-        if (*d->__nextchar != '\0')
-          {
-            d->optarg = d->__nextchar;
-            /* If we end this ARGV-element by taking the rest as an arg,
-               we must advance to the next element now.  */
-            d->optind++;
-          }
-        else if (d->optind == argc)
-          {
-            if (print_errors)
-              {
-#if defined _LIBC
-                char *buf;
-
-                if (__asprintf (&buf,
-                                _("%s: option requires an argument -- '%c'\n"),
-                                argv[0], c) >= 0)
-                  {
-                    _IO_flockfile (stderr);
-
-                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
-
-                    __fxprintf (NULL, "%s", buf);
-
-                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                    _IO_funlockfile (stderr);
-
-                    free (buf);
-                  }
-#else
-                fprintf (stderr,
-                         _("%s: option requires an argument -- '%c'\n"),
-                         argv[0], c);
-#endif
-              }
-            d->optopt = c;
-            if (optstring[0] == ':')
-              c = ':';
-            else
-              c = '?';
-            return c;
-          }
-        else
-          /* We already incremented 'd->optind' once;
-             increment it again when taking next ARGV-elt as argument.  */
-          d->optarg = argv[d->optind++];
-
-        /* optarg is now the argument, see if it's in the
-           table of longopts.  */
-
-        for (d->__nextchar = nameend = d->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, d->__nextchar, nameend - d->__nextchar))
-            {
-              if ((unsigned int) (nameend - d->__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 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
-                char *buf;
-
-                if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
-                                argv[0], d->optarg) >= 0)
-                  {
-                    _IO_flockfile (stderr);
-
-                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
-
-                    __fxprintf (NULL, "%s", buf);
-
-                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                    _IO_funlockfile (stderr);
-
-                    free (buf);
-                  }
-#else
-                fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
-                         argv[0], d->optarg);
-#endif
-              }
-            d->__nextchar += strlen (d->__nextchar);
-            d->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)
-                  d->optarg = nameend + 1;
-                else
-                  {
-                    if (print_errors)
-                      {
-#if defined _LIBC
-                        char *buf;
-
-                        if (__asprintf (&buf, _("\
-%s: option '-W %s' doesn't allow an argument\n"),
-                                        argv[0], pfound->name) >= 0)
-                          {
-                            _IO_flockfile (stderr);
-
-                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                            ((_IO_FILE *) stderr)->_flags2
-                              |= _IO_FLAGS2_NOTCANCEL;
-
-                            __fxprintf (NULL, "%s", buf);
-
-                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                            _IO_funlockfile (stderr);
-
-                            free (buf);
-                          }
-#else
-                        fprintf (stderr, _("\
-%s: option '-W %s' doesn't allow an argument\n"),
-                                 argv[0], pfound->name);
-#endif
-                      }
-
-                    d->__nextchar += strlen (d->__nextchar);
-                    return '?';
-                  }
-              }
-            else if (pfound->has_arg == 1)
-              {
-                if (d->optind < argc)
-                  d->optarg = argv[d->optind++];
-                else
-                  {
-                    if (print_errors)
-                      {
-#if defined _LIBC
-                        char *buf;
-
-                        if (__asprintf (&buf, _("\
-%s: option '-W %s' requires an argument\n"),
-                                        argv[0], pfound->name) >= 0)
-                          {
-                            _IO_flockfile (stderr);
-
-                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                            ((_IO_FILE *) stderr)->_flags2
-                              |= _IO_FLAGS2_NOTCANCEL;
-
-                            __fxprintf (NULL, "%s", buf);
-
-                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                            _IO_funlockfile (stderr);
-
-                            free (buf);
-                          }
-#else
-                        fprintf (stderr, _("\
-%s: option '-W %s' requires an argument\n"),
-                                 argv[0], pfound->name);
-#endif
-                      }
-                    d->__nextchar += strlen (d->__nextchar);
-                    return optstring[0] == ':' ? ':' : '?';
-                  }
-              }
-            else
-              d->optarg = NULL;
-            d->__nextchar += strlen (d->__nextchar);
-            if (longind != NULL)
-              *longind = option_index;
-            if (pfound->flag)
-              {
-                *(pfound->flag) = pfound->val;
-                return 0;
-              }
-            return pfound->val;
-          }
-
-      no_longs:
-        d->__nextchar = NULL;
-        return 'W';   /* Let the application handle it.   */
+       /* This is an option that requires an argument.  */
+       if (*d->__nextchar != '\0')
+         d->optarg = d->__nextchar;
+       else if (d->optind == argc)
+         {
+           if (print_errors)
+             fprintf (stderr,
+                      _("%s: option requires an argument -- '%c'\n"),
+                      argv[0], c);
+
+           d->optopt = c;
+           if (optstring[0] == ':')
+             c = ':';
+           else
+             c = '?';
+           return c;
+         }
+       else
+         d->optarg = argv[d->optind];
+
+       d->__nextchar = d->optarg;
+       d->optarg = NULL;
+       return process_long_option (argc, argv, optstring, longopts, longind,
+                                   0 /* long_only */, d, print_errors, "-W ");
       }
     if (temp[1] == ':')
       {
-        if (temp[2] == ':')
-          {
-            /* This is an option that accepts an argument optionally.  */
-            if (*d->__nextchar != '\0')
-              {
-                d->optarg = d->__nextchar;
-                d->optind++;
-              }
-            else
-              d->optarg = NULL;
-            d->__nextchar = NULL;
-          }
-        else
-          {
-            /* This is an option that requires an argument.  */
-            if (*d->__nextchar != '\0')
-              {
-                d->optarg = d->__nextchar;
-                /* If we end this ARGV-element by taking the rest as an arg,
-                   we must advance to the next element now.  */
-                d->optind++;
-              }
-            else if (d->optind == argc)
-              {
-                if (print_errors)
-                  {
-#if defined _LIBC
-                    char *buf;
-
-                    if (__asprintf (&buf, _("\
-%s: option requires an argument -- '%c'\n"),
-                                    argv[0], c) >= 0)
-                      {
-                        _IO_flockfile (stderr);
-
-                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
-                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
-
-                        __fxprintf (NULL, "%s", buf);
-
-                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
-                        _IO_funlockfile (stderr);
-
-                        free (buf);
-                      }
-#else
-                    fprintf (stderr,
-                             _("%s: option requires an argument -- '%c'\n"),
-                             argv[0], c);
-#endif
-                  }
-                d->optopt = c;
-                if (optstring[0] == ':')
-                  c = ':';
-                else
-                  c = '?';
-              }
-            else
-              /* We already incremented 'optind' once;
-                 increment it again when taking next ARGV-elt as argument.  */
-              d->optarg = argv[d->optind++];
-            d->__nextchar = NULL;
-          }
+       if (temp[2] == ':')
+         {
+           /* This is an option that accepts an argument optionally.  */
+           if (*d->__nextchar != '\0')
+             {
+               d->optarg = d->__nextchar;
+               d->optind++;
+             }
+           else
+             d->optarg = NULL;
+           d->__nextchar = NULL;
+         }
+       else
+         {
+           /* This is an option that requires an argument.  */
+           if (*d->__nextchar != '\0')
+             {
+               d->optarg = d->__nextchar;
+               /* If we end this ARGV-element by taking the rest as an arg,
+                  we must advance to the next element now.  */
+               d->optind++;
+             }
+           else if (d->optind == argc)
+             {
+               if (print_errors)
+                 fprintf (stderr,
+                          _("%s: option requires an argument -- '%c'\n"),
+                          argv[0], c);
+
+               d->optopt = c;
+               if (optstring[0] == ':')
+                 c = ':';
+               else
+                 c = '?';
+             }
+           else
+             /* We already incremented 'optind' once;
+                increment it again when taking next ARGV-elt as argument.  */
+             d->optarg = argv[d->optind++];
+           d->__nextchar = NULL;
+         }
       }
     return c;
   }
@@ -1156,8 +700,8 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
 
 int
 _getopt_internal (int argc, char **argv, const char *optstring,
-                  const struct option *longopts, int *longind, int long_only,
-                  int posixly_correct)
+                 const struct option *longopts, int *longind, int long_only,
+                 int posixly_correct)
 {
   int result;
 
@@ -1165,8 +709,8 @@ _getopt_internal (int argc, char **argv, const char 
*optstring,
   getopt_data.opterr = opterr;
 
   result = _getopt_internal_r (argc, argv, optstring, longopts,
-                               longind, long_only, &getopt_data,
-                               posixly_correct);
+                              longind, long_only, &getopt_data,
+                              posixly_correct);
 
   optind = getopt_data.optind;
   optarg = getopt_data.optarg;
@@ -1175,32 +719,23 @@ _getopt_internal (int argc, char **argv, const char 
*optstring,
   return result;
 }
 
-/* glibc gets a LSB-compliant getopt.
-   Standalone applications get a POSIX-compliant getopt.  */
-#if _LIBC
-enum { POSIXLY_CORRECT = 0 };
-#else
-enum { POSIXLY_CORRECT = 1 };
-#endif
-
-int
-getopt (int argc, char *const *argv, const char *optstring)
-{
-  return _getopt_internal (argc, (char **) argv, optstring,
-                           (const struct option *) 0,
-                           (int *) 0,
-                           0, POSIXLY_CORRECT);
-}
+/* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
+   Standalone applications just get a POSIX-compliant getopt.
+   POSIX and LSB both require these functions to take 'char *const *argv'
+   even though this is incorrect (because of the permutation).  */
+#define GETOPT_ENTRY(NAME, POSIXLY_CORRECT)                    \
+  int                                                          \
+  NAME (int argc, char *const *argv, const char *optstring)    \
+  {                                                            \
+    return _getopt_internal (argc, (char **)argv, optstring,   \
+                            0, 0, 0, POSIXLY_CORRECT);         \
+  }
 
 #ifdef _LIBC
-int
-__posix_getopt (int argc, char *const *argv, const char *optstring)
-{
-  return _getopt_internal (argc, argv, optstring,
-                           (const struct option *) 0,
-                           (int *) 0,
-                           0, 1);
-}
+GETOPT_ENTRY(getopt, 0)
+GETOPT_ENTRY(__posix_getopt, 1)
+#else
+GETOPT_ENTRY(getopt, 1)
 #endif
 
 
@@ -1221,51 +756,51 @@ main (int argc, char **argv)
 
       c = getopt (argc, argv, "abc:d:0123456789");
       if (c == -1)
-        break;
+       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);
-        }
+       {
+       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 ("%s ", argv[optind++]);
       printf ("\n");
     }
 
diff --git a/lib/getopt.in.h b/lib/getopt.in.h
index 3f57c68..91e086c 100644
--- a/lib/getopt.in.h
+++ b/lib/getopt.in.h
@@ -1,20 +1,22 @@
 /* Declarations for getopt.
    Copyright (C) 1989-2017 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 General Public
-   License as published by the Free Software Foundation; either
-   version 3 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
+   This file is part of gnulib.
+   Unlike most of the getopt implementation, it is NOT shared
+   with the GNU C Library, which supplies a different version of
+   this file.
+
+   gnulib is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 3 of
+   the License, or (at your option) any later version.
+
+   gnulib 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
    General Public License for more details.
 
    You should have received a copy of the GNU General Public
-   License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   License along with gnulib; if not, see <http://www.gnu.org/licenses/>.  */
 
 #ifndef address@hidden@_GETOPT_H
 
@@ -32,249 +34,26 @@
 # undef _GL_SYSTEM_GETOPT
 #endif
 
-#ifndef address@hidden@_GETOPT_H
-
-#ifndef __need_getopt
-# define address@hidden@_GETOPT_H 1
-#endif
+#define address@hidden@_GETOPT_H 1
 
 /* Standalone applications should #define __GETOPT_PREFIX to an
    identifier that prefixes the external functions and variables
-   defined in this header.  When this happens, include the
-   headers that might declare getopt so that they will not cause
-   confusion if included after this file (if the system had <getopt.h>,
-   we have already included it).  Then systematically rename
-   identifiers so that they do not collide with the system functions
-   and variables.  Renaming avoids problems with some compilers and
-   linkers.  */
+   defined in getopt_core.h and getopt_ext.h.  When this happens,
+   include the headers that might declare getopt so that they will not
+   cause confusion if included after this file (if the system had
+   <getopt.h>, we have already included it).  */
 #if defined __GETOPT_PREFIX
-# if !defined __need_getopt
-#  if address@hidden@
-#   define __need_system_stdlib_h
-#   include <stdlib.h>
-#   undef __need_system_stdlib_h
-#   include <stdio.h>
-#   include <unistd.h>
-#  endif
-#  undef __need_getopt
-# endif
-# undef __GETOPT_CONCAT
-# undef __GETOPT_XCONCAT
-# undef __GETOPT_ID
-# undef getopt
-# undef getopt_long
-# undef getopt_long_only
-# undef optarg
-# undef opterr
-# undef optind
-# undef optopt
-# undef option
-# undef _getopt_internal
-# define __GETOPT_CONCAT(x, y) x ## y
-# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
-# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
-# define getopt __GETOPT_ID (getopt)
-# define getopt_long __GETOPT_ID (getopt_long)
-# define getopt_long_only __GETOPT_ID (getopt_long_only)
-# define optarg __GETOPT_ID (optarg)
-# define opterr __GETOPT_ID (opterr)
-# define optind __GETOPT_ID (optind)
-# define optopt __GETOPT_ID (optopt)
-# define option __GETOPT_ID (option)
-# define _getopt_internal __GETOPT_ID (getopt_internal)
-#endif
-
-/* Standalone applications get correct prototypes for getopt_long and
-   getopt_long_only; they declare "char **argv".  libc uses prototypes
-   with "char *const *argv" that are incorrect because getopt_long and
-   getopt_long_only can permute argv; this is required for backward
-   compatibility (e.g., for LSB 2.0.1).
-
-   This used to be '#if defined __GETOPT_PREFIX && !defined __need_getopt',
-   but it caused redefinition warnings if both unistd.h and getopt.h were
-   included, since unistd.h includes getopt.h having previously defined
-   __need_getopt.
-
-   The only place where __getopt_argv_const is used is in definitions
-   of getopt_long and getopt_long_only below, but these are visible
-   only if __need_getopt is not defined, so it is quite safe to rewrite
-   the conditional as follows:
-*/
-#if !defined __need_getopt
-# if defined __GETOPT_PREFIX
-#  define __getopt_argv_const /* empty */
-# else
-#  define __getopt_argv_const const
-# endif
-#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 <features.h>, but
-   that does not exist if we are standalone.  So: if __GNU_LIBRARY__ is
-   not defined, include <ctype.h>, which will pull in <features.h> 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 <ctype.h>
-#endif
-
-#ifndef __THROW
-# ifndef __GNUC_PREREQ
-#  define __GNUC_PREREQ(maj, min) (0)
-# endif
-# if defined __cplusplus && __GNUC_PREREQ (2,8)
-#  define __THROW       throw ()
-# else
-#  define __THROW
+# if address@hidden@
+#  define __need_system_stdlib_h
+#  include <stdlib.h>
+#  undef __need_system_stdlib_h
+#  include <stdio.h>
+#  include <unistd.h>
 # endif
 #endif
 
-/* The definition of _GL_ARG_NONNULL is copied here.  */
-
-#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.  */
+#include <getopt_cdefs.h>
+#include <getopt_pfx_core.h>
+#include <getopt_pfx_ext.h>
 
-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.  */
-
-# if !GNULIB_defined_struct_option
-struct option
-{
-  const char *name;
-  /* 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;
-};
-#  define GNULIB_defined_struct_option 1
-# endif
-
-/* 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 '\1'.  This behavior is specific to the GNU
-   'getopt'.  If OPTS begins with '+', or POSIXLY_CORRECT is set in
-   the environment, then do not permute arguments.  */
-
-extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
-       __THROW _GL_ARG_NONNULL ((2, 3));
-
-#ifndef __GETOPT_PREFIX
-# if defined __need_getopt && defined __USE_POSIX2 \
-  && !defined __USE_POSIX_IMPLICITLY && !defined __USE_GNU
-/* The GNU getopt has more functionality than the standard version.  The
-   additional functionality can be disable at runtime.  This redirection
-   helps to also do this at runtime.  */
-#  ifdef __REDIRECT
-  extern int __REDIRECT_NTH (getopt, (int ___argc, char *const *___argv,
-                                     const char *__shortopts),
-                            __posix_getopt);
-#  else
-extern int __posix_getopt (int ___argc, char *const *___argv,
-                          const char *__shortopts)
-  __THROW _GL_ARG_NONNULL ((2, 3));
-#   define getopt __posix_getopt
-#  endif
-# endif
-#endif
-
-#ifndef __need_getopt
-extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
-                        const char *__shortopts,
-                        const struct option *__longopts, int *__longind)
-       __THROW _GL_ARG_NONNULL ((2, 3));
-extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
-                             const char *__shortopts,
-                             const struct option *__longopts, int *__longind)
-       __THROW _GL_ARG_NONNULL ((2, 3));
-
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-/* Make sure we later can get all the definitions and declarations.  */
-#undef __need_getopt
-
-#endif /* address@hidden@_GETOPT_H */
 #endif /* address@hidden@_GETOPT_H */
diff --git a/lib/getopt1.c b/lib/getopt1.c
index 166b129..2bc5926 100644
--- a/lib/getopt1.c
+++ b/lib/getopt1.c
@@ -1,6 +1,7 @@
 /* getopt_long and getopt_long_only entry points for GNU getopt.
    Copyright (C) 1987-2017 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
+   This file is part of the GNU C Library and is also part of gnulib.
+   Patches to this file should be submitted to both projects.
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
@@ -16,41 +17,28 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-#ifdef _LIBC
-# include <getopt.h>
-#else
+#ifndef _LIBC
 # include <config.h>
-# include "getopt.h"
 #endif
-#include "getopt_int.h"
-
-#include <stdio.h>
 
-/* This needs to come after some library #include
-   to get __GNU_LIBRARY__ defined.  */
-#ifdef __GNU_LIBRARY__
-#include <stdlib.h>
-#endif
-
-#ifndef NULL
-#define NULL 0
-#endif
+#include "getopt.h"
+#include "getopt_int.h"
 
 int
 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
-             const struct option *long_options, int *opt_index)
+            const struct option *long_options, int *opt_index)
 {
   return _getopt_internal (argc, (char **) argv, options, long_options,
-                           opt_index, 0, 0);
+                          opt_index, 0, 0);
 }
 
 int
 _getopt_long_r (int argc, char **argv, const char *options,
-                const struct option *long_options, int *opt_index,
-                struct _getopt_data *d)
+               const struct option *long_options, int *opt_index,
+               struct _getopt_data *d)
 {
   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
-                             0, d, 0);
+                            0, d, 0);
 }
 
 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
@@ -60,26 +48,27 @@ _getopt_long_r (int argc, char **argv, const char *options,
 
 int
 getopt_long_only (int argc, char *__getopt_argv_const *argv,
-                  const char *options,
-                  const struct option *long_options, int *opt_index)
+                 const char *options,
+                 const struct option *long_options, int *opt_index)
 {
   return _getopt_internal (argc, (char **) argv, options, long_options,
-                           opt_index, 1, 0);
+                          opt_index, 1, 0);
 }
 
 int
 _getopt_long_only_r (int argc, char **argv, const char *options,
-                     const struct option *long_options, int *opt_index,
-                     struct _getopt_data *d)
+                    const struct option *long_options, int *opt_index,
+                    struct _getopt_data *d)
 {
   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
-                             1, d, 0);
+                            1, d, 0);
 }
 
 
 #ifdef TEST
 
 #include <stdio.h>
+#include <stdlib.h>
 
 int
 main (int argc, char **argv)
@@ -93,74 +82,74 @@ main (int argc, char **argv)
       int option_index = 0;
       static const 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}
+       {"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);
+                      long_options, &option_index);
       if (c == -1)
-        break;
+       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);
-        }
+       {
+       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 ("%s ", argv[optind++]);
       printf ("\n");
     }
 
diff --git a/lib/getopt_cdefs.in.h b/lib/getopt_cdefs.in.h
new file mode 100644
index 0000000..18f9fb4
--- /dev/null
+++ b/lib/getopt_cdefs.in.h
@@ -0,0 +1,75 @@
+/* getopt-on-non-glibc compatibility macros.
+   Copyright (C) 1989-2017 Free Software Foundation, Inc.
+   This file is part of gnulib.
+   Unlike most of the getopt implementation, it is NOT shared
+   with the GNU C Library.
+
+   gnulib is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 3 of
+   the License, or (at your option) any later version.
+
+   gnulib 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
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public
+   License along with gnulib; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _GETOPT_CDEFS_H
+#define _GETOPT_CDEFS_H 1
+
+/* This header should not be used directly; include getopt.h or
+   unistd.h instead.  It does not have a protective #error, because
+   the guard macro for getopt.h in gnulib is not fixed.  */
+
+/* getopt_core.h and getopt_ext.h are shared with GNU libc, and expect
+   a number of the internal macros supplied to GNU libc's headers by
+   sys/cdefs.h.  Provide fallback definitions for all of them.  */
+#if @HAVE_SYS_CDEFS_H@
+# include <sys/cdefs.h>
+#endif
+
+#ifndef __BEGIN_DECLS
+# ifdef __cplusplus
+#  define __BEGIN_DECLS extern "C" {
+# else
+#  define __BEGIN_DECLS /* nothing */
+# endif
+#endif
+#ifndef __END_DECLS
+# ifdef __cplusplus
+#  define __END_DECLS }
+# else
+#  define __END_DECLS /* nothing */
+# endif
+#endif
+
+#ifndef __GNUC_PREREQ
+# if defined __GNUC__ && defined __GNUC_VERSION__
+# define __GNUC_PREREQ(maj, min) \
+        ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
+# else
+#  define __GNUC_PREREQ(maj, min) 0
+# endif
+#endif
+
+#ifndef __THROW
+# if defined __cplusplus && __GNUC_PREREQ (2,8)
+#  define __THROW       throw ()
+# else
+#  define __THROW
+# endif
+#endif
+
+#ifndef __nonnull
+# if __GNUC_PREREQ (3,3)
+#  define __nonnull(params) __attribute__ ((__nonnull__ params))
+# else
+#  define __nonnull(params)
+# endif
+#endif
+
+#endif /* getopt_cdefs.h */
diff --git a/lib/getopt_core.h b/lib/getopt_core.h
new file mode 100644
index 0000000..d6ef8a6
--- /dev/null
+++ b/lib/getopt_core.h
@@ -0,0 +1,96 @@
+/* Declarations for getopt (basic, portable features only).
+   Copyright (C) 1989-2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library and is also part of gnulib.
+   Patches to this file should be submitted to both projects.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public
+   License as published by the Free Software Foundation; either
+   version 3 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
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _GETOPT_CORE_H
+#define _GETOPT_CORE_H 1
+
+/* This header should not be used directly; include getopt.h or
+   unistd.h instead.  Unlike most bits headers, it does not have
+   a protective #error, because the guard macro for getopt.h in
+   gnulib is not fixed.  */
+
+__BEGIN_DECLS
+
+/* 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;
+
+/* 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 '\1'.  This behavior is specific to the GNU
+   'getopt'.  If OPTS begins with '+', or POSIXLY_CORRECT is set in
+   the environment, then do not permute arguments.
+
+   For standards compliance, the 'argv' argument has the type
+   char *const *, but this is inaccurate; if argument permutation is
+   enabled, the argv array (not the strings it points to) must be
+   writable.  */
+
+extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
+       __THROW __nonnull ((2, 3));
+
+__END_DECLS
+
+#endif /* getopt_core.h */
diff --git a/lib/getopt_ext.h b/lib/getopt_ext.h
new file mode 100644
index 0000000..79b7a67
--- /dev/null
+++ b/lib/getopt_ext.h
@@ -0,0 +1,77 @@
+/* Declarations for getopt (GNU extensions).
+   Copyright (C) 1989-2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library and is also part of gnulib.
+   Patches to this file should be submitted to both projects.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public
+   License as published by the Free Software Foundation; either
+   version 3 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
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _GETOPT_EXT_H
+#define _GETOPT_EXT_H 1
+
+/* This header should not be used directly; include getopt.h instead.
+   Unlike most bits headers, it does not have a protective #error,
+   because the guard macro for getopt.h in gnulib is not fixed.  */
+
+__BEGIN_DECLS
+
+/* 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
+{
+  const char *name;
+  /* 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
+
+extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
+                       const char *__shortopts,
+                       const struct option *__longopts, int *__longind)
+       __THROW __nonnull ((2, 3));
+extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
+                            const char *__shortopts,
+                            const struct option *__longopts, int *__longind)
+       __THROW __nonnull ((2, 3));
+
+__END_DECLS
+
+#endif /* getopt_ext.h */
diff --git a/lib/getopt_int.h b/lib/getopt_int.h
index c614fcb..a556219 100644
--- a/lib/getopt_int.h
+++ b/lib/getopt_int.h
@@ -1,6 +1,7 @@
 /* Internal declarations for getopt.
    Copyright (C) 1989-2017 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
+   This file is part of the GNU C Library and is also part of gnulib.
+   Patches to this file should be submitted to both projects.
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
@@ -17,14 +18,14 @@
    <http://www.gnu.org/licenses/>.  */
 
 #ifndef _GETOPT_INT_H
-#define _GETOPT_INT_H   1
+#define _GETOPT_INT_H  1
 
 #include <getopt.h>
 
 extern int _getopt_internal (int ___argc, char **___argv,
-                             const char *__shortopts,
-                             const struct option *__longopts, int *__longind,
-                             int __long_only, int __posixly_correct);
+                            const char *__shortopts,
+                            const struct option *__longopts, int *__longind,
+                            int __long_only, int __posixly_correct);
 
 
 /* Reentrant versions which can handle parsing multiple argument
@@ -32,28 +33,20 @@ extern int _getopt_internal (int ___argc, char **___argv,
 
 /* 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 POSIX
+   specifies should happen.
 
-   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, or by calling getopt.
-
-   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.
+   PERMUTE means 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.
+   with character code 1.
 
    The special argument '--' forces an end of option-scanning regardless
    of the value of 'ordering'.  In the case of RETURN_IN_ORDER, only
@@ -91,11 +84,6 @@ struct _getopt_data
   /* See __ord above.  */
   enum __ord __ordering;
 
-  /* If the POSIXLY_CORRECT environment variable is set
-     or getopt was called.  */
-  int __posixly_correct;
-
-
   /* Handle permutation of arguments.  */
 
   /* Describe the part of ARGV that contains non-options that have
@@ -104,32 +92,27 @@ struct _getopt_data
 
   int __first_nonopt;
   int __last_nonopt;
-
-#if defined _LIBC && defined USE_NONOPTION_FLAGS
-  int __nonoption_flags_max_len;
-  int __nonoption_flags_len;
-#endif
 };
 
 /* The initializer is necessary to set OPTIND and OPTERR to their
    default values and to clear the initialization flag.  */
-#define _GETOPT_DATA_INITIALIZER        { 1, 1 }
+#define _GETOPT_DATA_INITIALIZER       { 1, 1 }
 
 extern int _getopt_internal_r (int ___argc, char **___argv,
-                               const char *__shortopts,
-                               const struct option *__longopts, int *__longind,
-                               int __long_only, struct _getopt_data *__data,
-                               int __posixly_correct);
+                              const char *__shortopts,
+                              const struct option *__longopts, int *__longind,
+                              int __long_only, struct _getopt_data *__data,
+                              int __posixly_correct);
 
 extern int _getopt_long_r (int ___argc, char **___argv,
-                           const char *__shortopts,
-                           const struct option *__longopts, int *__longind,
-                           struct _getopt_data *__data);
+                          const char *__shortopts,
+                          const struct option *__longopts, int *__longind,
+                          struct _getopt_data *__data);
 
 extern int _getopt_long_only_r (int ___argc, char **___argv,
-                                const char *__shortopts,
-                                const struct option *__longopts,
-                                int *__longind,
-                                struct _getopt_data *__data);
+                               const char *__shortopts,
+                               const struct option *__longopts,
+                               int *__longind,
+                               struct _getopt_data *__data);
 
 #endif /* getopt_int.h */
diff --git a/lib/getopt_pfx_core.h b/lib/getopt_pfx_core.h
new file mode 100644
index 0000000..d585a08
--- /dev/null
+++ b/lib/getopt_pfx_core.h
@@ -0,0 +1,54 @@
+/* getopt (basic, portable features) gnulib wrapper header.
+   Copyright (C) 1989-2017 Free Software Foundation, Inc.
+   This file is part of gnulib.
+   Unlike most of the getopt implementation, it is NOT shared
+   with the GNU C Library.
+
+   gnulib is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 3 of
+   the License, or (at your option) any later version.
+
+   gnulib 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
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public
+   License along with gnulib; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _GETOPT_PFX_CORE_H
+#define _GETOPT_PFX_CORE_H 1
+
+/* This header should not be used directly; include getopt.h or
+   unistd.h instead.  It does not have a protective #error, because
+   the guard macro for getopt.h in gnulib is not fixed.  */
+
+/* Standalone applications should #define __GETOPT_PREFIX to an
+   identifier that prefixes the external functions and variables
+   defined in getopt_core.h and getopt_ext.h.  Systematically
+   rename identifiers so that they do not collide with the system
+   functions and variables.  Renaming avoids problems with some
+   compilers and linkers.  */
+#ifdef __GETOPT_PREFIX
+# ifndef __GETOPT_ID
+#  define __GETOPT_CONCAT(x, y) x ## y
+#  define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
+#  define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
+# endif
+# undef getopt
+# undef optarg
+# undef opterr
+# undef optind
+# undef optopt
+# define getopt __GETOPT_ID (getopt)
+# define optarg __GETOPT_ID (optarg)
+# define opterr __GETOPT_ID (opterr)
+# define optind __GETOPT_ID (optind)
+# define optopt __GETOPT_ID (optopt)
+#endif
+
+#include <getopt_core.h>
+
+#endif /* getopt_pfx_core.h */
diff --git a/lib/getopt_pfx_ext.h b/lib/getopt_pfx_ext.h
new file mode 100644
index 0000000..3da156a
--- /dev/null
+++ b/lib/getopt_pfx_ext.h
@@ -0,0 +1,64 @@
+/* getopt (GNU extensions) gnulib wrapper header.
+   Copyright (C) 1989-2017 Free Software Foundation, Inc.
+   This file is part of gnulib.
+   Unlike most of the getopt implementation, it is NOT shared
+   with the GNU C Library.
+
+   gnulib is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 3 of
+   the License, or (at your option) any later version.
+
+   gnulib 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
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public
+   License along with gnulib; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _GETOPT_PFX_EXT_H
+#define _GETOPT_PFX_EXT_H 1
+
+/* This header should not be used directly; include getopt.h instead.
+   It does not have a protective #error, because the guard macro for
+   getopt.h in gnulib is not fixed.  */
+
+/* Standalone applications should #define __GETOPT_PREFIX to an
+   identifier that prefixes the external functions and variables
+   defined in getopt_core.h and getopt_ext.h.  Systematically
+   rename identifiers so that they do not collide with the system
+   functions and variables.  Renaming avoids problems with some
+   compilers and linkers.  */
+#ifdef __GETOPT_PREFIX
+# ifndef __GETOPT_ID
+#  define __GETOPT_CONCAT(x, y) x ## y
+#  define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
+#  define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
+# endif
+# undef getopt_long
+# undef getopt_long_only
+# undef option
+# define getopt_long __GETOPT_ID (getopt_long)
+# define getopt_long_only __GETOPT_ID (getopt_long_only)
+# define option __GETOPT_ID (option)
+#endif
+
+/* Standalone applications get correct prototypes for getopt_long and
+   getopt_long_only; they declare "char **argv".  For backward
+   compatibility with old applications, if __GETOPT_PREFIX is not
+   defined, we supply GNU-libc-compatible, but incorrect, prototypes
+   using "char *const *argv".  (GNU libc is stuck with the incorrect
+   prototypes, as they are baked into older versions of LSB.)  */
+#ifndef __getopt_argv_const
+# if defined __GETOPT_PREFIX
+#  define __getopt_argv_const /* empty */
+# else
+#  define __getopt_argv_const const
+# endif
+#endif
+
+#include <getopt_ext.h>
+
+#endif /* getopt_pfx_ext.h */
diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index ca95ed7..38adf2a 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -84,6 +84,7 @@ EOVERFLOW_VALUE = @EOVERFLOW_VALUE@
 ERRNO_H = @ERRNO_H@
 EXECINFO_H = @EXECINFO_H@
 EXEEXT = @EXEEXT@
+FIND_DELETE = @FIND_DELETE@
 FIRSTFILE_OBJ = @FIRSTFILE_OBJ@
 FONTCONFIG_CFLAGS = @FONTCONFIG_CFLAGS@
 FONTCONFIG_LIBS = @FONTCONFIG_LIBS@
@@ -94,6 +95,7 @@ GCONF_CFLAGS = @GCONF_CFLAGS@
 GCONF_LIBS = @GCONF_LIBS@
 GETADDRINFO_A_LIBS = @GETADDRINFO_A_LIBS@
 GETLOADAVG_LIBS = @GETLOADAVG_LIBS@
+GETOPT_CDEFS_H = @GETOPT_CDEFS_H@
 GETOPT_H = @GETOPT_H@
 GFILENOTIFY_CFLAGS = @GFILENOTIFY_CFLAGS@
 GFILENOTIFY_LIBS = @GFILENOTIFY_LIBS@
@@ -488,6 +490,7 @@ HAVE_STRVERSCMP = @HAVE_STRVERSCMP@
 HAVE_SYMLINK = @HAVE_SYMLINK@
 HAVE_SYMLINKAT = @HAVE_SYMLINKAT@
 HAVE_SYS_BITYPES_H = @HAVE_SYS_BITYPES_H@
+HAVE_SYS_CDEFS_H = @HAVE_SYS_CDEFS_H@
 HAVE_SYS_INTTYPES_H = @HAVE_SYS_INTTYPES_H@
 HAVE_SYS_LOADAVG_H = @HAVE_SYS_LOADAVG_H@
 HAVE_SYS_PARAM_H = @HAVE_SYS_PARAM_H@
@@ -1511,11 +1514,11 @@ endif
 ## begin gnulib module getopt-posix
 ifeq (,$(OMIT_GNULIB_MODULE_getopt-posix))
 
-BUILT_SOURCES += $(GETOPT_H)
+BUILT_SOURCES += $(GETOPT_H) $(GETOPT_CDEFS_H)
 
 # We need the following in order to create <getopt.h> when the system
 # doesn't have one that works with the given compiler.
-getopt.h: getopt.in.h $(top_builddir)/config.status $(ARG_NONNULL_H)
+getopt.h: getopt.in.h $(top_builddir)/config.status
        $(AM_V_GEN)rm -f address@hidden $@ && \
        { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
          sed -e 's|@''GUARD_PREFIX''@|GL|g' \
@@ -1524,13 +1527,21 @@ getopt.h: getopt.in.h $(top_builddir)/config.status 
$(ARG_NONNULL_H)
              -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
              -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
              -e 's|@''NEXT_GETOPT_H''@|$(NEXT_GETOPT_H)|g' \
-             -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
              < $(srcdir)/getopt.in.h; \
        } > address@hidden && \
        mv -f address@hidden $@
-MOSTLYCLEANFILES += getopt.h getopt.h-t
 
-EXTRA_DIST += getopt.c getopt.in.h getopt1.c getopt_int.h
+getopt_cdefs.h: getopt_cdefs.in.h $(top_builddir)/config.status
+       $(AM_V_GEN)rm -f address@hidden $@ && \
+       { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
+          sed -e 's|@''HAVE_SYS_CDEFS_H''@|$(HAVE_SYS_CDEFS_H)|g' \
+             < $(srcdir)/getopt_cdefs.in.h; \
+       } > address@hidden && \
+       mv -f address@hidden $@
+
+MOSTLYCLEANFILES += getopt.h getopt.h-t getopt_cdefs.h getopt_cdefs.h-t
+
+EXTRA_DIST += getopt.c getopt.in.h getopt1.c getopt_cdefs.in.h getopt_core.h 
getopt_ext.h getopt_int.h getopt_pfx_core.h getopt_pfx_ext.h
 
 EXTRA_libgnu_a_SOURCES += getopt.c getopt1.c
 
diff --git a/lib/md5.c b/lib/md5.c
index e82b051..dcbba45 100644
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -246,7 +246,8 @@ md5_process_bytes (const void *buffer, size_t len, struct 
md5_ctx *ctx)
           md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
           ctx->buflen &= 63;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~63],
                   ctx->buflen);
@@ -288,6 +289,8 @@ md5_process_bytes (const void *buffer, size_t len, struct 
md5_ctx *ctx)
         {
           md5_process_block (ctx->buffer, 64, ctx);
           left_over -= 64;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 64.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
diff --git a/lib/md5.h b/lib/md5.h
index fb20b7b..e38a619 100644
--- a/lib/md5.h
+++ b/lib/md5.h
@@ -74,8 +74,8 @@ struct md5_ctx
   uint32_t D;
 
   uint32_t total[2];
-  uint32_t buflen;
-  uint32_t buffer[32];
+  uint32_t buflen;     /* ≥ 0, ≤ 128 */
+  uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
 };
 
 /*
diff --git a/lib/sha1.c b/lib/sha1.c
index ea0474f..a578141 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -233,7 +233,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct 
sha1_ctx *ctx)
           sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
           ctx->buflen &= 63;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~63],
                   ctx->buflen);
@@ -275,6 +276,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct 
sha1_ctx *ctx)
         {
           sha1_process_block (ctx->buffer, 64, ctx);
           left_over -= 64;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 64.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
diff --git a/lib/sha1.h b/lib/sha1.h
index 7d7e19a..fcef9ce 100644
--- a/lib/sha1.h
+++ b/lib/sha1.h
@@ -46,8 +46,8 @@ struct sha1_ctx
   uint32_t E;
 
   uint32_t total[2];
-  uint32_t buflen;
-  uint32_t buffer[32];
+  uint32_t buflen;     /* ≥ 0, ≤ 128 */
+  uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
 };
 
 /* Initialize structure containing state of computation. */
diff --git a/lib/sha256.c b/lib/sha256.c
index 03d3899..c0fb8be 100644
--- a/lib/sha256.c
+++ b/lib/sha256.c
@@ -366,7 +366,8 @@ sha256_process_bytes (const void *buffer, size_t len, 
struct sha256_ctx *ctx)
           sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
           ctx->buflen &= 63;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~63],
                   ctx->buflen);
@@ -408,6 +409,8 @@ sha256_process_bytes (const void *buffer, size_t len, 
struct sha256_ctx *ctx)
         {
           sha256_process_block (ctx->buffer, 64, ctx);
           left_over -= 64;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 64.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
diff --git a/lib/sha256.h b/lib/sha256.h
index ffb91fa..348b76e 100644
--- a/lib/sha256.h
+++ b/lib/sha256.h
@@ -44,8 +44,8 @@ struct sha256_ctx
   uint32_t state[8];
 
   uint32_t total[2];
-  size_t buflen;
-  uint32_t buffer[32];
+  size_t buflen;       /* ≥ 0, ≤ 128 */
+  uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
 };
 
 /* Initialize structure containing state of computation. */
diff --git a/lib/sha512.c b/lib/sha512.c
index 6876bfd..dbde671 100644
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -374,7 +374,8 @@ sha512_process_bytes (const void *buffer, size_t len, 
struct sha512_ctx *ctx)
           sha512_process_block (ctx->buffer, ctx->buflen & ~127, ctx);
 
           ctx->buflen &= 127;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 128 ≤ (left_over + add) & ~127.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~127],
                   ctx->buflen);
@@ -416,6 +417,8 @@ sha512_process_bytes (const void *buffer, size_t len, 
struct sha512_ctx *ctx)
         {
           sha512_process_block (ctx->buffer, 128, ctx);
           left_over -= 128;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 128.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
diff --git a/lib/sha512.h b/lib/sha512.h
index 121e6c3..4460e6c 100644
--- a/lib/sha512.h
+++ b/lib/sha512.h
@@ -44,8 +44,8 @@ struct sha512_ctx
   u64 state[8];
 
   u64 total[2];
-  size_t buflen;
-  u64 buffer[32];
+  size_t buflen;  /* ≥ 0, ≤ 256 */
+  u64 buffer[32]; /* 256 bytes; the first buflen bytes are in use */
 };
 
 /* Initialize structure containing state of computation. */
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index d5fa34d..ed175bf 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -130,8 +130,8 @@
 /* Get getopt(), optarg, optind, opterr, optopt.
    But avoid namespace pollution on glibc systems.  */
 #if @GNULIB_UNISTD_H_GETOPT@ && !defined __GLIBC__ && !defined 
_GL_SYSTEM_GETOPT
-# define __need_getopt
-# include <getopt.h>
+# include <getopt_cdefs.h>
+# include <getopt_pfx_core.h>
 #endif
 
 #ifndef _GL_INLINE_HEADER_BEGIN
diff --git a/m4/getopt.m4 b/m4/getopt.m4
index 7a94626..d900769 100644
--- a/m4/getopt.m4
+++ b/m4/getopt.m4
@@ -1,4 +1,4 @@
-# getopt.m4 serial 44
+# getopt.m4 serial 45
 dnl Copyright (C) 2002-2006, 2008-2017 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -33,8 +33,6 @@ AC_DEFUN([gl_FUNC_GETOPT_POSIX],
 AC_DEFUN([gl_FUNC_GETOPT_GNU],
 [
   m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
-
-  AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
 ])
 
 # Determine whether to replace the entire getopt facility.
@@ -354,15 +352,19 @@ dnl is ambiguous with environment values that contain 
newlines.
 
 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
 [
-  GETOPT_H=getopt.h
+  AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
+  if test $ac_cv_header_sys_cdefs_h = yes; then
+    HAVE_SYS_CDEFS_H=1
+  else
+    HAVE_SYS_CDEFS_H=0
+  fi
+  AC_SUBST([HAVE_SYS_CDEFS_H])
+
   AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
     [Define to rpl_ if the getopt replacement functions and variables
      should be used.])
+  GETOPT_H=getopt.h
+  GETOPT_CDEFS_H=getopt_cdefs.h
   AC_SUBST([GETOPT_H])
-])
-
-# Prerequisites of lib/getopt*.
-AC_DEFUN([gl_PREREQ_GETOPT],
-[
-  AC_CHECK_DECLS_ONCE([getenv])
+  AC_SUBST([GETOPT_CDEFS_H])
 ])
diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4
index e613490..5aea057 100644
--- a/m4/gnulib-comp.m4
+++ b/m4/gnulib-comp.m4
@@ -259,19 +259,10 @@ AC_DEFUN([gl_INIT],
   fi
   gl_STDLIB_MODULE_INDICATOR([getloadavg])
   gl_FUNC_GETOPT_GNU
-  if test $REPLACE_GETOPT = 1; then
-    AC_LIBOBJ([getopt])
-    AC_LIBOBJ([getopt1])
-    gl_PREREQ_GETOPT
-    dnl Arrange for unistd.h to include getopt.h.
-    GNULIB_GL_UNISTD_H_GETOPT=1
-  fi
-  AC_SUBST([GNULIB_GL_UNISTD_H_GETOPT])
   gl_FUNC_GETOPT_POSIX
   if test $REPLACE_GETOPT = 1; then
     AC_LIBOBJ([getopt])
     AC_LIBOBJ([getopt1])
-    gl_PREREQ_GETOPT
     dnl Arrange for unistd.h to include getopt.h.
     GNULIB_GL_UNISTD_H_GETOPT=1
   fi
@@ -911,7 +902,12 @@ AC_DEFUN([gl_FILE_LIST], [
   lib/getopt.c
   lib/getopt.in.h
   lib/getopt1.c
+  lib/getopt_cdefs.in.h
+  lib/getopt_core.h
+  lib/getopt_ext.h
   lib/getopt_int.h
+  lib/getopt_pfx_core.h
+  lib/getopt_pfx_ext.h
   lib/gettext.h
   lib/gettime.c
   lib/gettimeofday.c



reply via email to

[Prev in Thread] Current Thread [Next in Thread]