Back to index

cell-binutils  2.17cvs20070401
Defines | Functions | Variables
setenv.c File Reference
#include "ansidecl.h"
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>

Go to the source code of this file.

Defines

#define setenv   libiberty_setenv
#define unsetenv   libiberty_unsetenv
#define __set_errno(ev)   ((errno) = (ev))
#define __environ   environ
#define LOCK
#define UNLOCK

Functions

int setenv (const char *name, const char *value, int replace)
void unsetenv (const char *name)

Variables

int errno
char ** environ
static char ** last_environ

Define Documentation

#define __environ   environ

Definition at line 62 of file setenv.c.

#define __set_errno (   ev)    ((errno) = (ev))

Definition at line 50 of file setenv.c.

#define LOCK

Definition at line 72 of file setenv.c.

#define setenv   libiberty_setenv

Definition at line 39 of file setenv.c.

#define UNLOCK

Definition at line 73 of file setenv.c.

#define unsetenv   libiberty_unsetenv

Definition at line 40 of file setenv.c.


Function Documentation

int setenv ( const char *  name,
const char *  value,
int  replace 
)

Definition at line 83 of file setenv.c.

{
  register char **ep = 0;
  register size_t size;
  const size_t namelen = strlen (name);
  const size_t vallen = strlen (value) + 1;

  LOCK;

  size = 0;
  if (__environ != NULL)
    {
      for (ep = __environ; *ep != NULL; ++ep)
       if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
         break;
       else
         ++size;
    }

  if (__environ == NULL || *ep == NULL)
    {
      char **new_environ;
      if (__environ == last_environ && __environ != NULL)
       /* We allocated this space; we can extend it.  */
       new_environ = (char **) realloc (last_environ,
                                    (size + 2) * sizeof (char *));
      else
       new_environ = (char **) malloc ((size + 2) * sizeof (char *));

      if (new_environ == NULL)
       {
         UNLOCK;
         return -1;
       }

      new_environ[size] = (char *) malloc (namelen + 1 + vallen);
      if (new_environ[size] == NULL)
       {
         free ((char *) new_environ);
         __set_errno (ENOMEM);
         UNLOCK;
         return -1;
       }

      if (__environ != last_environ)
       memcpy ((char *) new_environ, (char *) __environ,
              size * sizeof (char *));

      memcpy (new_environ[size], name, namelen);
      new_environ[size][namelen] = '=';
      memcpy (&new_environ[size][namelen + 1], value, vallen);

      new_environ[size + 1] = NULL;

      last_environ = __environ = new_environ;
    }
  else if (replace)
    {
      size_t len = strlen (*ep);
      if (len + 1 < namelen + 1 + vallen)
       {
         /* The existing string is too short; malloc a new one.  */
         char *new_string = (char *) malloc (namelen + 1 + vallen);
         if (new_string == NULL)
           {
             UNLOCK;
             return -1;
           }
         *ep = new_string;
       }
      memcpy (*ep, name, namelen);
      (*ep)[namelen] = '=';
      memcpy (&(*ep)[namelen + 1], value, vallen);
    }

  UNLOCK;

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unsetenv ( const char *  name)

Definition at line 164 of file setenv.c.

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

  LOCK;

  for (ep = __environ; *ep; ++ep)
    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
      {
       /* Found it.  Remove this pointer by moving later ones back.  */
       char **dp = ep;
       do
         dp[0] = dp[1];
       while (*dp++);
       /* Continue the loop in case NAME appears again.  */
      }

  UNLOCK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char** environ
char** last_environ [static]

Definition at line 79 of file setenv.c.