1 /* Copyright (C) 1991,92,94,96,98,99,2002,2005 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 /* Return the value of the environment variable NAME. This implementation
28 is tuned a bit in that it assumes no environment variable has an empty
29 name which of course should always be true. We have a special case for
30 one character names so that for the general case we can assume at least
31 two characters which we can access. By doing this we can avoid using the
32 `strncmp' most of the time. */
37 size_t len
= strlen (name
);
41 if (__environ
== NULL
|| name
[0] == '\0')
46 /* The name of the variable consists of only one character. Therefore
47 the first two characters of the environment entry are this character
48 and a '=' character. */
49 #if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned
50 name_start
= ('=' << 8) | *(const unsigned char *) name
;
52 # if __BYTE_ORDER == __BIG_ENDIAN
53 name_start
= '=' | ((*(const unsigned char *) name
) << 8);
55 #error "Funny byte order."
58 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
60 #if _STRING_ARCH_unaligned
61 uint16_t ep_start
= *(uint16_t *) *ep
;
63 uint16_t ep_start
= (((unsigned char *) *ep
)[0]
64 | (((unsigned char *) *ep
)[1] << 8));
66 if (name_start
== ep_start
)
72 #if _STRING_ARCH_unaligned
73 name_start
= *(const uint16_t *) name
;
75 name_start
= (((const unsigned char *) name
)[0]
76 | (((const unsigned char *) name
)[1] << 8));
81 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
83 #if _STRING_ARCH_unaligned
84 uint16_t ep_start
= *(uint16_t *) *ep
;
86 uint16_t ep_start
= (((unsigned char *) *ep
)[0]
87 | (((unsigned char *) *ep
)[1] << 8));
90 if (name_start
== ep_start
&& !strncmp (*ep
+ 2, name
, len
)
91 && (*ep
)[len
+ 2] == '=')
92 return &(*ep
)[len
+ 3];
98 libc_hidden_def (getenv
)