00001 /* Perform additional initialization for getopt functions in GNU libc. 00002 Copyright (C) 1997, 1998, 2001 Free Software Foundation, Inc. 00003 This file is part of the GNU C Library. 00004 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. 00005 00006 The GNU C Library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 The GNU C Library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with the GNU C Library; if not, write to the Free 00018 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 00019 02111-1307 USA. */ 00020 00021 #ifdef USE_NONOPTION_FLAGS 00022 /* Attention: this file is *not* necessary when the GNU getopt functions 00023 are used outside the GNU libc. Some additional functionality of the 00024 getopt functions in GNU libc require this additional work. */ 00025 00026 #include <getopt.h> 00027 #include <string.h> 00028 #include <unistd.h> 00029 #include <sys/types.h> 00030 00031 #include <stdio-common/_itoa.h> 00032 00033 /* Variable to synchronize work. */ 00034 char *__getopt_nonoption_flags; 00035 00036 00037 /* Remove the environment variable "_<PID>_GNU_nonoption_argv_flags_" if 00038 it is still available. If the getopt functions are also used in the 00039 application it does not exist anymore since it was saved for the use 00040 in getopt. */ 00041 void 00042 __getopt_clean_environment (char **env) 00043 { 00044 /* Bash 2.0 puts a special variable in the environment for each 00045 command it runs, specifying which ARGV elements are the results 00046 of file name wildcard expansion and therefore should not be 00047 considered as options. */ 00048 static const char envvar_tail[] = "_GNU_nonoption_argv_flags_="; 00049 char var[50]; 00050 char *cp, **ep; 00051 size_t len; 00052 00053 /* Construct the "_<PID>_GNU_nonoption_argv_flags_=" string. We must 00054 not use `sprintf'. */ 00055 cp = memcpy (&var[sizeof (var) - sizeof (envvar_tail)], envvar_tail, 00056 sizeof (envvar_tail)); 00057 cp = _itoa_word (__getpid (), cp, 10, 0); 00058 /* Note: we omit adding the leading '_' since we explicitly test for 00059 it before calling strncmp. */ 00060 len = (var + sizeof (var) - 1) - cp; 00061 00062 for (ep = env; *ep != NULL; ++ep) 00063 if ((*ep)[0] == '_' 00064 && __builtin_expect (strncmp (*ep + 1, cp, len) == 0, 0)) 00065 { 00066 /* Found it. Store this pointer and move later ones back. */ 00067 char **dp = ep; 00068 __getopt_nonoption_flags = &(*ep)[len]; 00069 do 00070 dp[0] = dp[1]; 00071 while (*dp++); 00072 /* Continue the loop in case the name appears again. */ 00073 } 00074 } 00075 #endif /* USE_NONOPTION_FLAGS */