]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/getenv.c
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / lib / sh / getenv.c
1 /* getenv.c - get environment variable value from the shell's variable
2 list. */
3
4 /* Copyright (C) 1997 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
21
22 #include <config.h>
23
24 #if defined (CAN_REDEFINE_GETENV)
25
26 #if defined (HAVE_UNISTD_H)
27 # include <unistd.h>
28 #endif
29
30 #include <bashansi.h>
31 #include <shell.h>
32
33 extern char **environ;
34
35 /* We supply our own version of getenv () because we want library
36 routines to get the changed values of exported variables. */
37
38 /* The NeXT C library has getenv () defined and used in the same file.
39 This screws our scheme. However, Bash will run on the NeXT using
40 the C library getenv (), since right now the only environment variable
41 that we care about is HOME, and that is already defined. */
42 static char *last_tempenv_value = (char *)NULL;
43
44 char *
45 getenv (name)
46 #if defined (__linux__) || defined (__bsdi__) || defined (convex)
47 const char *name;
48 #else
49 char const *name;
50 #endif /* !__linux__ && !__bsdi__ && !convex */
51 {
52 SHELL_VAR *var;
53
54 var = find_tempenv_variable ((char *)name);
55 if (var)
56 {
57 FREE (last_tempenv_value);
58
59 last_tempenv_value = value_cell (var) ? savestring (value_cell (var)) : (char *)NULL;
60 dispose_variable (var);
61 return (last_tempenv_value);
62 }
63 else if (shell_variables)
64 {
65 var = find_variable ((char *)name);
66 if (var && exported_p (var))
67 return (value_cell (var));
68 }
69 else
70 {
71 register int i, len;
72
73 /* In some cases, s5r3 invokes getenv() before main(); BSD systems
74 using gprof also exhibit this behavior. This means that
75 shell_variables will be 0 when this is invoked. We look up the
76 variable in the real environment in that case. */
77
78 for (i = 0, len = strlen (name); environ[i]; i++)
79 {
80 if ((STREQN (environ[i], name, len)) && (environ[i][len] == '='))
81 return (environ[i] + len + 1);
82 }
83 }
84
85 return ((char *)NULL);
86 }
87
88 /* Some versions of Unix use _getenv instead. */
89 char *
90 _getenv (name)
91 #if defined (__linux__) || defined (__bsdi__) || defined (convex)
92 const char *name;
93 #else
94 char const *name;
95 #endif /* !__linux__ && !__bsdi__ && !convex */
96 {
97 return (getenv (name));
98 }
99 #endif /* CAN_REDEFINE_GETENV */