aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2017-04-06 16:29:36 -0700
committerPaul Eggert2017-04-06 16:30:09 -0700
commit022755953fc63ef70b72576e14e94324d055ef60 (patch)
tree47ea3e6fb2396a41f413badaf203d698543d68c3
parent451a0c8b6955006f5aaa8b660a9e0b935b4fa095 (diff)
downloademacs-022755953fc63ef70b72576e14e94324d055ef60.tar.gz
emacs-022755953fc63ef70b72576e14e94324d055ef60.zip
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.
-rwxr-xr-xbuild-aux/config.sub6
-rw-r--r--doc/misc/texinfo.tex11
-rw-r--r--lib/getopt.c1445
-rw-r--r--lib/getopt.in.h275
-rw-r--r--lib/getopt1.c157
-rw-r--r--lib/getopt_cdefs.in.h75
-rw-r--r--lib/getopt_core.h96
-rw-r--r--lib/getopt_ext.h77
-rw-r--r--lib/getopt_int.h69
-rw-r--r--lib/getopt_pfx_core.h54
-rw-r--r--lib/getopt_pfx_ext.h64
-rw-r--r--lib/gnulib.mk.in21
-rw-r--r--lib/md5.c5
-rw-r--r--lib/md5.h4
-rw-r--r--lib/sha1.c5
-rw-r--r--lib/sha1.h4
-rw-r--r--lib/sha256.c5
-rw-r--r--lib/sha256.h4
-rw-r--r--lib/sha512.c5
-rw-r--r--lib/sha512.h4
-rw-r--r--lib/unistd.in.h4
-rw-r--r--m4/getopt.m422
-rw-r--r--m4/gnulib-comp.m414
23 files changed, 1049 insertions, 1377 deletions
diff --git a/build-aux/config.sub b/build-aux/config.sub
index 7203bf1e64b..40ea5dfe115 100755
--- a/build-aux/config.sub
+++ b/build-aux/config.sub
@@ -2,7 +2,7 @@
2# Configuration validation subroutine script. 2# Configuration validation subroutine script.
3# Copyright 1992-2017 Free Software Foundation, Inc. 3# Copyright 1992-2017 Free Software Foundation, Inc.
4 4
5timestamp='2017-03-21' 5timestamp='2017-04-02'
6 6
7# This file is free software; you can redistribute it and/or modify it 7# This file is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by 8# under the terms of the GNU General Public License as published by
@@ -263,7 +263,7 @@ case $basic_machine in
263 | fido | fr30 | frv | ft32 \ 263 | fido | fr30 | frv | ft32 \
264 | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \ 264 | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \
265 | hexagon \ 265 | hexagon \
266 | i370 | i860 | i960 | ia64 \ 266 | i370 | i860 | i960 | ia16 | ia64 \
267 | ip2k | iq2000 \ 267 | ip2k | iq2000 \
268 | k1om \ 268 | k1om \
269 | le32 | le64 \ 269 | le32 | le64 \
@@ -389,7 +389,7 @@ case $basic_machine in
389 | h8300-* | h8500-* \ 389 | h8300-* | h8500-* \
390 | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \ 390 | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \
391 | hexagon-* \ 391 | hexagon-* \
392 | i*86-* | i860-* | i960-* | ia64-* \ 392 | i*86-* | i860-* | i960-* | ia16-* | ia64-* \
393 | ip2k-* | iq2000-* \ 393 | ip2k-* | iq2000-* \
394 | k1om-* \ 394 | k1om-* \
395 | le32-* | le64-* \ 395 | le32-* | le64-* \
diff --git a/doc/misc/texinfo.tex b/doc/misc/texinfo.tex
index 970d8784688..7bcb27b76e1 100644
--- a/doc/misc/texinfo.tex
+++ b/doc/misc/texinfo.tex
@@ -3,11 +3,11 @@
3% Load plain if necessary, i.e., if running under initex. 3% Load plain if necessary, i.e., if running under initex.
4\expandafter\ifx\csname fmtname\endcsname\relax\input plain\fi 4\expandafter\ifx\csname fmtname\endcsname\relax\input plain\fi
5% 5%
6\def\texinfoversion{2017-03-07.20} 6\def\texinfoversion{2017-03-25.14}
7% 7%
8% Copyright 1985, 1986, 1988, 1990, 1991, 1992, 1993, 1994, 1995, 8% Copyright 1985, 1986, 1988, 1990, 1991, 1992, 1993, 1994, 1995,
9% 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 9% 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
10% 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 10% 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
11% Free Software Foundation, Inc. 11% Free Software Foundation, Inc.
12% 12%
13% This texinfo.tex file is free software: you can redistribute it and/or 13% This texinfo.tex file is free software: you can redistribute it and/or
@@ -11312,7 +11312,6 @@ directory should work if nowhere else does.}
11312 % 11312 %
11313 \lispnarrowing = 0.3in 11313 \lispnarrowing = 0.3in
11314 \tolerance = 700 11314 \tolerance = 700
11315 \hfuzz = 1pt
11316 \contentsrightmargin = 0pt 11315 \contentsrightmargin = 0pt
11317 \defbodyindent = .5cm 11316 \defbodyindent = .5cm
11318}} 11317}}
@@ -11330,7 +11329,6 @@ directory should work if nowhere else does.}
11330 % 11329 %
11331 \lispnarrowing = 0.25in 11330 \lispnarrowing = 0.25in
11332 \tolerance = 700 11331 \tolerance = 700
11333 \hfuzz = 1pt
11334 \contentsrightmargin = 0pt 11332 \contentsrightmargin = 0pt
11335 \defbodyindent = .4cm 11333 \defbodyindent = .4cm
11336}} 11334}}
@@ -11356,7 +11354,6 @@ directory should work if nowhere else does.}
11356 {297mm}{210mm}% 11354 {297mm}{210mm}%
11357 % 11355 %
11358 \tolerance = 700 11356 \tolerance = 700
11359 \hfuzz = 1pt
11360 \contentsrightmargin = 0pt 11357 \contentsrightmargin = 0pt
11361 \defbodyindent = 5mm 11358 \defbodyindent = 5mm
11362}} 11359}}
@@ -11375,7 +11372,6 @@ directory should work if nowhere else does.}
11375 % 11372 %
11376 \lispnarrowing = 0.2in 11373 \lispnarrowing = 0.2in
11377 \tolerance = 800 11374 \tolerance = 800
11378 \hfuzz = 1.2pt
11379 \contentsrightmargin = 0pt 11375 \contentsrightmargin = 0pt
11380 \defbodyindent = 2mm 11376 \defbodyindent = 2mm
11381 \tableindent = 12mm 11377 \tableindent = 12mm
@@ -11431,6 +11427,9 @@ directory should work if nowhere else does.}
11431% 11427%
11432\letterpaper 11428\letterpaper
11433 11429
11430% Default value of \hfuzz, for suppressing warnings about overfull hboxes.
11431\hfuzz = 1pt
11432
11434 11433
11435\message{and turning on texinfo input format.} 11434\message{and turning on texinfo input format.}
11436 11435
diff --git a/lib/getopt.c b/lib/getopt.c
index 7afd0dcabe8..a7db39b68df 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -1,9 +1,7 @@
1/* Getopt for GNU. 1/* Getopt for GNU.
2 NOTE: getopt is part of the C library, so if you don't know what
3 "Keep this file name-space clean" means, talk to drepper@gnu.org
4 before changing it!
5 Copyright (C) 1987-2017 Free Software Foundation, Inc. 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
6 This file is part of the GNU C Library. 3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
7 5
8 The GNU C Library is free software; you can redistribute it and/or 6 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public 7 modify it under the terms of the GNU General Public
@@ -27,34 +25,57 @@
27 25
28#include <stdio.h> 26#include <stdio.h>
29#include <stdlib.h> 27#include <stdlib.h>
30#include <unistd.h>
31#include <string.h> 28#include <string.h>
29#include <unistd.h>
32 30
33#ifdef _LIBC 31#ifdef _LIBC
32/* When used as part of glibc, error printing must be done differently
33 for standards compliance. getopt is not a cancellation point, so
34 it must not call functions that are, and it is specified by an
35 older standard than stdio locking, so it must not refer to
36 functions in the "user namespace" related to stdio locking.
37 Finally, it must use glibc's internal message translation so that
38 the messages are looked up in the proper text domain. */
34# include <libintl.h> 39# include <libintl.h>
40# define fprintf __fxprintf_nocancel
41# define flockfile(fp) _IO_flockfile (fp)
42# define funlockfile(fp) _IO_funlockfile (fp)
35#else 43#else
36# include "gettext.h" 44# include "gettext.h"
37# define _(msgid) gettext (msgid) 45# define _(msgid) gettext (msgid)
46/* When used standalone, flockfile and funlockfile might not be
47 available. */
48# ifndef _POSIX_THREAD_SAFE_FUNCTIONS
49# define flockfile(fp) /* nop */
50# define funlockfile(fp) /* nop */
51# endif
52/* When used standalone, do not attempt to use alloca. */
53# define __libc_use_alloca(size) 0
54# undef alloca
55# define alloca(size) (abort (), (void *)0)
38#endif 56#endif
39 57
40#if defined _LIBC 58/* This implementation of 'getopt' has three modes for handling
41# include <wchar.h> 59 options interspersed with non-option arguments. It can stop
42#endif 60 scanning for options at the first non-option argument encountered,
43 61 as POSIX specifies. It can continue scanning for options after the
44/* This version of 'getopt' appears to the caller like standard Unix 'getopt' 62 first non-option argument, but permute 'argv' as it goes so that,
45 but it behaves differently for the user, since it allows the user 63 after 'getopt' is done, all the options precede all the non-option
46 to intersperse the options with the other arguments. 64 arguments and 'optind' points to the first non-option argument.
47 65 Or, it can report non-option arguments as if they were arguments to
48 As 'getopt_long' works, it permutes the elements of ARGV so that, 66 the option character '\x01'.
49 when it is done, all the options precede everything else. Thus 67
50 all application programs are extended to handle flexible argument order. 68 The default behavior of 'getopt_long' is to permute the argument list.
51 69 When this implementation is used standalone, the default behavior of
52 Using 'getopt' or setting the environment variable POSIXLY_CORRECT 70 'getopt' is to stop at the first non-option argument, but when it is
71 used as part of GNU libc it also permutes the argument list. In both
72 cases, setting the environment variable POSIXLY_CORRECT to any value
53 disables permutation. 73 disables permutation.
54 Then the behavior is completely standard.
55 74
56 GNU application programs can use a third alternative mode in which 75 If the first character of the OPTSTRING argument to 'getopt' or
57 they can distinguish the relative order of options and other arguments. */ 76 'getopt_long' is '+', both functions will stop at the first
77 non-option argument. If it is '-', both functions will report
78 non-option arguments as arguments to the option character '\x01'. */
58 79
59#include "getopt_int.h" 80#include "getopt_int.h"
60 81
@@ -95,42 +116,7 @@ int optopt = '?';
95/* Keep a global copy of all internal members of getopt_data. */ 116/* Keep a global copy of all internal members of getopt_data. */
96 117
97static struct _getopt_data getopt_data; 118static struct _getopt_data getopt_data;
98
99
100#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
101extern char *getenv ();
102#endif
103 119
104#ifdef _LIBC
105/* Stored original parameters.
106 XXX This is no good solution. We should rather copy the args so
107 that we can compare them later. But we must not use malloc(3). */
108extern int __libc_argc;
109extern char **__libc_argv;
110
111/* Bash 2.0 gives us an environment variable containing flags
112 indicating ARGV elements that should not be considered arguments. */
113
114# ifdef USE_NONOPTION_FLAGS
115/* Defined in getopt_init.c */
116extern char *__getopt_nonoption_flags;
117# endif
118
119# ifdef USE_NONOPTION_FLAGS
120# define SWAP_FLAGS(ch1, ch2) \
121 if (d->__nonoption_flags_len > 0) \
122 { \
123 char __tmp = __getopt_nonoption_flags[ch1]; \
124 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
125 __getopt_nonoption_flags[ch2] = __tmp; \
126 }
127# else
128# define SWAP_FLAGS(ch1, ch2)
129# endif
130#else /* !_LIBC */
131# define SWAP_FLAGS(ch1, ch2)
132#endif /* _LIBC */
133
134/* Exchange two adjacent subsequences of ARGV. 120/* Exchange two adjacent subsequences of ARGV.
135 One subsequence is elements [first_nonopt,last_nonopt) 121 One subsequence is elements [first_nonopt,last_nonopt)
136 which contains all the non-options that have been skipped so far. 122 which contains all the non-options that have been skipped so far.
@@ -153,64 +139,40 @@ exchange (char **argv, struct _getopt_data *d)
153 It leaves the longer segment in the right place overall, 139 It leaves the longer segment in the right place overall,
154 but it consists of two parts that need to be swapped next. */ 140 but it consists of two parts that need to be swapped next. */
155 141
156#if defined _LIBC && defined USE_NONOPTION_FLAGS
157 /* First make sure the handling of the '__getopt_nonoption_flags'
158 string can work normally. Our top argument must be in the range
159 of the string. */
160 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
161 {
162 /* We must extend the array. The user plays games with us and
163 presents new arguments. */
164 char *new_str = malloc (top + 1);
165 if (new_str == NULL)
166 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
167 else
168 {
169 memset (__mempcpy (new_str, __getopt_nonoption_flags,
170 d->__nonoption_flags_max_len),
171 '\0', top + 1 - d->__nonoption_flags_max_len);
172 d->__nonoption_flags_max_len = top + 1;
173 __getopt_nonoption_flags = new_str;
174 }
175 }
176#endif
177
178 while (top > middle && middle > bottom) 142 while (top > middle && middle > bottom)
179 { 143 {
180 if (top - middle > middle - bottom) 144 if (top - middle > middle - bottom)
181 { 145 {
182 /* Bottom segment is the short one. */ 146 /* Bottom segment is the short one. */
183 int len = middle - bottom; 147 int len = middle - bottom;
184 int i; 148 int i;
185 149
186 /* Swap it with the top part of the top segment. */ 150 /* Swap it with the top part of the top segment. */
187 for (i = 0; i < len; i++) 151 for (i = 0; i < len; i++)
188 { 152 {
189 tem = argv[bottom + i]; 153 tem = argv[bottom + i];
190 argv[bottom + i] = argv[top - (middle - bottom) + i]; 154 argv[bottom + i] = argv[top - (middle - bottom) + i];
191 argv[top - (middle - bottom) + i] = tem; 155 argv[top - (middle - bottom) + i] = tem;
192 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); 156 }
193 } 157 /* Exclude the moved bottom segment from further swapping. */
194 /* Exclude the moved bottom segment from further swapping. */ 158 top -= len;
195 top -= len; 159 }
196 }
197 else 160 else
198 { 161 {
199 /* Top segment is the short one. */ 162 /* Top segment is the short one. */
200 int len = top - middle; 163 int len = top - middle;
201 int i; 164 int i;
202 165
203 /* Swap it with the bottom part of the bottom segment. */ 166 /* Swap it with the bottom part of the bottom segment. */
204 for (i = 0; i < len; i++) 167 for (i = 0; i < len; i++)
205 { 168 {
206 tem = argv[bottom + i]; 169 tem = argv[bottom + i];
207 argv[bottom + i] = argv[middle + i]; 170 argv[bottom + i] = argv[middle + i];
208 argv[middle + i] = tem; 171 argv[middle + i] = tem;
209 SWAP_FLAGS (bottom + i, middle + i); 172 }
210 } 173 /* Exclude the moved top segment from further swapping. */
211 /* Exclude the moved top segment from further swapping. */ 174 bottom += len;
212 bottom += len; 175 }
213 }
214 } 176 }
215 177
216 /* Update records for the slots the non-options now occupy. */ 178 /* Update records for the slots the non-options now occupy. */
@@ -219,25 +181,216 @@ exchange (char **argv, struct _getopt_data *d)
219 d->__last_nonopt = d->optind; 181 d->__last_nonopt = d->optind;
220} 182}
221 183
222/* Initialize the internal data when the first call is made. */ 184/* Process the argument starting with d->__nextchar as a long option.
185 d->optind should *not* have been advanced over this argument.
186
187 If the value returned is -1, it was not actually a long option, the
188 state is unchanged, and the argument should be processed as a set
189 of short options (this can only happen when long_only is true).
190 Otherwise, the option (and its argument, if any) have been consumed
191 and the return value is the value to return from _getopt_internal_r. */
192static int
193process_long_option (int argc, char **argv, const char *optstring,
194 const struct option *longopts, int *longind,
195 int long_only, struct _getopt_data *d,
196 int print_errors, const char *prefix)
197{
198 char *nameend;
199 size_t namelen;
200 const struct option *p;
201 const struct option *pfound = NULL;
202 int n_options;
203 int option_index;
204
205 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
206 /* Do nothing. */ ;
207 namelen = nameend - d->__nextchar;
208
209 /* First look for an exact match, counting the options as a side
210 effect. */
211 for (p = longopts, n_options = 0; p->name; p++, n_options++)
212 if (!strncmp (p->name, d->__nextchar, namelen)
213 && namelen == strlen (p->name))
214 {
215 /* Exact match found. */
216 pfound = p;
217 option_index = n_options;
218 break;
219 }
220
221 if (pfound == NULL)
222 {
223 /* Didn't find an exact match, so look for abbreviations. */
224 unsigned char *ambig_set = NULL;
225 int ambig_malloced = 0;
226 int ambig_fallback = 0;
227 int indfound = -1;
228
229 for (p = longopts, option_index = 0; p->name; p++, option_index++)
230 if (!strncmp (p->name, d->__nextchar, namelen))
231 {
232 if (pfound == NULL)
233 {
234 /* First nonexact match found. */
235 pfound = p;
236 indfound = option_index;
237 }
238 else if (long_only
239 || pfound->has_arg != p->has_arg
240 || pfound->flag != p->flag
241 || pfound->val != p->val)
242 {
243 /* Second or later nonexact match found. */
244 if (!ambig_fallback)
245 {
246 if (!print_errors)
247 /* Don't waste effort tracking the ambig set if
248 we're not going to print it anyway. */
249 ambig_fallback = 1;
250 else if (!ambig_set)
251 {
252 if (__libc_use_alloca (n_options))
253 ambig_set = alloca (n_options);
254 else if ((ambig_set = malloc (n_options)) == NULL)
255 /* Fall back to simpler error message. */
256 ambig_fallback = 1;
257 else
258 ambig_malloced = 1;
259
260 if (ambig_set)
261 {
262 memset (ambig_set, 0, n_options);
263 ambig_set[indfound] = 1;
264 }
265 }
266 if (ambig_set)
267 ambig_set[option_index] = 1;
268 }
269 }
270 }
271
272 if (ambig_set || ambig_fallback)
273 {
274 if (print_errors)
275 {
276 if (ambig_fallback)
277 fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
278 argv[0], prefix, d->__nextchar);
279 else
280 {
281 flockfile (stderr);
282 fprintf (stderr,
283 _("%s: option '%s%s' is ambiguous; possibilities:"),
284 argv[0], prefix, d->__nextchar);
285
286 for (option_index = 0; option_index < n_options; option_index++)
287 if (ambig_set[option_index])
288 fprintf (stderr, " '%s%s'",
289 prefix, longopts[option_index].name);
290
291 /* This must use 'fprintf' even though it's only
292 printing a single character, so that it goes through
293 __fxprintf_nocancel when compiled as part of glibc. */
294 fprintf (stderr, "\n");
295 funlockfile (stderr);
296 }
297 }
298 if (ambig_malloced)
299 free (ambig_set);
300 d->__nextchar += strlen (d->__nextchar);
301 d->optind++;
302 d->optopt = 0;
303 return '?';
304 }
305
306 option_index = indfound;
307 }
308
309 if (pfound == NULL)
310 {
311 /* Can't find it as a long option. If this is not getopt_long_only,
312 or the option starts with '--' or is not a valid short option,
313 then it's an error. */
314 if (!long_only || argv[d->optind][1] == '-'
315 || strchr (optstring, *d->__nextchar) == NULL)
316 {
317 if (print_errors)
318 fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
319 argv[0], prefix, d->__nextchar);
320
321 d->__nextchar = NULL;
322 d->optind++;
323 d->optopt = 0;
324 return '?';
325 }
326
327 /* Otherwise interpret it as a short option. */
328 return -1;
329 }
330
331 /* We have found a matching long option. Consume it. */
332 d->optind++;
333 d->__nextchar = NULL;
334 if (*nameend)
335 {
336 /* Don't test has_arg with >, because some C compilers don't
337 allow it to be used on enums. */
338 if (pfound->has_arg)
339 d->optarg = nameend + 1;
340 else
341 {
342 if (print_errors)
343 fprintf (stderr,
344 _("%s: option '%s%s' doesn't allow an argument\n"),
345 argv[0], prefix, pfound->name);
346
347 d->optopt = pfound->val;
348 return '?';
349 }
350 }
351 else if (pfound->has_arg == 1)
352 {
353 if (d->optind < argc)
354 d->optarg = argv[d->optind++];
355 else
356 {
357 if (print_errors)
358 fprintf (stderr,
359 _("%s: option '%s%s' requires an argument\n"),
360 argv[0], prefix, pfound->name);
361
362 d->optopt = pfound->val;
363 return optstring[0] == ':' ? ':' : '?';
364 }
365 }
366
367 if (longind != NULL)
368 *longind = option_index;
369 if (pfound->flag)
370 {
371 *(pfound->flag) = pfound->val;
372 return 0;
373 }
374 return pfound->val;
375}
376
377/* Initialize internal data upon the first call to getopt. */
223 378
224static const char * 379static const char *
225_getopt_initialize (int argc _GL_UNUSED, 380_getopt_initialize (int argc _GL_UNUSED,
226 char **argv _GL_UNUSED, const char *optstring, 381 char **argv _GL_UNUSED, const char *optstring,
227 struct _getopt_data *d, int posixly_correct) 382 struct _getopt_data *d, int posixly_correct)
228{ 383{
229 /* Start processing options with ARGV-element 1 (since ARGV-element 0 384 /* Start processing options with ARGV-element 1 (since ARGV-element 0
230 is the program name); the sequence of previously skipped 385 is the program name); the sequence of previously skipped
231 non-option ARGV-elements is empty. */ 386 non-option ARGV-elements is empty. */
387 if (d->optind == 0)
388 d->optind = 1;
232 389
233 d->__first_nonopt = d->__last_nonopt = d->optind; 390 d->__first_nonopt = d->__last_nonopt = d->optind;
234
235 d->__nextchar = NULL; 391 d->__nextchar = NULL;
236 392
237 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
238
239 /* Determine how to handle the ordering of options and nonoptions. */ 393 /* Determine how to handle the ordering of options and nonoptions. */
240
241 if (optstring[0] == '-') 394 if (optstring[0] == '-')
242 { 395 {
243 d->__ordering = RETURN_IN_ORDER; 396 d->__ordering = RETURN_IN_ORDER;
@@ -248,41 +401,12 @@ _getopt_initialize (int argc _GL_UNUSED,
248 d->__ordering = REQUIRE_ORDER; 401 d->__ordering = REQUIRE_ORDER;
249 ++optstring; 402 ++optstring;
250 } 403 }
251 else if (d->__posixly_correct) 404 else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
252 d->__ordering = REQUIRE_ORDER; 405 d->__ordering = REQUIRE_ORDER;
253 else 406 else
254 d->__ordering = PERMUTE; 407 d->__ordering = PERMUTE;
255 408
256#if defined _LIBC && defined USE_NONOPTION_FLAGS 409 d->__initialized = 1;
257 if (!d->__posixly_correct
258 && argc == __libc_argc && argv == __libc_argv)
259 {
260 if (d->__nonoption_flags_max_len == 0)
261 {
262 if (__getopt_nonoption_flags == NULL
263 || __getopt_nonoption_flags[0] == '\0')
264 d->__nonoption_flags_max_len = -1;
265 else
266 {
267 const char *orig_str = __getopt_nonoption_flags;
268 int len = d->__nonoption_flags_max_len = strlen (orig_str);
269 if (d->__nonoption_flags_max_len < argc)
270 d->__nonoption_flags_max_len = argc;
271 __getopt_nonoption_flags =
272 (char *) malloc (d->__nonoption_flags_max_len);
273 if (__getopt_nonoption_flags == NULL)
274 d->__nonoption_flags_max_len = -1;
275 else
276 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
277 '\0', d->__nonoption_flags_max_len - len);
278 }
279 }
280 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
281 }
282 else
283 d->__nonoption_flags_len = 0;
284#endif
285
286 return optstring; 410 return optstring;
287} 411}
288 412
@@ -344,8 +468,8 @@ _getopt_initialize (int argc _GL_UNUSED,
344 468
345int 469int
346_getopt_internal_r (int argc, char **argv, const char *optstring, 470_getopt_internal_r (int argc, char **argv, const char *optstring,
347 const struct option *longopts, int *longind, 471 const struct option *longopts, int *longind,
348 int long_only, struct _getopt_data *d, int posixly_correct) 472 int long_only, struct _getopt_data *d, int posixly_correct)
349{ 473{
350 int print_errors = d->opterr; 474 int print_errors = d->opterr;
351 475
@@ -355,461 +479,129 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
355 d->optarg = NULL; 479 d->optarg = NULL;
356 480
357 if (d->optind == 0 || !d->__initialized) 481 if (d->optind == 0 || !d->__initialized)
358 { 482 optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
359 if (d->optind == 0)
360 d->optind = 1; /* Don't scan ARGV[0], the program name. */
361 optstring = _getopt_initialize (argc, argv, optstring, d,
362 posixly_correct);
363 d->__initialized = 1;
364 }
365 else if (optstring[0] == '-' || optstring[0] == '+') 483 else if (optstring[0] == '-' || optstring[0] == '+')
366 optstring++; 484 optstring++;
485
367 if (optstring[0] == ':') 486 if (optstring[0] == ':')
368 print_errors = 0; 487 print_errors = 0;
369 488
370 /* Test whether ARGV[optind] points to a non-option argument. 489 /* Test whether ARGV[optind] points to a non-option argument. */
371 Either it does not have option syntax, or there is an environment flag 490#define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
372 from the shell indicating it is not an option. The later information
373 is only used when the used in the GNU libc. */
374#if defined _LIBC && defined USE_NONOPTION_FLAGS
375# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
376 || (d->optind < d->__nonoption_flags_len \
377 && __getopt_nonoption_flags[d->optind] == '1'))
378#else
379# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
380#endif
381 491
382 if (d->__nextchar == NULL || *d->__nextchar == '\0') 492 if (d->__nextchar == NULL || *d->__nextchar == '\0')
383 { 493 {
384 /* Advance to the next ARGV-element. */ 494 /* Advance to the next ARGV-element. */
385 495
386 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 496 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
387 moved back by the user (who may also have changed the arguments). */ 497 moved back by the user (who may also have changed the arguments). */
388 if (d->__last_nonopt > d->optind) 498 if (d->__last_nonopt > d->optind)
389 d->__last_nonopt = d->optind; 499 d->__last_nonopt = d->optind;
390 if (d->__first_nonopt > d->optind) 500 if (d->__first_nonopt > d->optind)
391 d->__first_nonopt = d->optind; 501 d->__first_nonopt = d->optind;
392 502
393 if (d->__ordering == PERMUTE) 503 if (d->__ordering == PERMUTE)
394 { 504 {
395 /* If we have just processed some options following some non-options, 505 /* If we have just processed some options following some non-options,
396 exchange them so that the options come first. */ 506 exchange them so that the options come first. */
397 507
398 if (d->__first_nonopt != d->__last_nonopt 508 if (d->__first_nonopt != d->__last_nonopt
399 && d->__last_nonopt != d->optind) 509 && d->__last_nonopt != d->optind)
400 exchange ((char **) argv, d); 510 exchange (argv, d);
401 else if (d->__last_nonopt != d->optind) 511 else if (d->__last_nonopt != d->optind)
402 d->__first_nonopt = d->optind; 512 d->__first_nonopt = d->optind;
403 513
404 /* Skip any additional non-options 514 /* Skip any additional non-options
405 and extend the range of non-options previously skipped. */ 515 and extend the range of non-options previously skipped. */
406 516
407 while (d->optind < argc && NONOPTION_P) 517 while (d->optind < argc && NONOPTION_P)
408 d->optind++; 518 d->optind++;
409 d->__last_nonopt = d->optind; 519 d->__last_nonopt = d->optind;
410 } 520 }
411 521
412 /* The special ARGV-element '--' means premature end of options. 522 /* The special ARGV-element '--' means premature end of options.
413 Skip it like a null option, 523 Skip it like a null option,
414 then exchange with previous non-options as if it were an option, 524 then exchange with previous non-options as if it were an option,
415 then skip everything else like a non-option. */ 525 then skip everything else like a non-option. */
416 526
417 if (d->optind != argc && !strcmp (argv[d->optind], "--")) 527 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
418 { 528 {
419 d->optind++; 529 d->optind++;
420 530
421 if (d->__first_nonopt != d->__last_nonopt 531 if (d->__first_nonopt != d->__last_nonopt
422 && d->__last_nonopt != d->optind) 532 && d->__last_nonopt != d->optind)
423 exchange ((char **) argv, d); 533 exchange (argv, d);
424 else if (d->__first_nonopt == d->__last_nonopt) 534 else if (d->__first_nonopt == d->__last_nonopt)
425 d->__first_nonopt = d->optind; 535 d->__first_nonopt = d->optind;
426 d->__last_nonopt = argc; 536 d->__last_nonopt = argc;
427 537
428 d->optind = argc; 538 d->optind = argc;
429 } 539 }
430 540
431 /* If we have done all the ARGV-elements, stop the scan 541 /* If we have done all the ARGV-elements, stop the scan
432 and back over any non-options that we skipped and permuted. */ 542 and back over any non-options that we skipped and permuted. */
433 543
434 if (d->optind == argc) 544 if (d->optind == argc)
435 { 545 {
436 /* Set the next-arg-index to point at the non-options 546 /* Set the next-arg-index to point at the non-options
437 that we previously skipped, so the caller will digest them. */ 547 that we previously skipped, so the caller will digest them. */
438 if (d->__first_nonopt != d->__last_nonopt) 548 if (d->__first_nonopt != d->__last_nonopt)
439 d->optind = d->__first_nonopt; 549 d->optind = d->__first_nonopt;
440 return -1; 550 return -1;
441 } 551 }
442 552
443 /* If we have come to a non-option and did not permute it, 553 /* If we have come to a non-option and did not permute it,
444 either stop the scan or describe it to the caller and pass it by. */ 554 either stop the scan or describe it to the caller and pass it by. */
445 555
446 if (NONOPTION_P) 556 if (NONOPTION_P)
447 { 557 {
448 if (d->__ordering == REQUIRE_ORDER) 558 if (d->__ordering == REQUIRE_ORDER)
449 return -1; 559 return -1;
450 d->optarg = argv[d->optind++]; 560 d->optarg = argv[d->optind++];
451 return 1; 561 return 1;
452 } 562 }
453 563
454 /* We have found another option-ARGV-element. 564 /* We have found another option-ARGV-element.
455 Skip the initial punctuation. */ 565 Check whether it might be a long option. */
456 566 if (longopts)
457 d->__nextchar = (argv[d->optind] + 1 567 {
458 + (longopts != NULL && argv[d->optind][1] == '-')); 568 if (argv[d->optind][1] == '-')
459 } 569 {
460 570 /* "--foo" is always a long option. The special option
461 /* Decode the current option-ARGV-element. */ 571 "--" was handled above. */
462 572 d->__nextchar = argv[d->optind] + 2;
463 /* Check whether the ARGV-element is a long option. 573 return process_long_option (argc, argv, optstring, longopts,
464 574 longind, long_only, d,
465 If long_only and the ARGV-element has the form "-f", where f is 575 print_errors, "--");
466 a valid short option, don't consider it an abbreviated form of 576 }
467 a long option that starts with f. Otherwise there would be no 577
468 way to give the -f short option. 578 /* If long_only and the ARGV-element has the form "-f",
469 579 where f is a valid short option, don't consider it an
470 On the other hand, if there's a long option "fubar" and 580 abbreviated form of a long option that starts with f.
471 the ARGV-element is "-fu", do consider that an abbreviation of 581 Otherwise there would be no way to give the -f short
472 the long option, just like "--fu", and not "-f" with arg "u". 582 option.
473 583
474 This distinction seems to be the most useful approach. */ 584 On the other hand, if there's a long option "fubar" and
475 585 the ARGV-element is "-fu", do consider that an
476 if (longopts != NULL 586 abbreviation of the long option, just like "--fu", and
477 && (argv[d->optind][1] == '-' 587 not "-f" with arg "u".
478 || (long_only && (argv[d->optind][2] 588
479 || !strchr (optstring, argv[d->optind][1]))))) 589 This distinction seems to be the most useful approach. */
480 { 590 if (long_only && (argv[d->optind][2]
481 char *nameend; 591 || !strchr (optstring, argv[d->optind][1])))
482 unsigned int namelen; 592 {
483 const struct option *p; 593 int code;
484 const struct option *pfound = NULL; 594 d->__nextchar = argv[d->optind] + 1;
485 struct option_list 595 code = process_long_option (argc, argv, optstring, longopts,
486 { 596 longind, long_only, d,
487 const struct option *p; 597 print_errors, "-");
488 struct option_list *next; 598 if (code != -1)
489 } *ambig_list = NULL; 599 return code;
490#ifdef _LIBC 600 }
491/* malloc() not used for _LIBC to simplify failure messages. */ 601 }
492# define free_option_list(l) 602
493#else 603 /* It is not a long option. Skip the initial punctuation. */
494# define free_option_list(l) \ 604 d->__nextchar = argv[d->optind] + 1;
495 while (l != NULL) \
496 { \
497 struct option_list *pn = l->next; \
498 free (l); \
499 l = pn; \
500 }
501#endif
502 int exact = 0;
503 int ambig = 0;
504 int indfound = -1;
505 int option_index;
506
507 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
508 /* Do nothing. */ ;
509 namelen = nameend - d->__nextchar;
510
511 /* Test all long options for either exact match
512 or abbreviated matches. */
513 for (p = longopts, option_index = 0; p->name; p++, option_index++)
514 if (!strncmp (p->name, d->__nextchar, namelen))
515 {
516 if (namelen == (unsigned int) strlen (p->name))
517 {
518 /* Exact match found. */
519 pfound = p;
520 indfound = option_index;
521 exact = 1;
522 break;
523 }
524 else if (pfound == NULL)
525 {
526 /* First nonexact match found. */
527 pfound = p;
528 indfound = option_index;
529 }
530 else if (ambig)
531 ; /* Taking simpler path to handling ambiguities. */
532 else if (long_only
533 || pfound->has_arg != p->has_arg
534 || pfound->flag != p->flag
535 || pfound->val != p->val)
536 {
537 /* Second or later nonexact match found. */
538#ifdef _LIBC
539 struct option_list *newp = alloca (sizeof (*newp));
540#else
541 struct option_list *newp = malloc (sizeof (*newp));
542 if (newp == NULL)
543 {
544 free_option_list (ambig_list);
545 ambig_list = NULL;
546 ambig = 1; /* Use simpler fallback message. */
547 }
548 else
549#endif
550 {
551 newp->p = p;
552 newp->next = ambig_list;
553 ambig_list = newp;
554 }
555 }
556 }
557
558 if ((ambig || ambig_list) && !exact)
559 {
560 if (print_errors && ambig_list)
561 {
562 struct option_list first;
563 first.p = pfound;
564 first.next = ambig_list;
565 ambig_list = &first;
566
567#if defined _LIBC
568 char *buf = NULL;
569 size_t buflen = 0;
570
571 FILE *fp = __open_memstream (&buf, &buflen);
572 if (fp != NULL)
573 {
574 fprintf (fp,
575 _("%s: option '%s' is ambiguous; possibilities:"),
576 argv[0], argv[d->optind]);
577
578 do
579 {
580 fprintf (fp, " '--%s'", ambig_list->p->name);
581 ambig_list = ambig_list->next;
582 }
583 while (ambig_list != NULL);
584
585 fputc_unlocked ('\n', fp);
586
587 if (__glibc_likely (fclose (fp) != EOF))
588 {
589 _IO_flockfile (stderr);
590
591 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
592 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
593
594 __fxprintf (NULL, "%s", buf);
595
596 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
597 _IO_funlockfile (stderr);
598
599 free (buf);
600 }
601 }
602#else
603 fprintf (stderr,
604 _("%s: option '%s' is ambiguous; possibilities:"),
605 argv[0], argv[d->optind]);
606 do
607 {
608 fprintf (stderr, " '--%s'", ambig_list->p->name);
609 ambig_list = ambig_list->next;
610 }
611 while (ambig_list != NULL);
612
613 fputc ('\n', stderr);
614#endif
615 }
616 else if (print_errors && ambig)
617 {
618 fprintf (stderr,
619 _("%s: option '%s' is ambiguous\n"),
620 argv[0], argv[d->optind]);
621 }
622 d->__nextchar += strlen (d->__nextchar);
623 d->optind++;
624 d->optopt = 0;
625 free_option_list (ambig_list);
626 return '?';
627 }
628
629 free_option_list (ambig_list);
630
631 if (pfound != NULL)
632 {
633 option_index = indfound;
634 d->optind++;
635 if (*nameend)
636 {
637 /* Don't test has_arg with >, because some C compilers don't
638 allow it to be used on enums. */
639 if (pfound->has_arg)
640 d->optarg = nameend + 1;
641 else
642 {
643 if (print_errors)
644 {
645#if defined _LIBC
646 char *buf;
647 int n;
648#endif
649
650 if (argv[d->optind - 1][1] == '-')
651 {
652 /* --option */
653#if defined _LIBC
654 n = __asprintf (&buf, _("\
655%s: option '--%s' doesn't allow an argument\n"),
656 argv[0], pfound->name);
657#else
658 fprintf (stderr, _("\
659%s: option '--%s' doesn't allow an argument\n"),
660 argv[0], pfound->name);
661#endif
662 }
663 else
664 {
665 /* +option or -option */
666#if defined _LIBC
667 n = __asprintf (&buf, _("\
668%s: option '%c%s' doesn't allow an argument\n"),
669 argv[0], argv[d->optind - 1][0],
670 pfound->name);
671#else
672 fprintf (stderr, _("\
673%s: option '%c%s' doesn't allow an argument\n"),
674 argv[0], argv[d->optind - 1][0],
675 pfound->name);
676#endif
677 }
678
679#if defined _LIBC
680 if (n >= 0)
681 {
682 _IO_flockfile (stderr);
683
684 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
685 ((_IO_FILE *) stderr)->_flags2
686 |= _IO_FLAGS2_NOTCANCEL;
687
688 __fxprintf (NULL, "%s", buf);
689
690 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
691 _IO_funlockfile (stderr);
692
693 free (buf);
694 }
695#endif
696 }
697
698 d->__nextchar += strlen (d->__nextchar);
699
700 d->optopt = pfound->val;
701 return '?';
702 }
703 }
704 else if (pfound->has_arg == 1)
705 {
706 if (d->optind < argc)
707 d->optarg = argv[d->optind++];
708 else
709 {
710 if (print_errors)
711 {
712#if defined _LIBC
713 char *buf;
714
715 if (__asprintf (&buf, _("\
716%s: option '--%s' requires an argument\n"),
717 argv[0], pfound->name) >= 0)
718 {
719 _IO_flockfile (stderr);
720
721 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
722 ((_IO_FILE *) stderr)->_flags2
723 |= _IO_FLAGS2_NOTCANCEL;
724
725 __fxprintf (NULL, "%s", buf);
726
727 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
728 _IO_funlockfile (stderr);
729
730 free (buf);
731 }
732#else
733 fprintf (stderr,
734 _("%s: option '--%s' requires an argument\n"),
735 argv[0], pfound->name);
736#endif
737 }
738 d->__nextchar += strlen (d->__nextchar);
739 d->optopt = pfound->val;
740 return optstring[0] == ':' ? ':' : '?';
741 }
742 }
743 d->__nextchar += strlen (d->__nextchar);
744 if (longind != NULL)
745 *longind = option_index;
746 if (pfound->flag)
747 {
748 *(pfound->flag) = pfound->val;
749 return 0;
750 }
751 return pfound->val;
752 }
753
754 /* Can't find it as a long option. If this is not getopt_long_only,
755 or the option starts with '--' or is not a valid short
756 option, then it's an error.
757 Otherwise interpret it as a short option. */
758 if (!long_only || argv[d->optind][1] == '-'
759 || strchr (optstring, *d->__nextchar) == NULL)
760 {
761 if (print_errors)
762 {
763#if defined _LIBC
764 char *buf;
765 int n;
766#endif
767
768 if (argv[d->optind][1] == '-')
769 {
770 /* --option */
771#if defined _LIBC
772 n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
773 argv[0], d->__nextchar);
774#else
775 fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
776 argv[0], d->__nextchar);
777#endif
778 }
779 else
780 {
781 /* +option or -option */
782#if defined _LIBC
783 n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
784 argv[0], argv[d->optind][0], d->__nextchar);
785#else
786 fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
787 argv[0], argv[d->optind][0], d->__nextchar);
788#endif
789 }
790
791#if defined _LIBC
792 if (n >= 0)
793 {
794 _IO_flockfile (stderr);
795
796 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
797 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
798
799 __fxprintf (NULL, "%s", buf);
800
801 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
802 _IO_funlockfile (stderr);
803
804 free (buf);
805 }
806#endif
807 }
808 d->__nextchar = (char *) "";
809 d->optind++;
810 d->optopt = 0;
811 return '?';
812 }
813 } 605 }
814 606
815 /* Look at and handle the next short option-character. */ 607 /* Look at and handle the next short option-character. */
@@ -824,331 +616,83 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
824 616
825 if (temp == NULL || c == ':' || c == ';') 617 if (temp == NULL || c == ':' || c == ';')
826 { 618 {
827 if (print_errors) 619 if (print_errors)
828 { 620 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
829#if defined _LIBC 621 d->optopt = c;
830 char *buf; 622 return '?';
831 int n;
832#endif
833
834#if defined _LIBC
835 n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
836 argv[0], c);
837#else
838 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
839#endif
840
841#if defined _LIBC
842 if (n >= 0)
843 {
844 _IO_flockfile (stderr);
845
846 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
847 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
848
849 __fxprintf (NULL, "%s", buf);
850
851 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
852 _IO_funlockfile (stderr);
853
854 free (buf);
855 }
856#endif
857 }
858 d->optopt = c;
859 return '?';
860 } 623 }
624
861 /* Convenience. Treat POSIX -W foo same as long option --foo */ 625 /* Convenience. Treat POSIX -W foo same as long option --foo */
862 if (temp[0] == 'W' && temp[1] == ';') 626 if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
863 { 627 {
864 char *nameend; 628 /* This is an option that requires an argument. */
865 const struct option *p; 629 if (*d->__nextchar != '\0')
866 const struct option *pfound = NULL; 630 d->optarg = d->__nextchar;
867 int exact = 0; 631 else if (d->optind == argc)
868 int ambig = 0; 632 {
869 int indfound = 0; 633 if (print_errors)
870 int option_index; 634 fprintf (stderr,
871 635 _("%s: option requires an argument -- '%c'\n"),
872 if (longopts == NULL) 636 argv[0], c);
873 goto no_longs; 637
874 638 d->optopt = c;
875 /* This is an option that requires an argument. */ 639 if (optstring[0] == ':')
876 if (*d->__nextchar != '\0') 640 c = ':';
877 { 641 else
878 d->optarg = d->__nextchar; 642 c = '?';
879 /* If we end this ARGV-element by taking the rest as an arg, 643 return c;
880 we must advance to the next element now. */ 644 }
881 d->optind++; 645 else
882 } 646 d->optarg = argv[d->optind];
883 else if (d->optind == argc) 647
884 { 648 d->__nextchar = d->optarg;
885 if (print_errors) 649 d->optarg = NULL;
886 { 650 return process_long_option (argc, argv, optstring, longopts, longind,
887#if defined _LIBC 651 0 /* long_only */, d, print_errors, "-W ");
888 char *buf;
889
890 if (__asprintf (&buf,
891 _("%s: option requires an argument -- '%c'\n"),
892 argv[0], c) >= 0)
893 {
894 _IO_flockfile (stderr);
895
896 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
897 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
898
899 __fxprintf (NULL, "%s", buf);
900
901 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
902 _IO_funlockfile (stderr);
903
904 free (buf);
905 }
906#else
907 fprintf (stderr,
908 _("%s: option requires an argument -- '%c'\n"),
909 argv[0], c);
910#endif
911 }
912 d->optopt = c;
913 if (optstring[0] == ':')
914 c = ':';
915 else
916 c = '?';
917 return c;
918 }
919 else
920 /* We already incremented 'd->optind' once;
921 increment it again when taking next ARGV-elt as argument. */
922 d->optarg = argv[d->optind++];
923
924 /* optarg is now the argument, see if it's in the
925 table of longopts. */
926
927 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
928 nameend++)
929 /* Do nothing. */ ;
930
931 /* Test all long options for either exact match
932 or abbreviated matches. */
933 for (p = longopts, option_index = 0; p->name; p++, option_index++)
934 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
935 {
936 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
937 {
938 /* Exact match found. */
939 pfound = p;
940 indfound = option_index;
941 exact = 1;
942 break;
943 }
944 else if (pfound == NULL)
945 {
946 /* First nonexact match found. */
947 pfound = p;
948 indfound = option_index;
949 }
950 else if (long_only
951 || pfound->has_arg != p->has_arg
952 || pfound->flag != p->flag
953 || pfound->val != p->val)
954 /* Second or later nonexact match found. */
955 ambig = 1;
956 }
957 if (ambig && !exact)
958 {
959 if (print_errors)
960 {
961#if defined _LIBC
962 char *buf;
963
964 if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
965 argv[0], d->optarg) >= 0)
966 {
967 _IO_flockfile (stderr);
968
969 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
970 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
971
972 __fxprintf (NULL, "%s", buf);
973
974 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
975 _IO_funlockfile (stderr);
976
977 free (buf);
978 }
979#else
980 fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
981 argv[0], d->optarg);
982#endif
983 }
984 d->__nextchar += strlen (d->__nextchar);
985 d->optind++;
986 return '?';
987 }
988 if (pfound != NULL)
989 {
990 option_index = indfound;
991 if (*nameend)
992 {
993 /* Don't test has_arg with >, because some C compilers don't
994 allow it to be used on enums. */
995 if (pfound->has_arg)
996 d->optarg = nameend + 1;
997 else
998 {
999 if (print_errors)
1000 {
1001#if defined _LIBC
1002 char *buf;
1003
1004 if (__asprintf (&buf, _("\
1005%s: option '-W %s' doesn't allow an argument\n"),
1006 argv[0], pfound->name) >= 0)
1007 {
1008 _IO_flockfile (stderr);
1009
1010 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1011 ((_IO_FILE *) stderr)->_flags2
1012 |= _IO_FLAGS2_NOTCANCEL;
1013
1014 __fxprintf (NULL, "%s", buf);
1015
1016 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1017 _IO_funlockfile (stderr);
1018
1019 free (buf);
1020 }
1021#else
1022 fprintf (stderr, _("\
1023%s: option '-W %s' doesn't allow an argument\n"),
1024 argv[0], pfound->name);
1025#endif
1026 }
1027
1028 d->__nextchar += strlen (d->__nextchar);
1029 return '?';
1030 }
1031 }
1032 else if (pfound->has_arg == 1)
1033 {
1034 if (d->optind < argc)
1035 d->optarg = argv[d->optind++];
1036 else
1037 {
1038 if (print_errors)
1039 {
1040#if defined _LIBC
1041 char *buf;
1042
1043 if (__asprintf (&buf, _("\
1044%s: option '-W %s' requires an argument\n"),
1045 argv[0], pfound->name) >= 0)
1046 {
1047 _IO_flockfile (stderr);
1048
1049 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1050 ((_IO_FILE *) stderr)->_flags2
1051 |= _IO_FLAGS2_NOTCANCEL;
1052
1053 __fxprintf (NULL, "%s", buf);
1054
1055 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1056 _IO_funlockfile (stderr);
1057
1058 free (buf);
1059 }
1060#else
1061 fprintf (stderr, _("\
1062%s: option '-W %s' requires an argument\n"),
1063 argv[0], pfound->name);
1064#endif
1065 }
1066 d->__nextchar += strlen (d->__nextchar);
1067 return optstring[0] == ':' ? ':' : '?';
1068 }
1069 }
1070 else
1071 d->optarg = NULL;
1072 d->__nextchar += strlen (d->__nextchar);
1073 if (longind != NULL)
1074 *longind = option_index;
1075 if (pfound->flag)
1076 {
1077 *(pfound->flag) = pfound->val;
1078 return 0;
1079 }
1080 return pfound->val;
1081 }
1082
1083 no_longs:
1084 d->__nextchar = NULL;
1085 return 'W'; /* Let the application handle it. */
1086 } 652 }
1087 if (temp[1] == ':') 653 if (temp[1] == ':')
1088 { 654 {
1089 if (temp[2] == ':') 655 if (temp[2] == ':')
1090 { 656 {
1091 /* This is an option that accepts an argument optionally. */ 657 /* This is an option that accepts an argument optionally. */
1092 if (*d->__nextchar != '\0') 658 if (*d->__nextchar != '\0')
1093 { 659 {
1094 d->optarg = d->__nextchar; 660 d->optarg = d->__nextchar;
1095 d->optind++; 661 d->optind++;
1096 } 662 }
1097 else 663 else
1098 d->optarg = NULL; 664 d->optarg = NULL;
1099 d->__nextchar = NULL; 665 d->__nextchar = NULL;
1100 } 666 }
1101 else 667 else
1102 { 668 {
1103 /* This is an option that requires an argument. */ 669 /* This is an option that requires an argument. */
1104 if (*d->__nextchar != '\0') 670 if (*d->__nextchar != '\0')
1105 { 671 {
1106 d->optarg = d->__nextchar; 672 d->optarg = d->__nextchar;
1107 /* If we end this ARGV-element by taking the rest as an arg, 673 /* If we end this ARGV-element by taking the rest as an arg,
1108 we must advance to the next element now. */ 674 we must advance to the next element now. */
1109 d->optind++; 675 d->optind++;
1110 } 676 }
1111 else if (d->optind == argc) 677 else if (d->optind == argc)
1112 { 678 {
1113 if (print_errors) 679 if (print_errors)
1114 { 680 fprintf (stderr,
1115#if defined _LIBC 681 _("%s: option requires an argument -- '%c'\n"),
1116 char *buf; 682 argv[0], c);
1117 683
1118 if (__asprintf (&buf, _("\ 684 d->optopt = c;
1119%s: option requires an argument -- '%c'\n"), 685 if (optstring[0] == ':')
1120 argv[0], c) >= 0) 686 c = ':';
1121 { 687 else
1122 _IO_flockfile (stderr); 688 c = '?';
1123 689 }
1124 int old_flags2 = ((_IO_FILE *) stderr)->_flags2; 690 else
1125 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; 691 /* We already incremented 'optind' once;
1126 692 increment it again when taking next ARGV-elt as argument. */
1127 __fxprintf (NULL, "%s", buf); 693 d->optarg = argv[d->optind++];
1128 694 d->__nextchar = NULL;
1129 ((_IO_FILE *) stderr)->_flags2 = old_flags2; 695 }
1130 _IO_funlockfile (stderr);
1131
1132 free (buf);
1133 }
1134#else
1135 fprintf (stderr,
1136 _("%s: option requires an argument -- '%c'\n"),
1137 argv[0], c);
1138#endif
1139 }
1140 d->optopt = c;
1141 if (optstring[0] == ':')
1142 c = ':';
1143 else
1144 c = '?';
1145 }
1146 else
1147 /* We already incremented 'optind' once;
1148 increment it again when taking next ARGV-elt as argument. */
1149 d->optarg = argv[d->optind++];
1150 d->__nextchar = NULL;
1151 }
1152 } 696 }
1153 return c; 697 return c;
1154 } 698 }
@@ -1156,8 +700,8 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
1156 700
1157int 701int
1158_getopt_internal (int argc, char **argv, const char *optstring, 702_getopt_internal (int argc, char **argv, const char *optstring,
1159 const struct option *longopts, int *longind, int long_only, 703 const struct option *longopts, int *longind, int long_only,
1160 int posixly_correct) 704 int posixly_correct)
1161{ 705{
1162 int result; 706 int result;
1163 707
@@ -1165,8 +709,8 @@ _getopt_internal (int argc, char **argv, const char *optstring,
1165 getopt_data.opterr = opterr; 709 getopt_data.opterr = opterr;
1166 710
1167 result = _getopt_internal_r (argc, argv, optstring, longopts, 711 result = _getopt_internal_r (argc, argv, optstring, longopts,
1168 longind, long_only, &getopt_data, 712 longind, long_only, &getopt_data,
1169 posixly_correct); 713 posixly_correct);
1170 714
1171 optind = getopt_data.optind; 715 optind = getopt_data.optind;
1172 optarg = getopt_data.optarg; 716 optarg = getopt_data.optarg;
@@ -1175,32 +719,23 @@ _getopt_internal (int argc, char **argv, const char *optstring,
1175 return result; 719 return result;
1176} 720}
1177 721
1178/* glibc gets a LSB-compliant getopt. 722/* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
1179 Standalone applications get a POSIX-compliant getopt. */ 723 Standalone applications just get a POSIX-compliant getopt.
1180#if _LIBC 724 POSIX and LSB both require these functions to take 'char *const *argv'
1181enum { POSIXLY_CORRECT = 0 }; 725 even though this is incorrect (because of the permutation). */
1182#else 726#define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \
1183enum { POSIXLY_CORRECT = 1 }; 727 int \
1184#endif 728 NAME (int argc, char *const *argv, const char *optstring) \
1185 729 { \
1186int 730 return _getopt_internal (argc, (char **)argv, optstring, \
1187getopt (int argc, char *const *argv, const char *optstring) 731 0, 0, 0, POSIXLY_CORRECT); \
1188{ 732 }
1189 return _getopt_internal (argc, (char **) argv, optstring,
1190 (const struct option *) 0,
1191 (int *) 0,
1192 0, POSIXLY_CORRECT);
1193}
1194 733
1195#ifdef _LIBC 734#ifdef _LIBC
1196int 735GETOPT_ENTRY(getopt, 0)
1197__posix_getopt (int argc, char *const *argv, const char *optstring) 736GETOPT_ENTRY(__posix_getopt, 1)
1198{ 737#else
1199 return _getopt_internal (argc, argv, optstring, 738GETOPT_ENTRY(getopt, 1)
1200 (const struct option *) 0,
1201 (int *) 0,
1202 0, 1);
1203}
1204#endif 739#endif
1205 740
1206 741
@@ -1221,51 +756,51 @@ main (int argc, char **argv)
1221 756
1222 c = getopt (argc, argv, "abc:d:0123456789"); 757 c = getopt (argc, argv, "abc:d:0123456789");
1223 if (c == -1) 758 if (c == -1)
1224 break; 759 break;
1225 760
1226 switch (c) 761 switch (c)
1227 { 762 {
1228 case '0': 763 case '0':
1229 case '1': 764 case '1':
1230 case '2': 765 case '2':
1231 case '3': 766 case '3':
1232 case '4': 767 case '4':
1233 case '5': 768 case '5':
1234 case '6': 769 case '6':
1235 case '7': 770 case '7':
1236 case '8': 771 case '8':
1237 case '9': 772 case '9':
1238 if (digit_optind != 0 && digit_optind != this_option_optind) 773 if (digit_optind != 0 && digit_optind != this_option_optind)
1239 printf ("digits occur in two different argv-elements.\n"); 774 printf ("digits occur in two different argv-elements.\n");
1240 digit_optind = this_option_optind; 775 digit_optind = this_option_optind;
1241 printf ("option %c\n", c); 776 printf ("option %c\n", c);
1242 break; 777 break;
1243 778
1244 case 'a': 779 case 'a':
1245 printf ("option a\n"); 780 printf ("option a\n");
1246 break; 781 break;
1247 782
1248 case 'b': 783 case 'b':
1249 printf ("option b\n"); 784 printf ("option b\n");
1250 break; 785 break;
1251 786
1252 case 'c': 787 case 'c':
1253 printf ("option c with value '%s'\n", optarg); 788 printf ("option c with value '%s'\n", optarg);
1254 break; 789 break;
1255 790
1256 case '?': 791 case '?':
1257 break; 792 break;
1258 793
1259 default: 794 default:
1260 printf ("?? getopt returned character code 0%o ??\n", c); 795 printf ("?? getopt returned character code 0%o ??\n", c);
1261 } 796 }
1262 } 797 }
1263 798
1264 if (optind < argc) 799 if (optind < argc)
1265 { 800 {
1266 printf ("non-option ARGV-elements: "); 801 printf ("non-option ARGV-elements: ");
1267 while (optind < argc) 802 while (optind < argc)
1268 printf ("%s ", argv[optind++]); 803 printf ("%s ", argv[optind++]);
1269 printf ("\n"); 804 printf ("\n");
1270 } 805 }
1271 806
diff --git a/lib/getopt.in.h b/lib/getopt.in.h
index 3f57c68a25e..91e086c0207 100644
--- a/lib/getopt.in.h
+++ b/lib/getopt.in.h
@@ -1,20 +1,22 @@
1/* Declarations for getopt. 1/* Declarations for getopt.
2 Copyright (C) 1989-2017 Free Software Foundation, Inc. 2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of gnulib.
4 4 Unlike most of the getopt implementation, it is NOT shared
5 The GNU C Library is free software; you can redistribute it and/or 5 with the GNU C Library, which supplies a different version of
6 modify it under the terms of the GNU General Public 6 this file.
7 License as published by the Free Software Foundation; either 7
8 version 3 of the License, or (at your option) any later version. 8 gnulib is free software; you can redistribute it and/or modify it
9 9 under the terms of the GNU General Public License as
10 The GNU C Library is distributed in the hope that it will be useful, 10 published by the Free Software Foundation; either version 3 of
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 the License, or (at your option) any later version.
12
13 gnulib is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 16 General Public License for more details.
14 17
15 You should have received a copy of the GNU General Public 18 You should have received a copy of the GNU General Public
16 License along with the GNU C Library; if not, see 19 License along with gnulib; if not, see <http://www.gnu.org/licenses/>. */
17 <http://www.gnu.org/licenses/>. */
18 20
19#ifndef _@GUARD_PREFIX@_GETOPT_H 21#ifndef _@GUARD_PREFIX@_GETOPT_H
20 22
@@ -32,249 +34,26 @@
32# undef _GL_SYSTEM_GETOPT 34# undef _GL_SYSTEM_GETOPT
33#endif 35#endif
34 36
35#ifndef _@GUARD_PREFIX@_GETOPT_H 37#define _@GUARD_PREFIX@_GETOPT_H 1
36
37#ifndef __need_getopt
38# define _@GUARD_PREFIX@_GETOPT_H 1
39#endif
40 38
41/* Standalone applications should #define __GETOPT_PREFIX to an 39/* Standalone applications should #define __GETOPT_PREFIX to an
42 identifier that prefixes the external functions and variables 40 identifier that prefixes the external functions and variables
43 defined in this header. When this happens, include the 41 defined in getopt_core.h and getopt_ext.h. When this happens,
44 headers that might declare getopt so that they will not cause 42 include the headers that might declare getopt so that they will not
45 confusion if included after this file (if the system had <getopt.h>, 43 cause confusion if included after this file (if the system had
46 we have already included it). Then systematically rename 44 <getopt.h>, we have already included it). */
47 identifiers so that they do not collide with the system functions
48 and variables. Renaming avoids problems with some compilers and
49 linkers. */
50#if defined __GETOPT_PREFIX 45#if defined __GETOPT_PREFIX
51# if !defined __need_getopt 46# if !@HAVE_GETOPT_H@
52# if !@HAVE_GETOPT_H@ 47# define __need_system_stdlib_h
53# define __need_system_stdlib_h 48# include <stdlib.h>
54# include <stdlib.h> 49# undef __need_system_stdlib_h
55# undef __need_system_stdlib_h 50# include <stdio.h>
56# include <stdio.h> 51# include <unistd.h>
57# include <unistd.h>
58# endif
59# undef __need_getopt
60# endif
61# undef __GETOPT_CONCAT
62# undef __GETOPT_XCONCAT
63# undef __GETOPT_ID
64# undef getopt
65# undef getopt_long
66# undef getopt_long_only
67# undef optarg
68# undef opterr
69# undef optind
70# undef optopt
71# undef option
72# undef _getopt_internal
73# define __GETOPT_CONCAT(x, y) x ## y
74# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
75# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
76# define getopt __GETOPT_ID (getopt)
77# define getopt_long __GETOPT_ID (getopt_long)
78# define getopt_long_only __GETOPT_ID (getopt_long_only)
79# define optarg __GETOPT_ID (optarg)
80# define opterr __GETOPT_ID (opterr)
81# define optind __GETOPT_ID (optind)
82# define optopt __GETOPT_ID (optopt)
83# define option __GETOPT_ID (option)
84# define _getopt_internal __GETOPT_ID (getopt_internal)
85#endif
86
87/* Standalone applications get correct prototypes for getopt_long and
88 getopt_long_only; they declare "char **argv". libc uses prototypes
89 with "char *const *argv" that are incorrect because getopt_long and
90 getopt_long_only can permute argv; this is required for backward
91 compatibility (e.g., for LSB 2.0.1).
92
93 This used to be '#if defined __GETOPT_PREFIX && !defined __need_getopt',
94 but it caused redefinition warnings if both unistd.h and getopt.h were
95 included, since unistd.h includes getopt.h having previously defined
96 __need_getopt.
97
98 The only place where __getopt_argv_const is used is in definitions
99 of getopt_long and getopt_long_only below, but these are visible
100 only if __need_getopt is not defined, so it is quite safe to rewrite
101 the conditional as follows:
102*/
103#if !defined __need_getopt
104# if defined __GETOPT_PREFIX
105# define __getopt_argv_const /* empty */
106# else
107# define __getopt_argv_const const
108# endif
109#endif
110
111/* If __GNU_LIBRARY__ is not already defined, either we are being used
112 standalone, or this is the first header included in the source file.
113 If we are being used with glibc, we need to include <features.h>, but
114 that does not exist if we are standalone. So: if __GNU_LIBRARY__ is
115 not defined, include <ctype.h>, which will pull in <features.h> for us
116 if it's from glibc. (Why ctype.h? It's guaranteed to exist and it
117 doesn't flood the namespace with stuff the way some other headers do.) */
118#if !defined __GNU_LIBRARY__
119# include <ctype.h>
120#endif
121
122#ifndef __THROW
123# ifndef __GNUC_PREREQ
124# define __GNUC_PREREQ(maj, min) (0)
125# endif
126# if defined __cplusplus && __GNUC_PREREQ (2,8)
127# define __THROW throw ()
128# else
129# define __THROW
130# endif 52# endif
131#endif 53#endif
132 54
133/* The definition of _GL_ARG_NONNULL is copied here. */ 55#include <getopt_cdefs.h>
134 56#include <getopt_pfx_core.h>
135#ifdef __cplusplus 57#include <getopt_pfx_ext.h>
136extern "C" {
137#endif
138
139/* For communication from 'getopt' to the caller.
140 When 'getopt' finds an option that takes an argument,
141 the argument value is returned here.
142 Also, when 'ordering' is RETURN_IN_ORDER,
143 each non-option ARGV-element is returned here. */
144
145extern char *optarg;
146
147/* Index in ARGV of the next element to be scanned.
148 This is used for communication to and from the caller
149 and for communication between successive calls to 'getopt'.
150
151 On entry to 'getopt', zero means this is the first call; initialize.
152
153 When 'getopt' returns -1, this is the index of the first of the
154 non-option elements that the caller should itself scan.
155
156 Otherwise, 'optind' communicates from one call to the next
157 how much of ARGV has been scanned so far. */
158
159extern int optind;
160
161/* Callers store zero here to inhibit the error message 'getopt' prints
162 for unrecognized options. */
163
164extern int opterr;
165
166/* Set to an option character which was unrecognized. */
167 58
168extern int optopt;
169
170#ifndef __need_getopt
171/* Describe the long-named options requested by the application.
172 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
173 of 'struct option' terminated by an element containing a name which is
174 zero.
175
176 The field 'has_arg' is:
177 no_argument (or 0) if the option does not take an argument,
178 required_argument (or 1) if the option requires an argument,
179 optional_argument (or 2) if the option takes an optional argument.
180
181 If the field 'flag' is not NULL, it points to a variable that is set
182 to the value given in the field 'val' when the option is found, but
183 left unchanged if the option is not found.
184
185 To have a long-named option do something other than set an 'int' to
186 a compiled-in constant, such as set a value from 'optarg', set the
187 option's 'flag' field to zero and its 'val' field to a nonzero
188 value (the equivalent single-letter option character, if there is
189 one). For long options that have a zero 'flag' field, 'getopt'
190 returns the contents of the 'val' field. */
191
192# if !GNULIB_defined_struct_option
193struct option
194{
195 const char *name;
196 /* has_arg can't be an enum because some compilers complain about
197 type mismatches in all the code that assumes it is an int. */
198 int has_arg;
199 int *flag;
200 int val;
201};
202# define GNULIB_defined_struct_option 1
203# endif
204
205/* Names for the values of the 'has_arg' field of 'struct option'. */
206
207# define no_argument 0
208# define required_argument 1
209# define optional_argument 2
210#endif /* need getopt */
211
212
213/* Get definitions and prototypes for functions to process the
214 arguments in ARGV (ARGC of them, minus the program name) for
215 options given in OPTS.
216
217 Return the option character from OPTS just read. Return -1 when
218 there are no more options. For unrecognized options, or options
219 missing arguments, 'optopt' is set to the option letter, and '?' is
220 returned.
221
222 The OPTS string is a list of characters which are recognized option
223 letters, optionally followed by colons, specifying that that letter
224 takes an argument, to be placed in 'optarg'.
225
226 If a letter in OPTS is followed by two colons, its argument is
227 optional. This behavior is specific to the GNU 'getopt'.
228
229 The argument '--' causes premature termination of argument
230 scanning, explicitly telling 'getopt' that there are no more
231 options.
232
233 If OPTS begins with '-', then non-option arguments are treated as
234 arguments to the option '\1'. This behavior is specific to the GNU
235 'getopt'. If OPTS begins with '+', or POSIXLY_CORRECT is set in
236 the environment, then do not permute arguments. */
237
238extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
239 __THROW _GL_ARG_NONNULL ((2, 3));
240
241#ifndef __GETOPT_PREFIX
242# if defined __need_getopt && defined __USE_POSIX2 \
243 && !defined __USE_POSIX_IMPLICITLY && !defined __USE_GNU
244/* The GNU getopt has more functionality than the standard version. The
245 additional functionality can be disable at runtime. This redirection
246 helps to also do this at runtime. */
247# ifdef __REDIRECT
248 extern int __REDIRECT_NTH (getopt, (int ___argc, char *const *___argv,
249 const char *__shortopts),
250 __posix_getopt);
251# else
252extern int __posix_getopt (int ___argc, char *const *___argv,
253 const char *__shortopts)
254 __THROW _GL_ARG_NONNULL ((2, 3));
255# define getopt __posix_getopt
256# endif
257# endif
258#endif
259
260#ifndef __need_getopt
261extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
262 const char *__shortopts,
263 const struct option *__longopts, int *__longind)
264 __THROW _GL_ARG_NONNULL ((2, 3));
265extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
266 const char *__shortopts,
267 const struct option *__longopts, int *__longind)
268 __THROW _GL_ARG_NONNULL ((2, 3));
269
270#endif
271
272#ifdef __cplusplus
273}
274#endif
275
276/* Make sure we later can get all the definitions and declarations. */
277#undef __need_getopt
278
279#endif /* _@GUARD_PREFIX@_GETOPT_H */
280#endif /* _@GUARD_PREFIX@_GETOPT_H */ 59#endif /* _@GUARD_PREFIX@_GETOPT_H */
diff --git a/lib/getopt1.c b/lib/getopt1.c
index 166b1295333..2bc5926016a 100644
--- a/lib/getopt1.c
+++ b/lib/getopt1.c
@@ -1,6 +1,7 @@
1/* getopt_long and getopt_long_only entry points for GNU getopt. 1/* getopt_long and getopt_long_only entry points for GNU getopt.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc. 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
4 5
5 The GNU C Library is free software; you can redistribute it and/or 6 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public 7 modify it under the terms of the GNU General Public
@@ -16,41 +17,28 @@
16 License along with the GNU C Library; if not, see 17 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */ 18 <http://www.gnu.org/licenses/>. */
18 19
19#ifdef _LIBC 20#ifndef _LIBC
20# include <getopt.h>
21#else
22# include <config.h> 21# include <config.h>
23# include "getopt.h"
24#endif 22#endif
25#include "getopt_int.h"
26
27#include <stdio.h>
28 23
29/* This needs to come after some library #include 24#include "getopt.h"
30 to get __GNU_LIBRARY__ defined. */ 25#include "getopt_int.h"
31#ifdef __GNU_LIBRARY__
32#include <stdlib.h>
33#endif
34
35#ifndef NULL
36#define NULL 0
37#endif
38 26
39int 27int
40getopt_long (int argc, char *__getopt_argv_const *argv, const char *options, 28getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
41 const struct option *long_options, int *opt_index) 29 const struct option *long_options, int *opt_index)
42{ 30{
43 return _getopt_internal (argc, (char **) argv, options, long_options, 31 return _getopt_internal (argc, (char **) argv, options, long_options,
44 opt_index, 0, 0); 32 opt_index, 0, 0);
45} 33}
46 34
47int 35int
48_getopt_long_r (int argc, char **argv, const char *options, 36_getopt_long_r (int argc, char **argv, const char *options,
49 const struct option *long_options, int *opt_index, 37 const struct option *long_options, int *opt_index,
50 struct _getopt_data *d) 38 struct _getopt_data *d)
51{ 39{
52 return _getopt_internal_r (argc, argv, options, long_options, opt_index, 40 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
53 0, d, 0); 41 0, d, 0);
54} 42}
55 43
56/* Like getopt_long, but '-' as well as '--' can indicate a long option. 44/* 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,
60 48
61int 49int
62getopt_long_only (int argc, char *__getopt_argv_const *argv, 50getopt_long_only (int argc, char *__getopt_argv_const *argv,
63 const char *options, 51 const char *options,
64 const struct option *long_options, int *opt_index) 52 const struct option *long_options, int *opt_index)
65{ 53{
66 return _getopt_internal (argc, (char **) argv, options, long_options, 54 return _getopt_internal (argc, (char **) argv, options, long_options,
67 opt_index, 1, 0); 55 opt_index, 1, 0);
68} 56}
69 57
70int 58int
71_getopt_long_only_r (int argc, char **argv, const char *options, 59_getopt_long_only_r (int argc, char **argv, const char *options,
72 const struct option *long_options, int *opt_index, 60 const struct option *long_options, int *opt_index,
73 struct _getopt_data *d) 61 struct _getopt_data *d)
74{ 62{
75 return _getopt_internal_r (argc, argv, options, long_options, opt_index, 63 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
76 1, d, 0); 64 1, d, 0);
77} 65}
78 66
79 67
80#ifdef TEST 68#ifdef TEST
81 69
82#include <stdio.h> 70#include <stdio.h>
71#include <stdlib.h>
83 72
84int 73int
85main (int argc, char **argv) 74main (int argc, char **argv)
@@ -93,74 +82,74 @@ main (int argc, char **argv)
93 int option_index = 0; 82 int option_index = 0;
94 static const struct option long_options[] = 83 static const struct option long_options[] =
95 { 84 {
96 {"add", 1, 0, 0}, 85 {"add", 1, 0, 0},
97 {"append", 0, 0, 0}, 86 {"append", 0, 0, 0},
98 {"delete", 1, 0, 0}, 87 {"delete", 1, 0, 0},
99 {"verbose", 0, 0, 0}, 88 {"verbose", 0, 0, 0},
100 {"create", 0, 0, 0}, 89 {"create", 0, 0, 0},
101 {"file", 1, 0, 0}, 90 {"file", 1, 0, 0},
102 {0, 0, 0, 0} 91 {0, 0, 0, 0}
103 }; 92 };
104 93
105 c = getopt_long (argc, argv, "abc:d:0123456789", 94 c = getopt_long (argc, argv, "abc:d:0123456789",
106 long_options, &option_index); 95 long_options, &option_index);
107 if (c == -1) 96 if (c == -1)
108 break; 97 break;
109 98
110 switch (c) 99 switch (c)
111 { 100 {
112 case 0: 101 case 0:
113 printf ("option %s", long_options[option_index].name); 102 printf ("option %s", long_options[option_index].name);
114 if (optarg) 103 if (optarg)
115 printf (" with arg %s", optarg); 104 printf (" with arg %s", optarg);
116 printf ("\n"); 105 printf ("\n");
117 break; 106 break;
118 107
119 case '0': 108 case '0':
120 case '1': 109 case '1':
121 case '2': 110 case '2':
122 case '3': 111 case '3':
123 case '4': 112 case '4':
124 case '5': 113 case '5':
125 case '6': 114 case '6':
126 case '7': 115 case '7':
127 case '8': 116 case '8':
128 case '9': 117 case '9':
129 if (digit_optind != 0 && digit_optind != this_option_optind) 118 if (digit_optind != 0 && digit_optind != this_option_optind)
130 printf ("digits occur in two different argv-elements.\n"); 119 printf ("digits occur in two different argv-elements.\n");
131 digit_optind = this_option_optind; 120 digit_optind = this_option_optind;
132 printf ("option %c\n", c); 121 printf ("option %c\n", c);
133 break; 122 break;
134 123
135 case 'a': 124 case 'a':
136 printf ("option a\n"); 125 printf ("option a\n");
137 break; 126 break;
138 127
139 case 'b': 128 case 'b':
140 printf ("option b\n"); 129 printf ("option b\n");
141 break; 130 break;
142 131
143 case 'c': 132 case 'c':
144 printf ("option c with value '%s'\n", optarg); 133 printf ("option c with value '%s'\n", optarg);
145 break; 134 break;
146 135
147 case 'd': 136 case 'd':
148 printf ("option d with value '%s'\n", optarg); 137 printf ("option d with value '%s'\n", optarg);
149 break; 138 break;
150 139
151 case '?': 140 case '?':
152 break; 141 break;
153 142
154 default: 143 default:
155 printf ("?? getopt returned character code 0%o ??\n", c); 144 printf ("?? getopt returned character code 0%o ??\n", c);
156 } 145 }
157 } 146 }
158 147
159 if (optind < argc) 148 if (optind < argc)
160 { 149 {
161 printf ("non-option ARGV-elements: "); 150 printf ("non-option ARGV-elements: ");
162 while (optind < argc) 151 while (optind < argc)
163 printf ("%s ", argv[optind++]); 152 printf ("%s ", argv[optind++]);
164 printf ("\n"); 153 printf ("\n");
165 } 154 }
166 155
diff --git a/lib/getopt_cdefs.in.h b/lib/getopt_cdefs.in.h
new file mode 100644
index 00000000000..18f9fb4f98a
--- /dev/null
+++ b/lib/getopt_cdefs.in.h
@@ -0,0 +1,75 @@
1/* getopt-on-non-glibc compatibility macros.
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of gnulib.
4 Unlike most of the getopt implementation, it is NOT shared
5 with the GNU C Library.
6
7 gnulib is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of
10 the License, or (at your option) any later version.
11
12 gnulib is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public
18 License along with gnulib; if not, see
19 <http://www.gnu.org/licenses/>. */
20
21#ifndef _GETOPT_CDEFS_H
22#define _GETOPT_CDEFS_H 1
23
24/* This header should not be used directly; include getopt.h or
25 unistd.h instead. It does not have a protective #error, because
26 the guard macro for getopt.h in gnulib is not fixed. */
27
28/* getopt_core.h and getopt_ext.h are shared with GNU libc, and expect
29 a number of the internal macros supplied to GNU libc's headers by
30 sys/cdefs.h. Provide fallback definitions for all of them. */
31#if @HAVE_SYS_CDEFS_H@
32# include <sys/cdefs.h>
33#endif
34
35#ifndef __BEGIN_DECLS
36# ifdef __cplusplus
37# define __BEGIN_DECLS extern "C" {
38# else
39# define __BEGIN_DECLS /* nothing */
40# endif
41#endif
42#ifndef __END_DECLS
43# ifdef __cplusplus
44# define __END_DECLS }
45# else
46# define __END_DECLS /* nothing */
47# endif
48#endif
49
50#ifndef __GNUC_PREREQ
51# if defined __GNUC__ && defined __GNUC_VERSION__
52# define __GNUC_PREREQ(maj, min) \
53 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
54# else
55# define __GNUC_PREREQ(maj, min) 0
56# endif
57#endif
58
59#ifndef __THROW
60# if defined __cplusplus && __GNUC_PREREQ (2,8)
61# define __THROW throw ()
62# else
63# define __THROW
64# endif
65#endif
66
67#ifndef __nonnull
68# if __GNUC_PREREQ (3,3)
69# define __nonnull(params) __attribute__ ((__nonnull__ params))
70# else
71# define __nonnull(params)
72# endif
73#endif
74
75#endif /* getopt_cdefs.h */
diff --git a/lib/getopt_core.h b/lib/getopt_core.h
new file mode 100644
index 00000000000..d6ef8a6b782
--- /dev/null
+++ b/lib/getopt_core.h
@@ -0,0 +1,96 @@
1/* Declarations for getopt (basic, portable features only).
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#ifndef _GETOPT_CORE_H
21#define _GETOPT_CORE_H 1
22
23/* This header should not be used directly; include getopt.h or
24 unistd.h instead. Unlike most bits headers, it does not have
25 a protective #error, because the guard macro for getopt.h in
26 gnulib is not fixed. */
27
28__BEGIN_DECLS
29
30/* For communication from 'getopt' to the caller.
31 When 'getopt' finds an option that takes an argument,
32 the argument value is returned here.
33 Also, when 'ordering' is RETURN_IN_ORDER,
34 each non-option ARGV-element is returned here. */
35
36extern char *optarg;
37
38/* Index in ARGV of the next element to be scanned.
39 This is used for communication to and from the caller
40 and for communication between successive calls to 'getopt'.
41
42 On entry to 'getopt', zero means this is the first call; initialize.
43
44 When 'getopt' returns -1, this is the index of the first of the
45 non-option elements that the caller should itself scan.
46
47 Otherwise, 'optind' communicates from one call to the next
48 how much of ARGV has been scanned so far. */
49
50extern int optind;
51
52/* Callers store zero here to inhibit the error message 'getopt' prints
53 for unrecognized options. */
54
55extern int opterr;
56
57/* Set to an option character which was unrecognized. */
58
59extern int optopt;
60
61/* Get definitions and prototypes for functions to process the
62 arguments in ARGV (ARGC of them, minus the program name) for
63 options given in OPTS.
64
65 Return the option character from OPTS just read. Return -1 when
66 there are no more options. For unrecognized options, or options
67 missing arguments, 'optopt' is set to the option letter, and '?' is
68 returned.
69
70 The OPTS string is a list of characters which are recognized option
71 letters, optionally followed by colons, specifying that that letter
72 takes an argument, to be placed in 'optarg'.
73
74 If a letter in OPTS is followed by two colons, its argument is
75 optional. This behavior is specific to the GNU 'getopt'.
76
77 The argument '--' causes premature termination of argument
78 scanning, explicitly telling 'getopt' that there are no more
79 options.
80
81 If OPTS begins with '-', then non-option arguments are treated as
82 arguments to the option '\1'. This behavior is specific to the GNU
83 'getopt'. If OPTS begins with '+', or POSIXLY_CORRECT is set in
84 the environment, then do not permute arguments.
85
86 For standards compliance, the 'argv' argument has the type
87 char *const *, but this is inaccurate; if argument permutation is
88 enabled, the argv array (not the strings it points to) must be
89 writable. */
90
91extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
92 __THROW __nonnull ((2, 3));
93
94__END_DECLS
95
96#endif /* getopt_core.h */
diff --git a/lib/getopt_ext.h b/lib/getopt_ext.h
new file mode 100644
index 00000000000..79b7a670d58
--- /dev/null
+++ b/lib/getopt_ext.h
@@ -0,0 +1,77 @@
1/* Declarations for getopt (GNU extensions).
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#ifndef _GETOPT_EXT_H
21#define _GETOPT_EXT_H 1
22
23/* This header should not be used directly; include getopt.h instead.
24 Unlike most bits headers, it does not have a protective #error,
25 because the guard macro for getopt.h in gnulib is not fixed. */
26
27__BEGIN_DECLS
28
29/* Describe the long-named options requested by the application.
30 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
31 of 'struct option' terminated by an element containing a name which is
32 zero.
33
34 The field 'has_arg' is:
35 no_argument (or 0) if the option does not take an argument,
36 required_argument (or 1) if the option requires an argument,
37 optional_argument (or 2) if the option takes an optional argument.
38
39 If the field 'flag' is not NULL, it points to a variable that is set
40 to the value given in the field 'val' when the option is found, but
41 left unchanged if the option is not found.
42
43 To have a long-named option do something other than set an 'int' to
44 a compiled-in constant, such as set a value from 'optarg', set the
45 option's 'flag' field to zero and its 'val' field to a nonzero
46 value (the equivalent single-letter option character, if there is
47 one). For long options that have a zero 'flag' field, 'getopt'
48 returns the contents of the 'val' field. */
49
50struct option
51{
52 const char *name;
53 /* has_arg can't be an enum because some compilers complain about
54 type mismatches in all the code that assumes it is an int. */
55 int has_arg;
56 int *flag;
57 int val;
58};
59
60/* Names for the values of the 'has_arg' field of 'struct option'. */
61
62#define no_argument 0
63#define required_argument 1
64#define optional_argument 2
65
66extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
67 const char *__shortopts,
68 const struct option *__longopts, int *__longind)
69 __THROW __nonnull ((2, 3));
70extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
71 const char *__shortopts,
72 const struct option *__longopts, int *__longind)
73 __THROW __nonnull ((2, 3));
74
75__END_DECLS
76
77#endif /* getopt_ext.h */
diff --git a/lib/getopt_int.h b/lib/getopt_int.h
index c614fcb65be..a5562195123 100644
--- a/lib/getopt_int.h
+++ b/lib/getopt_int.h
@@ -1,6 +1,7 @@
1/* Internal declarations for getopt. 1/* Internal declarations for getopt.
2 Copyright (C) 1989-2017 Free Software Foundation, Inc. 2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
4 5
5 The GNU C Library is free software; you can redistribute it and/or 6 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public 7 modify it under the terms of the GNU General Public
@@ -17,14 +18,14 @@
17 <http://www.gnu.org/licenses/>. */ 18 <http://www.gnu.org/licenses/>. */
18 19
19#ifndef _GETOPT_INT_H 20#ifndef _GETOPT_INT_H
20#define _GETOPT_INT_H 1 21#define _GETOPT_INT_H 1
21 22
22#include <getopt.h> 23#include <getopt.h>
23 24
24extern int _getopt_internal (int ___argc, char **___argv, 25extern int _getopt_internal (int ___argc, char **___argv,
25 const char *__shortopts, 26 const char *__shortopts,
26 const struct option *__longopts, int *__longind, 27 const struct option *__longopts, int *__longind,
27 int __long_only, int __posixly_correct); 28 int __long_only, int __posixly_correct);
28 29
29 30
30/* Reentrant versions which can handle parsing multiple argument 31/* Reentrant versions which can handle parsing multiple argument
@@ -32,28 +33,20 @@ extern int _getopt_internal (int ___argc, char **___argv,
32 33
33/* Describe how to deal with options that follow non-option ARGV-elements. 34/* Describe how to deal with options that follow non-option ARGV-elements.
34 35
35 If the caller did not specify anything, 36 REQUIRE_ORDER means don't recognize them as options; stop option
36 the default is REQUIRE_ORDER if the environment variable 37 processing when the first non-option is seen. This is what POSIX
37 POSIXLY_CORRECT is defined, PERMUTE otherwise. 38 specifies should happen.
38 39
39 REQUIRE_ORDER means don't recognize them as options; 40 PERMUTE means permute the contents of ARGV as we scan, so that
40 stop option processing when the first non-option is seen. 41 eventually all the non-options are at the end. This allows options
41 This is what Unix does. 42 to be given in any order, even with programs that were not written
42 This mode of operation is selected by either setting the environment 43 to expect this.
43 variable POSIXLY_CORRECT, or using '+' as the first character
44 of the list of option characters, or by calling getopt.
45
46 PERMUTE is the default. We permute the contents of ARGV as we
47 scan, so that eventually all the non-options are at the end.
48 This allows options to be given in any order, even with programs
49 that were not written to expect this.
50 44
51 RETURN_IN_ORDER is an option available to programs that were 45 RETURN_IN_ORDER is an option available to programs that were
52 written to expect options and other ARGV-elements in any order 46 written to expect options and other ARGV-elements in any order
53 and that care about the ordering of the two. We describe each 47 and that care about the ordering of the two. We describe each
54 non-option ARGV-element as if it were the argument of an option 48 non-option ARGV-element as if it were the argument of an option
55 with character code 1. Using '-' as the first character of the 49 with character code 1.
56 list of option characters selects this mode of operation.
57 50
58 The special argument '--' forces an end of option-scanning regardless 51 The special argument '--' forces an end of option-scanning regardless
59 of the value of 'ordering'. In the case of RETURN_IN_ORDER, only 52 of the value of 'ordering'. In the case of RETURN_IN_ORDER, only
@@ -91,11 +84,6 @@ struct _getopt_data
91 /* See __ord above. */ 84 /* See __ord above. */
92 enum __ord __ordering; 85 enum __ord __ordering;
93 86
94 /* If the POSIXLY_CORRECT environment variable is set
95 or getopt was called. */
96 int __posixly_correct;
97
98
99 /* Handle permutation of arguments. */ 87 /* Handle permutation of arguments. */
100 88
101 /* Describe the part of ARGV that contains non-options that have 89 /* Describe the part of ARGV that contains non-options that have
@@ -104,32 +92,27 @@ struct _getopt_data
104 92
105 int __first_nonopt; 93 int __first_nonopt;
106 int __last_nonopt; 94 int __last_nonopt;
107
108#if defined _LIBC && defined USE_NONOPTION_FLAGS
109 int __nonoption_flags_max_len;
110 int __nonoption_flags_len;
111#endif
112}; 95};
113 96
114/* The initializer is necessary to set OPTIND and OPTERR to their 97/* The initializer is necessary to set OPTIND and OPTERR to their
115 default values and to clear the initialization flag. */ 98 default values and to clear the initialization flag. */
116#define _GETOPT_DATA_INITIALIZER { 1, 1 } 99#define _GETOPT_DATA_INITIALIZER { 1, 1 }
117 100
118extern int _getopt_internal_r (int ___argc, char **___argv, 101extern int _getopt_internal_r (int ___argc, char **___argv,
119 const char *__shortopts, 102 const char *__shortopts,
120 const struct option *__longopts, int *__longind, 103 const struct option *__longopts, int *__longind,
121 int __long_only, struct _getopt_data *__data, 104 int __long_only, struct _getopt_data *__data,
122 int __posixly_correct); 105 int __posixly_correct);
123 106
124extern int _getopt_long_r (int ___argc, char **___argv, 107extern int _getopt_long_r (int ___argc, char **___argv,
125 const char *__shortopts, 108 const char *__shortopts,
126 const struct option *__longopts, int *__longind, 109 const struct option *__longopts, int *__longind,
127 struct _getopt_data *__data); 110 struct _getopt_data *__data);
128 111
129extern int _getopt_long_only_r (int ___argc, char **___argv, 112extern int _getopt_long_only_r (int ___argc, char **___argv,
130 const char *__shortopts, 113 const char *__shortopts,
131 const struct option *__longopts, 114 const struct option *__longopts,
132 int *__longind, 115 int *__longind,
133 struct _getopt_data *__data); 116 struct _getopt_data *__data);
134 117
135#endif /* getopt_int.h */ 118#endif /* getopt_int.h */
diff --git a/lib/getopt_pfx_core.h b/lib/getopt_pfx_core.h
new file mode 100644
index 00000000000..d585a08ce9f
--- /dev/null
+++ b/lib/getopt_pfx_core.h
@@ -0,0 +1,54 @@
1/* getopt (basic, portable features) gnulib wrapper header.
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of gnulib.
4 Unlike most of the getopt implementation, it is NOT shared
5 with the GNU C Library.
6
7 gnulib is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of
10 the License, or (at your option) any later version.
11
12 gnulib is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public
18 License along with gnulib; if not, see
19 <http://www.gnu.org/licenses/>. */
20
21#ifndef _GETOPT_PFX_CORE_H
22#define _GETOPT_PFX_CORE_H 1
23
24/* This header should not be used directly; include getopt.h or
25 unistd.h instead. It does not have a protective #error, because
26 the guard macro for getopt.h in gnulib is not fixed. */
27
28/* Standalone applications should #define __GETOPT_PREFIX to an
29 identifier that prefixes the external functions and variables
30 defined in getopt_core.h and getopt_ext.h. Systematically
31 rename identifiers so that they do not collide with the system
32 functions and variables. Renaming avoids problems with some
33 compilers and linkers. */
34#ifdef __GETOPT_PREFIX
35# ifndef __GETOPT_ID
36# define __GETOPT_CONCAT(x, y) x ## y
37# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
38# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
39# endif
40# undef getopt
41# undef optarg
42# undef opterr
43# undef optind
44# undef optopt
45# define getopt __GETOPT_ID (getopt)
46# define optarg __GETOPT_ID (optarg)
47# define opterr __GETOPT_ID (opterr)
48# define optind __GETOPT_ID (optind)
49# define optopt __GETOPT_ID (optopt)
50#endif
51
52#include <getopt_core.h>
53
54#endif /* getopt_pfx_core.h */
diff --git a/lib/getopt_pfx_ext.h b/lib/getopt_pfx_ext.h
new file mode 100644
index 00000000000..3da156a09ba
--- /dev/null
+++ b/lib/getopt_pfx_ext.h
@@ -0,0 +1,64 @@
1/* getopt (GNU extensions) gnulib wrapper header.
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 This file is part of gnulib.
4 Unlike most of the getopt implementation, it is NOT shared
5 with the GNU C Library.
6
7 gnulib is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of
10 the License, or (at your option) any later version.
11
12 gnulib is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public
18 License along with gnulib; if not, see
19 <http://www.gnu.org/licenses/>. */
20
21#ifndef _GETOPT_PFX_EXT_H
22#define _GETOPT_PFX_EXT_H 1
23
24/* This header should not be used directly; include getopt.h instead.
25 It does not have a protective #error, because the guard macro for
26 getopt.h in gnulib is not fixed. */
27
28/* Standalone applications should #define __GETOPT_PREFIX to an
29 identifier that prefixes the external functions and variables
30 defined in getopt_core.h and getopt_ext.h. Systematically
31 rename identifiers so that they do not collide with the system
32 functions and variables. Renaming avoids problems with some
33 compilers and linkers. */
34#ifdef __GETOPT_PREFIX
35# ifndef __GETOPT_ID
36# define __GETOPT_CONCAT(x, y) x ## y
37# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
38# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
39# endif
40# undef getopt_long
41# undef getopt_long_only
42# undef option
43# define getopt_long __GETOPT_ID (getopt_long)
44# define getopt_long_only __GETOPT_ID (getopt_long_only)
45# define option __GETOPT_ID (option)
46#endif
47
48/* Standalone applications get correct prototypes for getopt_long and
49 getopt_long_only; they declare "char **argv". For backward
50 compatibility with old applications, if __GETOPT_PREFIX is not
51 defined, we supply GNU-libc-compatible, but incorrect, prototypes
52 using "char *const *argv". (GNU libc is stuck with the incorrect
53 prototypes, as they are baked into older versions of LSB.) */
54#ifndef __getopt_argv_const
55# if defined __GETOPT_PREFIX
56# define __getopt_argv_const /* empty */
57# else
58# define __getopt_argv_const const
59# endif
60#endif
61
62#include <getopt_ext.h>
63
64#endif /* getopt_pfx_ext.h */
diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index ca95ed7e825..38adf2a9f76 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -84,6 +84,7 @@ EOVERFLOW_VALUE = @EOVERFLOW_VALUE@
84ERRNO_H = @ERRNO_H@ 84ERRNO_H = @ERRNO_H@
85EXECINFO_H = @EXECINFO_H@ 85EXECINFO_H = @EXECINFO_H@
86EXEEXT = @EXEEXT@ 86EXEEXT = @EXEEXT@
87FIND_DELETE = @FIND_DELETE@
87FIRSTFILE_OBJ = @FIRSTFILE_OBJ@ 88FIRSTFILE_OBJ = @FIRSTFILE_OBJ@
88FONTCONFIG_CFLAGS = @FONTCONFIG_CFLAGS@ 89FONTCONFIG_CFLAGS = @FONTCONFIG_CFLAGS@
89FONTCONFIG_LIBS = @FONTCONFIG_LIBS@ 90FONTCONFIG_LIBS = @FONTCONFIG_LIBS@
@@ -94,6 +95,7 @@ GCONF_CFLAGS = @GCONF_CFLAGS@
94GCONF_LIBS = @GCONF_LIBS@ 95GCONF_LIBS = @GCONF_LIBS@
95GETADDRINFO_A_LIBS = @GETADDRINFO_A_LIBS@ 96GETADDRINFO_A_LIBS = @GETADDRINFO_A_LIBS@
96GETLOADAVG_LIBS = @GETLOADAVG_LIBS@ 97GETLOADAVG_LIBS = @GETLOADAVG_LIBS@
98GETOPT_CDEFS_H = @GETOPT_CDEFS_H@
97GETOPT_H = @GETOPT_H@ 99GETOPT_H = @GETOPT_H@
98GFILENOTIFY_CFLAGS = @GFILENOTIFY_CFLAGS@ 100GFILENOTIFY_CFLAGS = @GFILENOTIFY_CFLAGS@
99GFILENOTIFY_LIBS = @GFILENOTIFY_LIBS@ 101GFILENOTIFY_LIBS = @GFILENOTIFY_LIBS@
@@ -488,6 +490,7 @@ HAVE_STRVERSCMP = @HAVE_STRVERSCMP@
488HAVE_SYMLINK = @HAVE_SYMLINK@ 490HAVE_SYMLINK = @HAVE_SYMLINK@
489HAVE_SYMLINKAT = @HAVE_SYMLINKAT@ 491HAVE_SYMLINKAT = @HAVE_SYMLINKAT@
490HAVE_SYS_BITYPES_H = @HAVE_SYS_BITYPES_H@ 492HAVE_SYS_BITYPES_H = @HAVE_SYS_BITYPES_H@
493HAVE_SYS_CDEFS_H = @HAVE_SYS_CDEFS_H@
491HAVE_SYS_INTTYPES_H = @HAVE_SYS_INTTYPES_H@ 494HAVE_SYS_INTTYPES_H = @HAVE_SYS_INTTYPES_H@
492HAVE_SYS_LOADAVG_H = @HAVE_SYS_LOADAVG_H@ 495HAVE_SYS_LOADAVG_H = @HAVE_SYS_LOADAVG_H@
493HAVE_SYS_PARAM_H = @HAVE_SYS_PARAM_H@ 496HAVE_SYS_PARAM_H = @HAVE_SYS_PARAM_H@
@@ -1511,11 +1514,11 @@ endif
1511## begin gnulib module getopt-posix 1514## begin gnulib module getopt-posix
1512ifeq (,$(OMIT_GNULIB_MODULE_getopt-posix)) 1515ifeq (,$(OMIT_GNULIB_MODULE_getopt-posix))
1513 1516
1514BUILT_SOURCES += $(GETOPT_H) 1517BUILT_SOURCES += $(GETOPT_H) $(GETOPT_CDEFS_H)
1515 1518
1516# We need the following in order to create <getopt.h> when the system 1519# We need the following in order to create <getopt.h> when the system
1517# doesn't have one that works with the given compiler. 1520# doesn't have one that works with the given compiler.
1518getopt.h: getopt.in.h $(top_builddir)/config.status $(ARG_NONNULL_H) 1521getopt.h: getopt.in.h $(top_builddir)/config.status
1519 $(AM_V_GEN)rm -f $@-t $@ && \ 1522 $(AM_V_GEN)rm -f $@-t $@ && \
1520 { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \ 1523 { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
1521 sed -e 's|@''GUARD_PREFIX''@|GL|g' \ 1524 sed -e 's|@''GUARD_PREFIX''@|GL|g' \
@@ -1524,13 +1527,21 @@ getopt.h: getopt.in.h $(top_builddir)/config.status $(ARG_NONNULL_H)
1524 -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ 1527 -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
1525 -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \ 1528 -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
1526 -e 's|@''NEXT_GETOPT_H''@|$(NEXT_GETOPT_H)|g' \ 1529 -e 's|@''NEXT_GETOPT_H''@|$(NEXT_GETOPT_H)|g' \
1527 -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
1528 < $(srcdir)/getopt.in.h; \ 1530 < $(srcdir)/getopt.in.h; \
1529 } > $@-t && \ 1531 } > $@-t && \
1530 mv -f $@-t $@ 1532 mv -f $@-t $@
1531MOSTLYCLEANFILES += getopt.h getopt.h-t
1532 1533
1533EXTRA_DIST += getopt.c getopt.in.h getopt1.c getopt_int.h 1534getopt_cdefs.h: getopt_cdefs.in.h $(top_builddir)/config.status
1535 $(AM_V_GEN)rm -f $@-t $@ && \
1536 { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
1537 sed -e 's|@''HAVE_SYS_CDEFS_H''@|$(HAVE_SYS_CDEFS_H)|g' \
1538 < $(srcdir)/getopt_cdefs.in.h; \
1539 } > $@-t && \
1540 mv -f $@-t $@
1541
1542MOSTLYCLEANFILES += getopt.h getopt.h-t getopt_cdefs.h getopt_cdefs.h-t
1543
1544EXTRA_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
1534 1545
1535EXTRA_libgnu_a_SOURCES += getopt.c getopt1.c 1546EXTRA_libgnu_a_SOURCES += getopt.c getopt1.c
1536 1547
diff --git a/lib/md5.c b/lib/md5.c
index e82b0514a0a..dcbba45ddff 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)
246 md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); 246 md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
247 247
248 ctx->buflen &= 63; 248 ctx->buflen &= 63;
249 /* The regions in the following copy operation cannot overlap. */ 249 /* The regions in the following copy operation cannot overlap,
250 because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
250 memcpy (ctx->buffer, 251 memcpy (ctx->buffer,
251 &((char *) ctx->buffer)[(left_over + add) & ~63], 252 &((char *) ctx->buffer)[(left_over + add) & ~63],
252 ctx->buflen); 253 ctx->buflen);
@@ -288,6 +289,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
288 { 289 {
289 md5_process_block (ctx->buffer, 64, ctx); 290 md5_process_block (ctx->buffer, 64, ctx);
290 left_over -= 64; 291 left_over -= 64;
292 /* The regions in the following copy operation cannot overlap,
293 because left_over ≤ 64. */
291 memcpy (ctx->buffer, &ctx->buffer[16], left_over); 294 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
292 } 295 }
293 ctx->buflen = left_over; 296 ctx->buflen = left_over;
diff --git a/lib/md5.h b/lib/md5.h
index fb20b7b1bc2..e38a6198708 100644
--- a/lib/md5.h
+++ b/lib/md5.h
@@ -74,8 +74,8 @@ struct md5_ctx
74 uint32_t D; 74 uint32_t D;
75 75
76 uint32_t total[2]; 76 uint32_t total[2];
77 uint32_t buflen; 77 uint32_t buflen; /* ≥ 0, ≤ 128 */
78 uint32_t buffer[32]; 78 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
79}; 79};
80 80
81/* 81/*
diff --git a/lib/sha1.c b/lib/sha1.c
index ea0474f2ce0..a57814131d6 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)
233 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx); 233 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
234 234
235 ctx->buflen &= 63; 235 ctx->buflen &= 63;
236 /* The regions in the following copy operation cannot overlap. */ 236 /* The regions in the following copy operation cannot overlap,
237 because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
237 memcpy (ctx->buffer, 238 memcpy (ctx->buffer,
238 &((char *) ctx->buffer)[(left_over + add) & ~63], 239 &((char *) ctx->buffer)[(left_over + add) & ~63],
239 ctx->buflen); 240 ctx->buflen);
@@ -275,6 +276,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
275 { 276 {
276 sha1_process_block (ctx->buffer, 64, ctx); 277 sha1_process_block (ctx->buffer, 64, ctx);
277 left_over -= 64; 278 left_over -= 64;
279 /* The regions in the following copy operation cannot overlap,
280 because left_over ≤ 64. */
278 memcpy (ctx->buffer, &ctx->buffer[16], left_over); 281 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
279 } 282 }
280 ctx->buflen = left_over; 283 ctx->buflen = left_over;
diff --git a/lib/sha1.h b/lib/sha1.h
index 7d7e19a64fd..fcef9ce2c60 100644
--- a/lib/sha1.h
+++ b/lib/sha1.h
@@ -46,8 +46,8 @@ struct sha1_ctx
46 uint32_t E; 46 uint32_t E;
47 47
48 uint32_t total[2]; 48 uint32_t total[2];
49 uint32_t buflen; 49 uint32_t buflen; /* ≥ 0, ≤ 128 */
50 uint32_t buffer[32]; 50 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
51}; 51};
52 52
53/* Initialize structure containing state of computation. */ 53/* Initialize structure containing state of computation. */
diff --git a/lib/sha256.c b/lib/sha256.c
index 03d3899ad27..c0fb8beecfe 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)
366 sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx); 366 sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
367 367
368 ctx->buflen &= 63; 368 ctx->buflen &= 63;
369 /* The regions in the following copy operation cannot overlap. */ 369 /* The regions in the following copy operation cannot overlap,
370 because ctx->buflen < 64 ≤ (left_over + add) & ~63. */
370 memcpy (ctx->buffer, 371 memcpy (ctx->buffer,
371 &((char *) ctx->buffer)[(left_over + add) & ~63], 372 &((char *) ctx->buffer)[(left_over + add) & ~63],
372 ctx->buflen); 373 ctx->buflen);
@@ -408,6 +409,8 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
408 { 409 {
409 sha256_process_block (ctx->buffer, 64, ctx); 410 sha256_process_block (ctx->buffer, 64, ctx);
410 left_over -= 64; 411 left_over -= 64;
412 /* The regions in the following copy operation cannot overlap,
413 because left_over ≤ 64. */
411 memcpy (ctx->buffer, &ctx->buffer[16], left_over); 414 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
412 } 415 }
413 ctx->buflen = left_over; 416 ctx->buflen = left_over;
diff --git a/lib/sha256.h b/lib/sha256.h
index ffb91fa47ef..348b76ef265 100644
--- a/lib/sha256.h
+++ b/lib/sha256.h
@@ -44,8 +44,8 @@ struct sha256_ctx
44 uint32_t state[8]; 44 uint32_t state[8];
45 45
46 uint32_t total[2]; 46 uint32_t total[2];
47 size_t buflen; 47 size_t buflen; /* ≥ 0, ≤ 128 */
48 uint32_t buffer[32]; 48 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
49}; 49};
50 50
51/* Initialize structure containing state of computation. */ 51/* Initialize structure containing state of computation. */
diff --git a/lib/sha512.c b/lib/sha512.c
index 6876bfdb08c..dbde67183b7 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)
374 sha512_process_block (ctx->buffer, ctx->buflen & ~127, ctx); 374 sha512_process_block (ctx->buffer, ctx->buflen & ~127, ctx);
375 375
376 ctx->buflen &= 127; 376 ctx->buflen &= 127;
377 /* The regions in the following copy operation cannot overlap. */ 377 /* The regions in the following copy operation cannot overlap,
378 because ctx->buflen < 128 ≤ (left_over + add) & ~127. */
378 memcpy (ctx->buffer, 379 memcpy (ctx->buffer,
379 &((char *) ctx->buffer)[(left_over + add) & ~127], 380 &((char *) ctx->buffer)[(left_over + add) & ~127],
380 ctx->buflen); 381 ctx->buflen);
@@ -416,6 +417,8 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
416 { 417 {
417 sha512_process_block (ctx->buffer, 128, ctx); 418 sha512_process_block (ctx->buffer, 128, ctx);
418 left_over -= 128; 419 left_over -= 128;
420 /* The regions in the following copy operation cannot overlap,
421 because left_over ≤ 128. */
419 memcpy (ctx->buffer, &ctx->buffer[16], left_over); 422 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
420 } 423 }
421 ctx->buflen = left_over; 424 ctx->buflen = left_over;
diff --git a/lib/sha512.h b/lib/sha512.h
index 121e6c3a252..4460e6c9b76 100644
--- a/lib/sha512.h
+++ b/lib/sha512.h
@@ -44,8 +44,8 @@ struct sha512_ctx
44 u64 state[8]; 44 u64 state[8];
45 45
46 u64 total[2]; 46 u64 total[2];
47 size_t buflen; 47 size_t buflen; /* ≥ 0, ≤ 256 */
48 u64 buffer[32]; 48 u64 buffer[32]; /* 256 bytes; the first buflen bytes are in use */
49}; 49};
50 50
51/* Initialize structure containing state of computation. */ 51/* Initialize structure containing state of computation. */
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index d5fa34dbe56..ed175bf8f2b 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -130,8 +130,8 @@
130/* Get getopt(), optarg, optind, opterr, optopt. 130/* Get getopt(), optarg, optind, opterr, optopt.
131 But avoid namespace pollution on glibc systems. */ 131 But avoid namespace pollution on glibc systems. */
132#if @GNULIB_UNISTD_H_GETOPT@ && !defined __GLIBC__ && !defined _GL_SYSTEM_GETOPT 132#if @GNULIB_UNISTD_H_GETOPT@ && !defined __GLIBC__ && !defined _GL_SYSTEM_GETOPT
133# define __need_getopt 133# include <getopt_cdefs.h>
134# include <getopt.h> 134# include <getopt_pfx_core.h>
135#endif 135#endif
136 136
137#ifndef _GL_INLINE_HEADER_BEGIN 137#ifndef _GL_INLINE_HEADER_BEGIN
diff --git a/m4/getopt.m4 b/m4/getopt.m4
index 7a946263558..d90076922b2 100644
--- a/m4/getopt.m4
+++ b/m4/getopt.m4
@@ -1,4 +1,4 @@
1# getopt.m4 serial 44 1# getopt.m4 serial 45
2dnl Copyright (C) 2002-2006, 2008-2017 Free Software Foundation, Inc. 2dnl Copyright (C) 2002-2006, 2008-2017 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation 3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it, 4dnl gives unlimited permission to copy and/or distribute it,
@@ -33,8 +33,6 @@ AC_DEFUN([gl_FUNC_GETOPT_POSIX],
33AC_DEFUN([gl_FUNC_GETOPT_GNU], 33AC_DEFUN([gl_FUNC_GETOPT_GNU],
34[ 34[
35 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU]) 35 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
36
37 AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
38]) 36])
39 37
40# Determine whether to replace the entire getopt facility. 38# Determine whether to replace the entire getopt facility.
@@ -354,15 +352,19 @@ dnl is ambiguous with environment values that contain newlines.
354 352
355AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER], 353AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
356[ 354[
357 GETOPT_H=getopt.h 355 AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
356 if test $ac_cv_header_sys_cdefs_h = yes; then
357 HAVE_SYS_CDEFS_H=1
358 else
359 HAVE_SYS_CDEFS_H=0
360 fi
361 AC_SUBST([HAVE_SYS_CDEFS_H])
362
358 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]], 363 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
359 [Define to rpl_ if the getopt replacement functions and variables 364 [Define to rpl_ if the getopt replacement functions and variables
360 should be used.]) 365 should be used.])
366 GETOPT_H=getopt.h
367 GETOPT_CDEFS_H=getopt_cdefs.h
361 AC_SUBST([GETOPT_H]) 368 AC_SUBST([GETOPT_H])
362]) 369 AC_SUBST([GETOPT_CDEFS_H])
363
364# Prerequisites of lib/getopt*.
365AC_DEFUN([gl_PREREQ_GETOPT],
366[
367 AC_CHECK_DECLS_ONCE([getenv])
368]) 370])
diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4
index e613490f1a4..5aea057c5d5 100644
--- a/m4/gnulib-comp.m4
+++ b/m4/gnulib-comp.m4
@@ -259,19 +259,10 @@ AC_DEFUN([gl_INIT],
259 fi 259 fi
260 gl_STDLIB_MODULE_INDICATOR([getloadavg]) 260 gl_STDLIB_MODULE_INDICATOR([getloadavg])
261 gl_FUNC_GETOPT_GNU 261 gl_FUNC_GETOPT_GNU
262 if test $REPLACE_GETOPT = 1; then
263 AC_LIBOBJ([getopt])
264 AC_LIBOBJ([getopt1])
265 gl_PREREQ_GETOPT
266 dnl Arrange for unistd.h to include getopt.h.
267 GNULIB_GL_UNISTD_H_GETOPT=1
268 fi
269 AC_SUBST([GNULIB_GL_UNISTD_H_GETOPT])
270 gl_FUNC_GETOPT_POSIX 262 gl_FUNC_GETOPT_POSIX
271 if test $REPLACE_GETOPT = 1; then 263 if test $REPLACE_GETOPT = 1; then
272 AC_LIBOBJ([getopt]) 264 AC_LIBOBJ([getopt])
273 AC_LIBOBJ([getopt1]) 265 AC_LIBOBJ([getopt1])
274 gl_PREREQ_GETOPT
275 dnl Arrange for unistd.h to include getopt.h. 266 dnl Arrange for unistd.h to include getopt.h.
276 GNULIB_GL_UNISTD_H_GETOPT=1 267 GNULIB_GL_UNISTD_H_GETOPT=1
277 fi 268 fi
@@ -911,7 +902,12 @@ AC_DEFUN([gl_FILE_LIST], [
911 lib/getopt.c 902 lib/getopt.c
912 lib/getopt.in.h 903 lib/getopt.in.h
913 lib/getopt1.c 904 lib/getopt1.c
905 lib/getopt_cdefs.in.h
906 lib/getopt_core.h
907 lib/getopt_ext.h
914 lib/getopt_int.h 908 lib/getopt_int.h
909 lib/getopt_pfx_core.h
910 lib/getopt_pfx_ext.h
915 lib/gettext.h 911 lib/gettext.h
916 lib/gettime.c 912 lib/gettime.c
917 lib/gettimeofday.c 913 lib/gettimeofday.c