]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1991-2015 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 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
31f7410f | 18 | #include <endian.h> |
28f540f4 | 19 | #include <errno.h> |
31f7410f | 20 | #include <stdint.h> |
28f540f4 RM |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | ||
28f540f4 | 25 | |
31f7410f UD |
26 | /* Return the value of the environment variable NAME. This implementation |
27 | is tuned a bit in that it assumes no environment variable has an empty | |
28 | name which of course should always be true. We have a special case for | |
29 | one character names so that for the general case we can assume at least | |
30 | two characters which we can access. By doing this we can avoid using the | |
31 | `strncmp' most of the time. */ | |
28f540f4 | 32 | char * |
d68171ed UD |
33 | getenv (name) |
34 | const char *name; | |
28f540f4 | 35 | { |
ecdc196c | 36 | size_t len = strlen (name); |
d68171ed | 37 | char **ep; |
31f7410f | 38 | uint16_t name_start; |
28f540f4 | 39 | |
31f7410f | 40 | if (__environ == NULL || name[0] == '\0') |
28f540f4 RM |
41 | return NULL; |
42 | ||
31f7410f UD |
43 | if (name[1] == '\0') |
44 | { | |
45 | /* The name of the variable consists of only one character. Therefore | |
46 | the first two characters of the environment entry are this character | |
47 | and a '=' character. */ | |
ec6275c0 | 48 | #if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned |
31f7410f UD |
49 | name_start = ('=' << 8) | *(const unsigned char *) name; |
50 | #else | |
31f7410f | 51 | name_start = '=' | ((*(const unsigned char *) name) << 8); |
31f7410f UD |
52 | #endif |
53 | for (ep = __environ; *ep != NULL; ++ep) | |
54 | { | |
55 | #if _STRING_ARCH_unaligned | |
56 | uint16_t ep_start = *(uint16_t *) *ep; | |
57 | #else | |
58 | uint16_t ep_start = (((unsigned char *) *ep)[0] | |
59 | | (((unsigned char *) *ep)[1] << 8)); | |
60 | #endif | |
61 | if (name_start == ep_start) | |
62 | return &(*ep)[2]; | |
63 | } | |
64 | } | |
65 | else | |
66 | { | |
67 | #if _STRING_ARCH_unaligned | |
68 | name_start = *(const uint16_t *) name; | |
69 | #else | |
70 | name_start = (((const unsigned char *) name)[0] | |
71 | | (((const unsigned char *) name)[1] << 8)); | |
72 | #endif | |
73 | len -= 2; | |
74 | name += 2; | |
75 | ||
76 | for (ep = __environ; *ep != NULL; ++ep) | |
77 | { | |
78 | #if _STRING_ARCH_unaligned | |
79 | uint16_t ep_start = *(uint16_t *) *ep; | |
80 | #else | |
81 | uint16_t ep_start = (((unsigned char *) *ep)[0] | |
82 | | (((unsigned char *) *ep)[1] << 8)); | |
83 | #endif | |
84 | ||
85 | if (name_start == ep_start && !strncmp (*ep + 2, name, len) | |
86 | && (*ep)[len + 2] == '=') | |
87 | return &(*ep)[len + 3]; | |
88 | } | |
89 | } | |
28f540f4 RM |
90 | |
91 | return NULL; | |
92 | } | |
a757607d | 93 | libc_hidden_def (getenv) |