Blame stdlib/getenv.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <endian.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Return the value of the environment variable NAME.  This implementation
Packit 6c4009
   is tuned a bit in that it assumes no environment variable has an empty
Packit 6c4009
   name which of course should always be true.  We have a special case for
Packit 6c4009
   one character names so that for the general case we can assume at least
Packit 6c4009
   two characters which we can access.  By doing this we can avoid using the
Packit 6c4009
   `strncmp' most of the time.  */
Packit 6c4009
char *
Packit 6c4009
getenv (const char *name)
Packit 6c4009
{
Packit 6c4009
  size_t len = strlen (name);
Packit 6c4009
  char **ep;
Packit 6c4009
  uint16_t name_start;
Packit 6c4009
Packit 6c4009
  if (__environ == NULL || name[0] == '\0')
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  if (name[1] == '\0')
Packit 6c4009
    {
Packit 6c4009
      /* The name of the variable consists of only one character.  Therefore
Packit 6c4009
	 the first two characters of the environment entry are this character
Packit 6c4009
	 and a '=' character.  */
Packit 6c4009
#if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned
Packit 6c4009
      name_start = ('=' << 8) | *(const unsigned char *) name;
Packit 6c4009
#else
Packit 6c4009
      name_start = '=' | ((*(const unsigned char *) name) << 8);
Packit 6c4009
#endif
Packit 6c4009
      for (ep = __environ; *ep != NULL; ++ep)
Packit 6c4009
	{
Packit 6c4009
#if _STRING_ARCH_unaligned
Packit 6c4009
	  uint16_t ep_start = *(uint16_t *) *ep;
Packit 6c4009
#else
Packit 6c4009
	  uint16_t ep_start = (((unsigned char *) *ep)[0]
Packit 6c4009
			       | (((unsigned char *) *ep)[1] << 8));
Packit 6c4009
#endif
Packit 6c4009
	  if (name_start == ep_start)
Packit 6c4009
	    return &(*ep)[2];
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
#if _STRING_ARCH_unaligned
Packit 6c4009
      name_start = *(const uint16_t *) name;
Packit 6c4009
#else
Packit 6c4009
      name_start = (((const unsigned char *) name)[0]
Packit 6c4009
		    | (((const unsigned char *) name)[1] << 8));
Packit 6c4009
#endif
Packit 6c4009
      len -= 2;
Packit 6c4009
      name += 2;
Packit 6c4009
Packit 6c4009
      for (ep = __environ; *ep != NULL; ++ep)
Packit 6c4009
	{
Packit 6c4009
#if _STRING_ARCH_unaligned
Packit 6c4009
	  uint16_t ep_start = *(uint16_t *) *ep;
Packit 6c4009
#else
Packit 6c4009
	  uint16_t ep_start = (((unsigned char *) *ep)[0]
Packit 6c4009
			       | (((unsigned char *) *ep)[1] << 8));
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
	  if (name_start == ep_start && !strncmp (*ep + 2, name, len)
Packit 6c4009
	      && (*ep)[len + 2] == '=')
Packit 6c4009
	    return &(*ep)[len + 3];
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (getenv)