Back to index

glibc  2.9
envz.c
Go to the documentation of this file.
00001 /* Routines for dealing with '\0' separated environment vectors
00002    Copyright (C) 1995-1998,2001,2002,2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Miles Bader <miles@gnu.org>
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <malloc.h>
00022 #include <string.h>
00023 
00024 #include <envz.h>
00025 
00026 /* The character separating names from values in an envz.  */
00027 #define SEP '='
00028 
00029 /* Returns a pointer to the entry in ENVZ for NAME, or 0 if there is none.
00030    If NAME contains the separator character, only the portion before it is
00031    used in the comparison.  */
00032 char *
00033 envz_entry (const char *envz, size_t envz_len, const char *name)
00034 {
00035   while (envz_len)
00036     {
00037       const char *p = name;
00038       const char *entry = envz;    /* Start of this entry. */
00039 
00040       /* See how far NAME and ENTRY match.  */
00041       while (envz_len && *p == *envz && *p && *p != SEP)
00042        p++, envz++, envz_len--;
00043 
00044       if ((*envz == '\0' || *envz == SEP) && (*p == '\0' || *p == SEP))
00045        /* Bingo! */
00046        return (char *) entry;
00047 
00048       /* No match, skip to the next entry.  */
00049       while (envz_len && *envz)
00050        envz++, envz_len--;
00051       if (envz_len)
00052        envz++, envz_len--;  /* skip '\0' */
00053     }
00054 
00055   return 0;
00056 }
00057 libc_hidden_def (envz_entry)
00058 
00059 /* Returns a pointer to the value portion of the entry in ENVZ for NAME, or 0
00060    if there is none.  */
00061 char *
00062 envz_get (const char *envz, size_t envz_len, const char *name)
00063 {
00064   char *entry = envz_entry (envz, envz_len, name);
00065   if (entry)
00066     {
00067       while (*entry && *entry != SEP)
00068        entry++;
00069       if (*entry)
00070        entry++;
00071       else
00072        entry = 0;           /* A null entry.  */
00073     }
00074   return entry;
00075 }
00076 
00077 /* Remove the entry for NAME from ENVZ & ENVZ_LEN, if any.  */
00078 void
00079 envz_remove (char **envz, size_t *envz_len, const char *name)
00080 {
00081   char *entry = envz_entry (*envz, *envz_len, name);
00082   if (entry)
00083     argz_delete (envz, envz_len, entry);
00084 }
00085 libc_hidden_def (envz_remove)
00086 
00087 /* Adds an entry for NAME with value VALUE to ENVZ & ENVZ_LEN.  If an entry
00088    with the same name already exists in ENVZ, it is removed.  If VALUE is
00089    NULL, then the new entry will a special null one, for which envz_get will
00090    return NULL, although envz_entry will still return an entry; this is handy
00091    because when merging with another envz, the null entry can override an
00092    entry in the other one.  Null entries can be removed with envz_strip ().  */
00093 error_t
00094 envz_add (char **envz, size_t *envz_len, const char *name, const char *value)
00095 {
00096   envz_remove (envz, envz_len, name);
00097 
00098   if (value)
00099     /* Add the new value, if there is one.  */
00100     {
00101       size_t name_len = strlen (name);
00102       size_t value_len = strlen (value);
00103       size_t old_envz_len = *envz_len;
00104       size_t new_envz_len = old_envz_len + name_len + 1 + value_len + 1;
00105       char *new_envz = realloc (*envz, new_envz_len);
00106 
00107       if (new_envz)
00108        {
00109          memcpy (new_envz + old_envz_len, name, name_len);
00110          new_envz[old_envz_len + name_len] = SEP;
00111          memcpy (new_envz + old_envz_len + name_len + 1, value, value_len);
00112          new_envz[new_envz_len - 1] = 0;
00113 
00114          *envz = new_envz;
00115          *envz_len = new_envz_len;
00116 
00117          return 0;
00118        }
00119       else
00120        return ENOMEM;
00121     }
00122   else
00123     /* Add a null entry.  */
00124     return __argz_add (envz, envz_len, name);
00125 }
00126 
00127 /* Adds each entry in ENVZ2 to ENVZ & ENVZ_LEN, as if with envz_add().  If
00128    OVERRIDE is true, then values in ENVZ2 will supersede those with the same
00129    name in ENV, otherwise not.  */
00130 error_t
00131 envz_merge (char **envz, size_t *envz_len, const char *envz2,
00132            size_t envz2_len, int override)
00133 {
00134   error_t err = 0;
00135 
00136   while (envz2_len && ! err)
00137     {
00138       char *old = envz_entry (*envz, *envz_len, envz2);
00139       size_t new_len = strlen (envz2) + 1;
00140 
00141       if (! old)
00142        err = __argz_append (envz, envz_len, envz2, new_len);
00143       else if (override)
00144        {
00145          argz_delete (envz, envz_len, old);
00146          err = __argz_append (envz, envz_len, envz2, new_len);
00147        }
00148 
00149       envz2 += new_len;
00150       envz2_len -= new_len;
00151     }
00152 
00153   return err;
00154 }
00155 
00156 /* Remove null entries.  */
00157 void
00158 envz_strip (char **envz, size_t *envz_len)
00159 {
00160   char *entry = *envz;
00161   size_t left = *envz_len;
00162   while (left)
00163     {
00164       size_t entry_len = strlen (entry) + 1;
00165       left -= entry_len;
00166       if (! strchr (entry, SEP))
00167        /* Null entry. */
00168        memmove (entry, entry + entry_len, left);
00169       else
00170        entry += entry_len;
00171     }
00172   *envz_len = entry - *envz;
00173 }