(__free_tcb): Free pd->tpp structure.
[glibc/history.git] / string / envz.c
bloba9d420212fc507f1aceae2f44e26fdecda42ce4c
1 /* Routines for dealing with '\0' separated environment vectors
2 Copyright (C) 1995-1998,2001,2002,2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.org>
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <malloc.h>
22 #include <string.h>
24 #include <envz.h>
26 /* The character separating names from values in an envz. */
27 #define SEP '='
29 /* Returns a pointer to the entry in ENVZ for NAME, or 0 if there is none.
30 If NAME contains the separator character, only the portion before it is
31 used in the comparison. */
32 char *
33 envz_entry (const char *envz, size_t envz_len, const char *name)
35 while (envz_len)
37 const char *p = name;
38 const char *entry = envz; /* Start of this entry. */
40 /* See how far NAME and ENTRY match. */
41 while (envz_len && *p == *envz && *p && *p != SEP)
42 p++, envz++, envz_len--;
44 if ((*envz == '\0' || *envz == SEP) && (*p == '\0' || *p == SEP))
45 /* Bingo! */
46 return (char *) entry;
48 /* No match, skip to the next entry. */
49 while (envz_len && *envz)
50 envz++, envz_len--;
51 if (envz_len)
52 envz++, envz_len--; /* skip '\0' */
55 return 0;
57 libc_hidden_def (envz_entry)
59 /* Returns a pointer to the value portion of the entry in ENVZ for NAME, or 0
60 if there is none. */
61 char *
62 envz_get (const char *envz, size_t envz_len, const char *name)
64 char *entry = envz_entry (envz, envz_len, name);
65 if (entry)
67 while (*entry && *entry != SEP)
68 entry++;
69 if (*entry)
70 entry++;
71 else
72 entry = 0; /* A null entry. */
74 return entry;
77 /* Remove the entry for NAME from ENVZ & ENVZ_LEN, if any. */
78 void
79 envz_remove (char **envz, size_t *envz_len, const char *name)
81 char *entry = envz_entry (*envz, *envz_len, name);
82 if (entry)
83 argz_delete (envz, envz_len, entry);
85 libc_hidden_def (envz_remove)
87 /* Adds an entry for NAME with value VALUE to ENVZ & ENVZ_LEN. If an entry
88 with the same name already exists in ENVZ, it is removed. If VALUE is
89 NULL, then the new entry will a special null one, for which envz_get will
90 return NULL, although envz_entry will still return an entry; this is handy
91 because when merging with another envz, the null entry can override an
92 entry in the other one. Null entries can be removed with envz_strip (). */
93 error_t
94 envz_add (char **envz, size_t *envz_len, const char *name, const char *value)
96 envz_remove (envz, envz_len, name);
98 if (value)
99 /* Add the new value, if there is one. */
101 size_t name_len = strlen (name);
102 size_t value_len = strlen (value);
103 size_t old_envz_len = *envz_len;
104 size_t new_envz_len = old_envz_len + name_len + 1 + value_len + 1;
105 char *new_envz = realloc (*envz, new_envz_len);
107 if (new_envz)
109 memcpy (new_envz + old_envz_len, name, name_len);
110 new_envz[old_envz_len + name_len] = SEP;
111 memcpy (new_envz + old_envz_len + name_len + 1, value, value_len);
112 new_envz[new_envz_len - 1] = 0;
114 *envz = new_envz;
115 *envz_len = new_envz_len;
117 return 0;
119 else
120 return ENOMEM;
122 else
123 /* Add a null entry. */
124 return __argz_add (envz, envz_len, name);
127 /* Adds each entry in ENVZ2 to ENVZ & ENVZ_LEN, as if with envz_add(). If
128 OVERRIDE is true, then values in ENVZ2 will supersede those with the same
129 name in ENV, otherwise not. */
130 error_t
131 envz_merge (char **envz, size_t *envz_len, const char *envz2,
132 size_t envz2_len, int override)
134 error_t err = 0;
136 while (envz2_len && ! err)
138 char *old = envz_entry (*envz, *envz_len, envz2);
139 size_t new_len = strlen (envz2) + 1;
141 if (! old)
142 err = __argz_append (envz, envz_len, envz2, new_len);
143 else if (override)
145 argz_delete (envz, envz_len, old);
146 err = __argz_append (envz, envz_len, envz2, new_len);
149 envz2 += new_len;
150 envz2_len -= new_len;
153 return err;
156 /* Remove null entries. */
157 void
158 envz_strip (char **envz, size_t *envz_len)
160 char *entry = *envz;
161 size_t left = *envz_len;
162 while (left)
164 size_t entry_len = strlen (entry) + 1;
165 left -= entry_len;
166 if (! strchr (entry, SEP))
167 /* Null entry. */
168 memmove (entry, entry + entry_len, left);
169 else
170 entry += entry_len;
172 *envz_len = entry - *envz;