]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/generic/getenv.c
Update.
[thirdparty/glibc.git] / sysdeps / generic / getenv.c
CommitLineData
31f7410f 1/* Copyright (C) 1991, 1992, 1994, 1996, 1998 Free Software Foundation, Inc.
f65fd747 2 This file is part of the GNU C Library.
28f540f4 3
f65fd747
UD
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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
12 Library General Public License for more details.
28f540f4 13
f65fd747
UD
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 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
26#ifndef HAVE_GNU_LD
27#define __environ environ
28#endif
29
31f7410f
UD
30/* Return the value of the environment variable NAME. This implementation
31 is tuned a bit in that it assumes no environment variable has an empty
32 name which of course should always be true. We have a special case for
33 one character names so that for the general case we can assume at least
34 two characters which we can access. By doing this we can avoid using the
35 `strncmp' most of the time. */
28f540f4 36char *
d68171ed
UD
37getenv (name)
38 const char *name;
28f540f4 39{
d68171ed
UD
40 const size_t len = strlen (name);
41 char **ep;
31f7410f 42 uint16_t name_start;
28f540f4 43
31f7410f 44 if (__environ == NULL || name[0] == '\0')
28f540f4
RM
45 return NULL;
46
31f7410f
UD
47 if (name[1] == '\0')
48 {
49 /* The name of the variable consists of only one character. Therefore
50 the first two characters of the environment entry are this character
51 and a '=' character. */
52#if __BYTE_ORDER == __LITTLE_ENDIAN
53 name_start = ('=' << 8) | *(const unsigned char *) name;
54#else
55# if __BYTE_ORDER == __BIG_ENDIAN
56 name_start = '=' | ((*(const unsigned char *) name) << 8);
57# else
58 #error "Funny byte order."
59# endif
60#endif
61 for (ep = __environ; *ep != NULL; ++ep)
62 {
63#if _STRING_ARCH_unaligned
64 uint16_t ep_start = *(uint16_t *) *ep;
65#else
66 uint16_t ep_start = (((unsigned char *) *ep)[0]
67 | (((unsigned char *) *ep)[1] << 8));
68#endif
69 if (name_start == ep_start)
70 return &(*ep)[2];
71 }
72 }
73 else
74 {
75#if _STRING_ARCH_unaligned
76 name_start = *(const uint16_t *) name;
77#else
78 name_start = (((const unsigned char *) name)[0]
79 | (((const unsigned char *) name)[1] << 8));
80#endif
81 len -= 2;
82 name += 2;
83
84 for (ep = __environ; *ep != NULL; ++ep)
85 {
86#if _STRING_ARCH_unaligned
87 uint16_t ep_start = *(uint16_t *) *ep;
88#else
89 uint16_t ep_start = (((unsigned char *) *ep)[0]
90 | (((unsigned char *) *ep)[1] << 8));
91#endif
92
93 if (name_start == ep_start && !strncmp (*ep + 2, name, len)
94 && (*ep)[len + 2] == '=')
95 return &(*ep)[len + 3];
96 }
97 }
28f540f4
RM
98
99 return NULL;
100}