1 | /* Copyright (C) 1991-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
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. |
8 | |
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. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <endian.h> |
19 | #include <errno.h> |
20 | #include <stdint.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <unistd.h> |
24 | |
25 | |
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. */ |
32 | char * |
33 | getenv (const char *name) |
34 | { |
35 | char **ep; |
36 | uint16_t name_start; |
37 | |
38 | if (__environ == NULL || name[0] == '\0') |
39 | return NULL; |
40 | |
41 | if (name[1] == '\0') |
42 | { |
43 | /* The name of the variable consists of only one character. Therefore |
44 | the first two characters of the environment entry are this character |
45 | and a '=' character. */ |
46 | #if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned |
47 | name_start = ('=' << 8) | *(const unsigned char *) name; |
48 | #else |
49 | name_start = '=' | ((*(const unsigned char *) name) << 8); |
50 | #endif |
51 | for (ep = __environ; *ep != NULL; ++ep) |
52 | { |
53 | #if _STRING_ARCH_unaligned |
54 | uint16_t ep_start = *(uint16_t *) *ep; |
55 | #else |
56 | uint16_t ep_start = (((unsigned char *) *ep)[0] |
57 | | (((unsigned char *) *ep)[1] << 8)); |
58 | #endif |
59 | if (name_start == ep_start) |
60 | return &(*ep)[2]; |
61 | } |
62 | } |
63 | else |
64 | { |
65 | size_t len = strlen (name); |
66 | #if _STRING_ARCH_unaligned |
67 | name_start = *(const uint16_t *) name; |
68 | #else |
69 | name_start = (((const unsigned char *) name)[0] |
70 | | (((const unsigned char *) name)[1] << 8)); |
71 | #endif |
72 | len -= 2; |
73 | name += 2; |
74 | |
75 | for (ep = __environ; *ep != NULL; ++ep) |
76 | { |
77 | #if _STRING_ARCH_unaligned |
78 | uint16_t ep_start = *(uint16_t *) *ep; |
79 | #else |
80 | uint16_t ep_start = (((unsigned char *) *ep)[0] |
81 | | (((unsigned char *) *ep)[1] << 8)); |
82 | #endif |
83 | |
84 | if (name_start == ep_start && !strncmp (*ep + 2, name, len) |
85 | && (*ep)[len + 2] == '=') |
86 | return &(*ep)[len + 3]; |
87 | } |
88 | } |
89 | |
90 | return NULL; |
91 | } |
92 | libc_hidden_def (getenv) |
93 | |