(Supported Configurations): Add s390-linux.
[glibc.git] / posix / getopt_init.c
blob6fddc8b26f5bb58fbce3314e76b2db6a0d686f7b
1 /* Perform additional initialization for getopt functions in GNU libc.
2 Copyright (C) 1997, 1998 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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. */
25 #include <getopt.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
30 #include <stdio-common/_itoa.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
39 in getopt. */
40 void
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_=";
48 char var[100];
49 char *cp, **ep;
50 size_t len;
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 *--cp = '_';
58 len = (var + sizeof (var) - 1) - cp;
60 for (ep = env; *ep != NULL; ++ep)
61 if (!strncmp (*ep, cp, len))
63 /* Found it. Store this pointer and move later ones back. */
64 char **dp = ep;
65 __getopt_nonoption_flags = &(*ep)[len];
67 dp[0] = dp[1];
68 while (*dp++);
69 /* Continue the loop in case the name appears again. */