Back to index

glibc  2.9
Defines | Functions
envz.c File Reference
#include <malloc.h>
#include <string.h>
#include <envz.h>

Go to the source code of this file.

Defines

#define SEP   '='

Functions

char * envz_entry (const char *envz, size_t envz_len, const char *name)
 libc_hidden_def (envz_entry)
void envz_remove (char **envz, size_t *envz_len, const char *name)
 libc_hidden_def (envz_remove)
error_t envz_merge (char **envz, size_t *envz_len, const char *envz2, size_t envz2_len, int override)
void envz_strip (char **envz, size_t *envz_len)

Define Documentation

#define SEP   '='

Definition at line 27 of file envz.c.


Function Documentation

char* envz_entry ( const char *  envz,
size_t  envz_len,
const char *  name 
)

Definition at line 33 of file envz.c.

{
  while (envz_len)
    {
      const char *p = name;
      const char *entry = envz;    /* Start of this entry. */

      /* See how far NAME and ENTRY match.  */
      while (envz_len && *p == *envz && *p && *p != SEP)
       p++, envz++, envz_len--;

      if ((*envz == '\0' || *envz == SEP) && (*p == '\0' || *p == SEP))
       /* Bingo! */
       return (char *) entry;

      /* No match, skip to the next entry.  */
      while (envz_len && *envz)
       envz++, envz_len--;
      if (envz_len)
       envz++, envz_len--;  /* skip '\0' */
    }

  return 0;
}

Here is the caller graph for this function:

error_t envz_merge ( char **  envz,
size_t envz_len,
const char *  envz2,
size_t  envz2_len,
int  override 
)

Definition at line 131 of file envz.c.

{
  error_t err = 0;

  while (envz2_len && ! err)
    {
      char *old = envz_entry (*envz, *envz_len, envz2);
      size_t new_len = strlen (envz2) + 1;

      if (! old)
       err = __argz_append (envz, envz_len, envz2, new_len);
      else if (override)
       {
         argz_delete (envz, envz_len, old);
         err = __argz_append (envz, envz_len, envz2, new_len);
       }

      envz2 += new_len;
      envz2_len -= new_len;
    }

  return err;
}

Here is the call graph for this function:

void envz_remove ( char **  envz,
size_t envz_len,
const char *  name 
)

Definition at line 79 of file envz.c.

{
  char *entry = envz_entry (*envz, *envz_len, name);
  if (entry)
    argz_delete (envz, envz_len, entry);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void envz_strip ( char **  envz,
size_t envz_len 
)

Definition at line 158 of file envz.c.

{
  char *entry = *envz;
  size_t left = *envz_len;
  while (left)
    {
      size_t entry_len = strlen (entry) + 1;
      left -= entry_len;
      if (! strchr (entry, SEP))
       /* Null entry. */
       memmove (entry, entry + entry_len, left);
      else
       entry += entry_len;
    }
  *envz_len = entry - *envz;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 57 of file envz.c.

{
  char *entry = envz_entry (envz, envz_len, name);
  if (entry)
    {
      while (*entry && *entry != SEP)
       entry++;
      if (*entry)
       entry++;
      else
       entry = 0;           /* A null entry.  */
    }
  return entry;
}

Here is the call graph for this function:

Definition at line 85 of file envz.c.

{
  envz_remove (envz, envz_len, name);

  if (value)
    /* Add the new value, if there is one.  */
    {
      size_t name_len = strlen (name);
      size_t value_len = strlen (value);
      size_t old_envz_len = *envz_len;
      size_t new_envz_len = old_envz_len + name_len + 1 + value_len + 1;
      char *new_envz = realloc (*envz, new_envz_len);

      if (new_envz)
       {
         memcpy (new_envz + old_envz_len, name, name_len);
         new_envz[old_envz_len + name_len] = SEP;
         memcpy (new_envz + old_envz_len + name_len + 1, value, value_len);
         new_envz[new_envz_len - 1] = 0;

         *envz = new_envz;
         *envz_len = new_envz_len;

         return 0;
       }
      else
       return ENOMEM;
    }
  else
    /* Add a null entry.  */
    return __argz_add (envz, envz_len, name);
}

Here is the call graph for this function: