S/390: Fix uc_link == NULL handling for makecontext
[platform/upstream/glibc.git] / posix / getopt_init.c
1 /* Perform additional initialization for getopt functions in GNU libc.
2    Copyright (C) 1997, 1998, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
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.
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    Lesser General Public License for more details.
15
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/>.  */
19
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.  */
24
25 #include <getopt.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29
30 #include <_itoa.h>
31
32 /* Variable to synchronize work.  */
33 char *__getopt_nonoption_flags;
34
35
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
39    in getopt.  */
40 void
41 __getopt_clean_environment (char **env)
42 {
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_=";
48   char var[50];
49   char *cp, **ep;
50   size_t len;
51
52   /* Construct the "_<PID>_GNU_nonoption_argv_flags_=" string.  We must
53      not use `sprintf'.  */
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;
60
61   for (ep = env; *ep != NULL; ++ep)
62     if ((*ep)[0] == '_'
63         && __builtin_expect (strncmp (*ep + 1, cp, len) == 0, 0))
64       {
65         /* Found it.  Store this pointer and move later ones back.  */
66         char **dp = ep;
67         __getopt_nonoption_flags = &(*ep)[len];
68         do
69           dp[0] = dp[1];
70         while (*dp++);
71         /* Continue the loop in case the name appears again.  */
72       }
73 }
74 #endif  /* USE_NONOPTION_FLAGS */