1 /* Copyright (C) 1991,92,94,96,98,99,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #define __environ environ
30 /* Return the value of the environment variable NAME. This implementation
31 is tuned a bit in that it assumes no environment variable has an empty
32 name which of course should always be true. We have a special case for
33 one character names so that for the general case we can assume at least
34 two characters which we can access. By doing this we can avoid using the
35 `strncmp' most of the time. */
40 size_t len
= strlen (name
);
44 if (__environ
== NULL
|| name
[0] == '\0')
49 /* The name of the variable consists of only one character. Therefore
50 the first two characters of the environment entry are this character
51 and a '=' character. */
52 #if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned
53 name_start
= ('=' << 8) | *(const unsigned char *) name
;
55 # if __BYTE_ORDER == __BIG_ENDIAN
56 name_start
= '=' | ((*(const unsigned char *) name
) << 8);
58 #error "Funny byte order."
61 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
63 #if _STRING_ARCH_unaligned
64 uint16_t ep_start
= *(uint16_t *) *ep
;
66 uint16_t ep_start
= (((unsigned char *) *ep
)[0]
67 | (((unsigned char *) *ep
)[1] << 8));
69 if (name_start
== ep_start
)
75 #if _STRING_ARCH_unaligned
76 name_start
= *(const uint16_t *) name
;
78 name_start
= (((const unsigned char *) name
)[0]
79 | (((const unsigned char *) name
)[1] << 8));
84 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
86 #if _STRING_ARCH_unaligned
87 uint16_t ep_start
= *(uint16_t *) *ep
;
89 uint16_t ep_start
= (((unsigned char *) *ep
)[0]
90 | (((unsigned char *) *ep
)[1] << 8));
93 if (name_start
== ep_start
&& !strncmp (*ep
+ 2, name
, len
)
94 && (*ep
)[len
+ 2] == '=')
95 return &(*ep
)[len
+ 3];
101 libc_hidden_def (getenv
)