1 /* getenv.c - get environment variable value from the shell's variable
4 /* Copyright (C) 1997-2002 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 #if defined (CAN_REDEFINE_GETENV)
26 #if defined (HAVE_UNISTD_H)
38 extern char **environ;
40 /* We supply our own version of getenv () because we want library
41 routines to get the changed values of exported variables. */
43 /* The NeXT C library has getenv () defined and used in the same file.
44 This screws our scheme. However, Bash will run on the NeXT using
45 the C library getenv (), since right now the only environment variable
46 that we care about is HOME, and that is already defined. */
47 static char *last_tempenv_value = (char *)NULL;
55 if (name == 0 || *name == '\0')
56 return ((char *)NULL);
58 var = find_tempenv_variable ((char *)name);
61 FREE (last_tempenv_value);
63 last_tempenv_value = value_cell (var) ? savestring (value_cell (var)) : (char *)NULL;
64 return (last_tempenv_value);
66 else if (shell_variables)
68 var = find_variable ((char *)name);
69 if (var && exported_p (var))
70 return (value_cell (var));
76 /* In some cases, s5r3 invokes getenv() before main(); BSD systems
77 using gprof also exhibit this behavior. This means that
78 shell_variables will be 0 when this is invoked. We look up the
79 variable in the real environment in that case. */
81 for (i = 0, len = strlen (name); environ[i]; i++)
83 if ((STREQN (environ[i], name, len)) && (environ[i][len] == '='))
84 return (environ[i] + len + 1);
88 return ((char *)NULL);
91 /* Some versions of Unix use _getenv instead. */
96 return (getenv (name));
99 /* SUSv3 says argument is a `char *'; BSD implementations disagree */
102 #ifndef HAVE_STD_PUTENV
112 if (str == 0 || *str == '\0')
118 offset = assignment (str, 0);
119 if (str[offset] != '=')
124 name = savestring (str);
127 value = name + offset + 1;
129 /* XXX - should we worry about readonly here? */
130 var = bind_variable (name, value, 0);
137 VUNSETATTR (var, att_invisible);
138 VSETATTR (var, att_exported);
146 #ifndef HAVE_STD_PUTENV
152 return putenv (name);
157 setenv (name, value, rewrite)
165 if (name == 0 || *name == '\0' || strchr (name, '=') != 0)
172 v = (char *)value; /* some compilers need explicit cast */
173 /* XXX - should we worry about readonly here? */
175 var = find_variable (name);
178 var = bind_variable (name, v, 0);
183 VUNSETATTR (var, att_invisible);
184 VSETATTR (var, att_exported);
191 _setenv (name, value, rewrite)
196 return setenv (name, value, rewrite);
200 /* SUSv3 says unsetenv returns int; existing implementations (BSD) disagree. */
202 #ifdef HAVE_STD_UNSETENV
203 #define UNSETENV_RETURN(N) return(N)
204 #define UNSETENV_RETTYPE int
206 #define UNSETENV_RETURN(N) return
207 #define UNSETENV_RETTYPE void
214 if (name == 0 || *name == '\0' || strchr (name, '=') != 0)
220 /* XXX - should we just remove the export attribute here? */
222 unbind_variable (name);
226 v = find_variable (name);
228 VUNSETATTR (v, att_exported);
233 #endif /* CAN_REDEFINE_GETENV */