]> mj.ucw.cz Git - libucw.git/blob - lib/getopt/getopt_init.c
d460098c8a020979b9fd8fd8724c9b19abec81fa
[libucw.git] / lib / getopt / 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, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #ifdef USE_NONOPTION_FLAGS
22 /* Attention: this file is *not* necessary when the GNU getopt functions
23    are used outside the GNU libc.  Some additional functionality of the
24    getopt functions in GNU libc require this additional work.  */
25
26 #include <getopt.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30
31 #include <stdio-common/_itoa.h>
32
33 /* Variable to synchronize work.  */
34 char *__getopt_nonoption_flags;
35
36
37 /* Remove the environment variable "_<PID>_GNU_nonoption_argv_flags_" if
38    it is still available.  If the getopt functions are also used in the
39    application it does not exist anymore since it was saved for the use
40    in getopt.  */
41 void
42 __getopt_clean_environment (char **env)
43 {
44   /* Bash 2.0 puts a special variable in the environment for each
45      command it runs, specifying which ARGV elements are the results
46      of file name wildcard expansion and therefore should not be
47      considered as options.  */
48   static const char envvar_tail[] = "_GNU_nonoption_argv_flags_=";
49   char var[50];
50   char *cp, **ep;
51   size_t len;
52
53   /* Construct the "_<PID>_GNU_nonoption_argv_flags_=" string.  We must
54      not use `sprintf'.  */
55   cp = memcpy (&var[sizeof (var) - sizeof (envvar_tail)], envvar_tail,
56                sizeof (envvar_tail));
57   cp = _itoa_word (__getpid (), cp, 10, 0);
58   /* Note: we omit adding the leading '_' since we explicitly test for
59      it before calling strncmp.  */
60   len = (var + sizeof (var) - 1) - cp;
61
62   for (ep = env; *ep != NULL; ++ep)
63     if ((*ep)[0] == '_'
64         && __builtin_expect (strncmp (*ep + 1, cp, len) == 0, 0))
65       {
66         /* Found it.  Store this pointer and move later ones back.  */
67         char **dp = ep;
68         __getopt_nonoption_flags = &(*ep)[len];
69         do
70           dp[0] = dp[1];
71         while (*dp++);
72         /* Continue the loop in case the name appears again.  */
73       }
74 }
75 #endif  /* USE_NONOPTION_FLAGS */