getopt: remove USE_NONOPTION_FLAGS

glibc's implementation of getopt includes code to parse an environment
variable named _XXX_GNU_nonoption_argv_flags_ (where XXX is the
current process's PID in decimal); but all of it has been #ifdefed out
since 2001, with no official way to turn it back on.

According to commentary in our config.h.in, bash version 2.0 set this
environment variable to indicate argv elements that were the result of
glob expansion and therefore should not be treated as options, but the
feature was "disabled later" because "it caused problems".  According
to bash's CHANGES file, "later" was release 2.01; it gives no more
detail about what the problems were.

Version 2.0 of bash was released on the last day of 1996, and version
2.01 in June of 1997.  Twenty years later, I think it is safe to
assume that this environment variable isn't coming back.

	* config.h.in (USE_NONOPTION_FLAGS): Remove.
	* csu/init-first.c: Remove all #ifdef USE_NONOPTION_FLAGS blocks.
	* sysdeps/mach/hurd/i386/init-first.c: Likewise.
	* posix/getopt_int.h: Likewise.
	* posix/getopt.c: Likewise. Also remove SWAP_FLAGS and the
	__libc_argc and __libc_argv externs, which were only used by
	#ifdef USE_NONOPTION_FLAGS blocks.
	* posix/getopt_init.c: Remove file.
	* posix/Makefile (routines): Remove getopt_init.
	* include/getopt.h: Don't declare __getopt_initialize_environment.
	* manual/getopt.texi: Remove mention of USE_NONOPTION_FLAGS in
	a comment.
This commit is contained in:
Zack Weinberg 2017-03-29 16:58:58 -04:00
parent e4e794841e
commit bf079e19f5
10 changed files with 18 additions and 211 deletions

View File

@ -1,3 +1,18 @@
2017-04-07 Zack Weinberg <zackw@panix.com>
* config.h.in (USE_NONOPTION_FLAGS): Remove.
* csu/init-first.c: Remove all #ifdef USE_NONOPTION_FLAGS blocks.
* sysdeps/mach/hurd/i386/init-first.c: Likewise.
* posix/getopt_int.h: Likewise.
* posix/getopt.c: Likewise. Also remove SWAP_FLAGS and the
__libc_argc and __libc_argv externs, which were only used by
#ifdef USE_NONOPTION_FLAGS blocks.
* posix/getopt_init.c: Remove file.
* posix/Makefile (routines): Remove getopt_init.
* include/getopt.h: Don't declare __getopt_initialize_environment.
* manual/getopt.texi: Remove mention of USE_NONOPTION_FLAGS in
a comment.
2017-04-07 Florian Weimer <fweimer@redhat.com>
* resolv/res_mkquery.c (res_nmkquery): Remove IQUERY support.

View File

@ -123,12 +123,6 @@
/* Override abi-tags ABI version if necessary. */
#undef __ABI_TAG_VERSION
/* bash 2.0 introduced the _XXX_GNU_nonoption_argv_flags_ variable to help
getopt determine whether a parameter is a flag or not. This features
was disabled later since it caused trouble. We are by default therefore
disabling the support as well. */
#undef USE_NONOPTION_FLAGS
/* Mach/Hurd specific: define if mig supports the `retcode' keyword. */
#undef HAVE_MIG_RETCODE

View File

@ -50,9 +50,6 @@ void
attribute_hidden
_init (int argc, char **argv, char **envp)
{
#endif
#ifdef USE_NONOPTION_FLAGS
extern void __getopt_clean_environment (char **);
#endif
__libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
@ -83,11 +80,6 @@ _init (int argc, char **argv, char **envp)
__init_misc (argc, argv, envp);
#ifdef USE_NONOPTION_FLAGS
/* This is a hack to make the special getopt in GNU libc working. */
__getopt_clean_environment (envp);
#endif
/* Initialize ctype data. */
__ctype_init ();

View File

@ -1,12 +1 @@
#ifndef _GETOPT_H
#include <features.h> /* Get __GNU_LIBRARY__ defined now. */
#include <posix/getopt.h>
# if defined _GETOPT_H && !defined _ISOMAC
/* Now define the internal interfaces. */
extern void __getopt_clean_environment (char **__env);
# endif /* _GETOPT_H && !_ISOMAC */
#endif

View File

@ -70,15 +70,12 @@ option argument, for those options that accept arguments.
@c leave (in case of cancellation) stderr in an inconsistent state.
@c Various implicit, indirect uses of malloc, in uses of memstream and
@c asprintf for error-printing, bring about the usual malloc issues.
@c (The explicit use of malloc in a conditional situation in
@c _getopt_initialize is never exercised in glibc.)
@c
@c _getopt_internal
@c _getopt_internal_r
@c gettext
@c _getopt_initialize
@c getenv
@c malloc if USE_NONOPTION_FLAGS, never defined in libc
@c open_memstream
@c lockfile, unlockfile, __fxprintf -> stderr
@c asprintf

View File

@ -45,7 +45,7 @@ routines := \
pathconf sysconf fpathconf \
glob glob64 fnmatch regex \
confstr \
getopt getopt1 getopt_init \
getopt getopt1 \
sched_setp sched_getp sched_sets sched_gets sched_yield sched_primax \
sched_primin sched_rr_gi sched_getaffinity sched_setaffinity \
getaddrinfo gai_strerror wordexp \

View File

@ -147,36 +147,6 @@ extern char *getenv ();
#endif /* not __GNU_LIBRARY__ */
#ifdef _LIBC
/* Stored original parameters.
XXX This is no good solution. We should rather copy the args so
that we can compare them later. But we must not use malloc(3). */
extern int __libc_argc;
extern char **__libc_argv;
/* Bash 2.0 gives us an environment variable containing flags
indicating ARGV elements that should not be considered arguments. */
# ifdef USE_NONOPTION_FLAGS
/* Defined in getopt_init.c */
extern char *__getopt_nonoption_flags;
# endif
# ifdef USE_NONOPTION_FLAGS
# define SWAP_FLAGS(ch1, ch2) \
if (d->__nonoption_flags_len > 0) \
{ \
char __tmp = __getopt_nonoption_flags[ch1]; \
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
__getopt_nonoption_flags[ch2] = __tmp; \
}
# else
# define SWAP_FLAGS(ch1, ch2)
# endif
#else /* !_LIBC */
# define SWAP_FLAGS(ch1, ch2)
#endif /* _LIBC */
/* Exchange two adjacent subsequences of ARGV.
One subsequence is elements [first_nonopt,last_nonopt)
which contains all the non-options that have been skipped so far.
@ -199,28 +169,6 @@ exchange (char **argv, struct _getopt_data *d)
It leaves the longer segment in the right place overall,
but it consists of two parts that need to be swapped next. */
#if defined _LIBC && defined USE_NONOPTION_FLAGS
/* First make sure the handling of the `__getopt_nonoption_flags'
string can work normally. Our top argument must be in the range
of the string. */
if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
{
/* We must extend the array. The user plays games with us and
presents new arguments. */
char *new_str = malloc (top + 1);
if (new_str == NULL)
d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
else
{
memset (__mempcpy (new_str, __getopt_nonoption_flags,
d->__nonoption_flags_max_len),
'\0', top + 1 - d->__nonoption_flags_max_len);
d->__nonoption_flags_max_len = top + 1;
__getopt_nonoption_flags = new_str;
}
}
#endif
while (top > middle && middle > bottom)
{
if (top - middle > middle - bottom)
@ -235,7 +183,6 @@ exchange (char **argv, struct _getopt_data *d)
tem = argv[bottom + i];
argv[bottom + i] = argv[top - (middle - bottom) + i];
argv[top - (middle - bottom) + i] = tem;
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
}
/* Exclude the moved bottom segment from further swapping. */
top -= len;
@ -252,7 +199,6 @@ exchange (char **argv, struct _getopt_data *d)
tem = argv[bottom + i];
argv[bottom + i] = argv[middle + i];
argv[middle + i] = tem;
SWAP_FLAGS (bottom + i, middle + i);
}
/* Exclude the moved top segment from further swapping. */
bottom += len;
@ -298,36 +244,6 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring,
else
d->__ordering = PERMUTE;
#if defined _LIBC && defined USE_NONOPTION_FLAGS
if (!d->__posixly_correct
&& argc == __libc_argc && argv == __libc_argv)
{
if (d->__nonoption_flags_max_len == 0)
{
if (__getopt_nonoption_flags == NULL
|| __getopt_nonoption_flags[0] == '\0')
d->__nonoption_flags_max_len = -1;
else
{
const char *orig_str = __getopt_nonoption_flags;
int len = d->__nonoption_flags_max_len = strlen (orig_str);
if (d->__nonoption_flags_max_len < argc)
d->__nonoption_flags_max_len = argc;
__getopt_nonoption_flags =
(char *) malloc (d->__nonoption_flags_max_len);
if (__getopt_nonoption_flags == NULL)
d->__nonoption_flags_max_len = -1;
else
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
'\0', d->__nonoption_flags_max_len - len);
}
}
d->__nonoption_flags_len = d->__nonoption_flags_max_len;
}
else
d->__nonoption_flags_len = 0;
#endif
return optstring;
}
@ -412,17 +328,8 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
if (optstring[0] == ':')
print_errors = 0;
/* Test whether ARGV[optind] points to a non-option argument.
Either it does not have option syntax, or there is an environment flag
from the shell indicating it is not an option. The later information
is only used when the used in the GNU libc. */
#if defined _LIBC && defined USE_NONOPTION_FLAGS
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
|| (d->optind < d->__nonoption_flags_len \
&& __getopt_nonoption_flags[d->optind] == '1'))
#else
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
#endif
/* Test whether ARGV[optind] points to a non-option argument. */
#define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
if (d->__nextchar == NULL || *d->__nextchar == '\0')
{

View File

@ -1,74 +0,0 @@
/* Perform additional initialization for getopt functions in GNU libc.
Copyright (C) 1997-2017 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#ifdef USE_NONOPTION_FLAGS
/* Attention: this file is *not* necessary when the GNU getopt functions
are used outside the GNU libc. Some additional functionality of the
getopt functions in GNU libc require this additional work. */
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <_itoa.h>
/* Variable to synchronize work. */
char *__getopt_nonoption_flags;
/* Remove the environment variable "_<PID>_GNU_nonoption_argv_flags_" if
it is still available. If the getopt functions are also used in the
application it does not exist anymore since it was saved for the use
in getopt. */
void
__getopt_clean_environment (char **env)
{
/* Bash 2.0 puts a special variable in the environment for each
command it runs, specifying which ARGV elements are the results
of file name wildcard expansion and therefore should not be
considered as options. */
static const char envvar_tail[] = "_GNU_nonoption_argv_flags_=";
char var[50];
char *cp, **ep;
size_t len;
/* Construct the "_<PID>_GNU_nonoption_argv_flags_=" string. We must
not use `sprintf'. */
cp = memcpy (&var[sizeof (var) - sizeof (envvar_tail)], envvar_tail,
sizeof (envvar_tail));
cp = _itoa_word (__getpid (), cp, 10, 0);
/* Note: we omit adding the leading '_' since we explicitly test for
it before calling strncmp. */
len = (var + sizeof (var) - 1) - cp;
for (ep = env; *ep != NULL; ++ep)
if ((*ep)[0] == '_'
&& __builtin_expect (strncmp (*ep + 1, cp, len) == 0, 0))
{
/* Found it. Store this pointer and move later ones back. */
char **dp = ep;
__getopt_nonoption_flags = &(*ep)[len];
do
dp[0] = dp[1];
while (*dp++);
/* Continue the loop in case the name appears again. */
}
}
#endif /* USE_NONOPTION_FLAGS */

View File

@ -98,11 +98,6 @@ struct _getopt_data
int __first_nonopt;
int __last_nonopt;
#if defined _LIBC && defined USE_NONOPTION_FLAGS
int __nonoption_flags_max_len;
int __nonoption_flags_len;
# endif
};
/* The initializer is necessary to set OPTIND and OPTERR to their

View File

@ -33,9 +33,6 @@
extern void __mach_init (void);
extern void __init_misc (int, char **, char **);
#ifdef USE_NONOPTION_FLAGS
extern void __getopt_clean_environment (char **);
#endif
extern void __libc_global_ctors (void);
unsigned int __hurd_threadvar_max;
@ -88,11 +85,6 @@ posixland_init (int argc, char **argv, char **envp)
#endif
__init_misc (argc, argv, envp);
#ifdef USE_NONOPTION_FLAGS
/* This is a hack to make the special getopt in GNU libc working. */
__getopt_clean_environment (envp);
#endif
/* Initialize ctype data. */
__ctype_init ();