Update.
[glibc.git] / posix / getopt_init.c
blob6619f2522b6262e718f8a2a04eb018d71e6aeaf8
1 /* Perform additional initialization for getopt functions in GNU libc.
2 Copyright (C) 1997 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;
35 extern pid_t __libc_pid;
38 /* Remove the environment variable "_<PID>_GNU_nonoption_argv_flags_" if
39 it is still available. If the getopt functions are also used in the
40 application it does not exist anymore since it was saved for the use
41 in getopt. */
42 void
43 __getopt_clean_environment (char **env)
45 /* Bash 2.0 puts a special variable in the environment for each
46 command it runs, specifying which ARGV elements are the results
47 of file name wildcard expansion and therefore should not be
48 considered as options. */
49 static const char envvar_tail[] = "_GNU_nonoption_argv_flags_=";
50 char var[100];
51 char *cp, **ep;
52 size_t len;
54 /* Generate name of the environment variable. We must know the PID
55 and we must not use `sprintf'. */
56 if (__libc_pid == 0xf00baa)
57 __libc_pid = getpid ();
59 /* Construct "_<PID>_GNU_nonoption_argv_flags_=" string. */
60 cp = memcpy (&var[sizeof (var) - sizeof (envvar_tail)], envvar_tail,
61 sizeof (envvar_tail));
62 cp = _itoa_word (__libc_pid, cp, 10, 0);
63 *--cp = '_';
64 len = (var + sizeof (var) - 1) - cp;
66 for (ep = env; *ep != NULL; ++ep)
67 if (!strncmp (*ep, cp, len))
69 /* Found it. Store this pointer and move later ones back. */
70 char **dp = ep;
71 __getopt_nonoption_flags = &(*ep)[len];
73 dp[0] = dp[1];
74 while (*dp++);
75 /* Continue the loop in case the name appears again. */