]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/getenv.c
1 /* getenv.c - get environment variable value from the shell's variable
4 /* Copyright (C) 1997 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 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
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
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 */
24 #if defined (CAN_REDEFINE_GETENV)
26 #if defined (HAVE_UNISTD_H)
33 extern char **environ
;
35 /* We supply our own version of getenv () because we want library
36 routines to get the changed values of exported variables. */
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
;
46 #if defined (__linux__) || defined (__bsdi__) || defined (convex)
50 #endif /* !__linux__ && !__bsdi__ && !convex */
54 var
= find_tempenv_variable ((char *)name
);
57 FREE (last_tempenv_value
);
59 last_tempenv_value
= value_cell (var
) ? savestring (value_cell (var
)) : (char *)NULL
;
60 dispose_variable (var
);
61 return (last_tempenv_value
);
63 else if (shell_variables
)
65 var
= find_variable ((char *)name
);
66 if (var
&& exported_p (var
))
67 return (value_cell (var
));
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. */
78 for (i
= 0, len
= strlen (name
); environ
[i
]; i
++)
80 if ((STREQN (environ
[i
], name
, len
)) && (environ
[i
][len
] == '='))
81 return (environ
[i
] + len
+ 1);
85 return ((char *)NULL
);
88 /* Some versions of Unix use _getenv instead. */
91 #if defined (__linux__) || defined (__bsdi__) || defined (convex)
95 #endif /* !__linux__ && !__bsdi__ && !convex */
97 return (getenv (name
));
99 #endif /* CAN_REDEFINE_GETENV */