]>
Commit | Line | Data |
---|---|---|
57d44131 | 1 | /* Copyright (C) 1991,92,94,96,98,99,2002,2005 Free Software Foundation, Inc. |
f65fd747 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
f65fd747 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
f65fd747 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 AJ |
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. */ | |
28f540f4 | 18 | |
31f7410f | 19 | #include <endian.h> |
28f540f4 | 20 | #include <errno.h> |
31f7410f | 21 | #include <stdint.h> |
28f540f4 RM |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | ||
28f540f4 | 26 | |
31f7410f UD |
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. */ | |
28f540f4 | 33 | char * |
d68171ed UD |
34 | getenv (name) |
35 | const char *name; | |
28f540f4 | 36 | { |
ecdc196c | 37 | size_t len = strlen (name); |
d68171ed | 38 | char **ep; |
31f7410f | 39 | uint16_t name_start; |
28f540f4 | 40 | |
31f7410f | 41 | if (__environ == NULL || name[0] == '\0') |
28f540f4 RM |
42 | return NULL; |
43 | ||
31f7410f UD |
44 | if (name[1] == '\0') |
45 | { | |
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. */ | |
ec6275c0 | 49 | #if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned |
31f7410f UD |
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) | |
59 | { | |
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]; | |
68 | } | |
69 | } | |
70 | else | |
71 | { | |
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; | |
80 | ||
81 | for (ep = __environ; *ep != NULL; ++ep) | |
82 | { | |
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 | |
89 | ||
90 | if (name_start == ep_start && !strncmp (*ep + 2, name, len) | |
91 | && (*ep)[len + 2] == '=') | |
92 | return &(*ep)[len + 3]; | |
93 | } | |
94 | } | |
28f540f4 RM |
95 | |
96 | return NULL; | |
97 | } | |
a757607d | 98 | libc_hidden_def (getenv) |