Back to index

glibc  2.9
Functions
getenv.c File Reference
#include <endian.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Go to the source code of this file.

Functions

char * getenv (char *name) const

Function Documentation

char* getenv ( char *  name) const

Definition at line 34 of file getenv.c.

{
  size_t len = strlen (name);
  char **ep;
  uint16_t name_start;

  if (__environ == NULL || name[0] == '\0')
    return NULL;

  if (name[1] == '\0')
    {
      /* The name of the variable consists of only one character.  Therefore
        the first two characters of the environment entry are this character
        and a '=' character.  */
#if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned
      name_start = ('=' << 8) | *(const unsigned char *) name;
#else
# if __BYTE_ORDER == __BIG_ENDIAN
      name_start = '=' | ((*(const unsigned char *) name) << 8);
# else
 #error "Funny byte order."
# endif
#endif
      for (ep = __environ; *ep != NULL; ++ep)
       {
#if _STRING_ARCH_unaligned
         uint16_t ep_start = *(uint16_t *) *ep;
#else
         uint16_t ep_start = (((unsigned char *) *ep)[0]
                            | (((unsigned char *) *ep)[1] << 8));
#endif
         if (name_start == ep_start)
           return &(*ep)[2];
       }
    }
  else
    {
#if _STRING_ARCH_unaligned
      name_start = *(const uint16_t *) name;
#else
      name_start = (((const unsigned char *) name)[0]
                  | (((const unsigned char *) name)[1] << 8));
#endif
      len -= 2;
      name += 2;

      for (ep = __environ; *ep != NULL; ++ep)
       {
#if _STRING_ARCH_unaligned
         uint16_t ep_start = *(uint16_t *) *ep;
#else
         uint16_t ep_start = (((unsigned char *) *ep)[0]
                            | (((unsigned char *) *ep)[1] << 8));
#endif

         if (name_start == ep_start && !strncmp (*ep + 2, name, len)
             && (*ep)[len + 2] == '=')
           return &(*ep)[len + 3];
       }
    }

  return NULL;
}

Here is the call graph for this function: