Add GLIBC_2.9 version tag for libutil.
[glibc/history.git] / stdlib / getenv.c
blob6cdfe2b2662a3653c072bb19da6f9f2a9ae296b2
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
17 02111-1307 USA. */
19 #include <endian.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
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. */
33 char *
34 getenv (name)
35 const char *name;
37 size_t len = strlen (name);
38 char **ep;
39 uint16_t name_start;
41 if (__environ == NULL || name[0] == '\0')
42 return NULL;
44 if (name[1] == '\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;
51 #else
52 # if __BYTE_ORDER == __BIG_ENDIAN
53 name_start = '=' | ((*(const unsigned char *) name) << 8);
54 # else
55 #error "Funny byte order."
56 # endif
57 #endif
58 for (ep = __environ; *ep != NULL; ++ep)
60 #if _STRING_ARCH_unaligned
61 uint16_t ep_start = *(uint16_t *) *ep;
62 #else
63 uint16_t ep_start = (((unsigned char *) *ep)[0]
64 | (((unsigned char *) *ep)[1] << 8));
65 #endif
66 if (name_start == ep_start)
67 return &(*ep)[2];
70 else
72 #if _STRING_ARCH_unaligned
73 name_start = *(const uint16_t *) name;
74 #else
75 name_start = (((const unsigned char *) name)[0]
76 | (((const unsigned char *) name)[1] << 8));
77 #endif
78 len -= 2;
79 name += 2;
81 for (ep = __environ; *ep != NULL; ++ep)
83 #if _STRING_ARCH_unaligned
84 uint16_t ep_start = *(uint16_t *) *ep;
85 #else
86 uint16_t ep_start = (((unsigned char *) *ep)[0]
87 | (((unsigned char *) *ep)[1] << 8));
88 #endif
90 if (name_start == ep_start && !strncmp (*ep + 2, name, len)
91 && (*ep)[len + 2] == '=')
92 return &(*ep)[len + 3];
96 return NULL;
98 libc_hidden_def (getenv)