1 /* Perform additional initialization for getopt functions in GNU libc.
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef USE_NONOPTION_FLAGS
21 /* Attention: this file is *not* necessary when the GNU getopt functions
22 are used outside the GNU libc. Some additional functionality of the
23 getopt functions in GNU libc require this additional work. */
28 #include <sys/types.h>
32 /* Variable to synchronize work. */
33 char *__getopt_nonoption_flags
;
36 /* Remove the environment variable "_<PID>_GNU_nonoption_argv_flags_" if
37 it is still available. If the getopt functions are also used in the
38 application it does not exist anymore since it was saved for the use
41 __getopt_clean_environment (char **env
)
43 /* Bash 2.0 puts a special variable in the environment for each
44 command it runs, specifying which ARGV elements are the results
45 of file name wildcard expansion and therefore should not be
46 considered as options. */
47 static const char envvar_tail
[] = "_GNU_nonoption_argv_flags_=";
52 /* Construct the "_<PID>_GNU_nonoption_argv_flags_=" string. We must
54 cp
= memcpy (&var
[sizeof (var
) - sizeof (envvar_tail
)], envvar_tail
,
55 sizeof (envvar_tail
));
56 cp
= _itoa_word (__getpid (), cp
, 10, 0);
57 /* Note: we omit adding the leading '_' since we explicitly test for
58 it before calling strncmp. */
59 len
= (var
+ sizeof (var
) - 1) - cp
;
61 for (ep
= env
; *ep
!= NULL
; ++ep
)
63 && __builtin_expect (strncmp (*ep
+ 1, cp
, len
) == 0, 0))
65 /* Found it. Store this pointer and move later ones back. */
67 __getopt_nonoption_flags
= &(*ep
)[len
];
71 /* Continue the loop in case the name appears again. */
74 #endif /* USE_NONOPTION_FLAGS */