powerpc: Fix return code of strcasecmp for unaligned inputs
[glibc.git] / string / envz.c
blob68343f3d45dd89f9466caf2fb4eab9235a30fe5a
1 /* Routines for dealing with '\0' separated environment vectors
2 Copyright (C) 1995-2016 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <malloc.h>
21 #include <string.h>
23 #include <envz.h>
25 /* The character separating names from values in an envz. */
26 #define SEP '='
28 /* Returns a pointer to the entry in ENVZ for NAME, or 0 if there is none.
29 If NAME contains the separator character, only the portion before it is
30 used in the comparison. */
31 char *
32 envz_entry (const char *envz, size_t envz_len, const char *name)
34 while (envz_len)
36 const char *p = name;
37 const char *entry = envz; /* Start of this entry. */
39 /* See how far NAME and ENTRY match. */
40 while (envz_len && *p == *envz && *p && *p != SEP)
41 p++, envz++, envz_len--;
43 if ((*envz == '\0' || *envz == SEP) && (*p == '\0' || *p == SEP))
44 /* Bingo! */
45 return (char *) entry;
47 /* No match, skip to the next entry. */
48 while (envz_len && *envz)
49 envz++, envz_len--;
50 if (envz_len)
51 envz++, envz_len--; /* skip '\0' */
54 return 0;
56 libc_hidden_def (envz_entry)
58 /* Returns a pointer to the value portion of the entry in ENVZ for NAME, or 0
59 if there is none. */
60 char *
61 envz_get (const char *envz, size_t envz_len, const char *name)
63 char *entry = envz_entry (envz, envz_len, name);
64 if (entry)
66 while (*entry && *entry != SEP)
67 entry++;
68 if (*entry)
69 entry++;
70 else
71 entry = 0; /* A null entry. */
73 return entry;
76 /* Remove the entry for NAME from ENVZ & ENVZ_LEN, if any. */
77 void
78 envz_remove (char **envz, size_t *envz_len, const char *name)
80 char *entry = envz_entry (*envz, *envz_len, name);
81 if (entry)
82 argz_delete (envz, envz_len, entry);
84 libc_hidden_def (envz_remove)
86 /* Adds an entry for NAME with value VALUE to ENVZ & ENVZ_LEN. If an entry
87 with the same name already exists in ENVZ, it is removed. If VALUE is
88 NULL, then the new entry will a special null one, for which envz_get will
89 return NULL, although envz_entry will still return an entry; this is handy
90 because when merging with another envz, the null entry can override an
91 entry in the other one. Null entries can be removed with envz_strip (). */
92 error_t
93 envz_add (char **envz, size_t *envz_len, const char *name, const char *value)
95 envz_remove (envz, envz_len, name);
97 if (value)
98 /* Add the new value, if there is one. */
100 size_t name_len = strlen (name);
101 size_t value_len = strlen (value);
102 size_t old_envz_len = *envz_len;
103 size_t new_envz_len = old_envz_len + name_len + 1 + value_len + 1;
104 char *new_envz = realloc (*envz, new_envz_len);
106 if (new_envz)
108 memcpy (new_envz + old_envz_len, name, name_len);
109 new_envz[old_envz_len + name_len] = SEP;
110 memcpy (new_envz + old_envz_len + name_len + 1, value, value_len);
111 new_envz[new_envz_len - 1] = 0;
113 *envz = new_envz;
114 *envz_len = new_envz_len;
116 return 0;
118 else
119 return ENOMEM;
121 else
122 /* Add a null entry. */
123 return __argz_add (envz, envz_len, name);
126 /* Adds each entry in ENVZ2 to ENVZ & ENVZ_LEN, as if with envz_add(). If
127 OVERRIDE is true, then values in ENVZ2 will supersede those with the same
128 name in ENV, otherwise not. */
129 error_t
130 envz_merge (char **envz, size_t *envz_len, const char *envz2,
131 size_t envz2_len, int override)
133 error_t err = 0;
135 while (envz2_len && ! err)
137 char *old = envz_entry (*envz, *envz_len, envz2);
138 size_t new_len = strlen (envz2) + 1;
140 if (! old)
141 err = __argz_append (envz, envz_len, envz2, new_len);
142 else if (override)
144 argz_delete (envz, envz_len, old);
145 err = __argz_append (envz, envz_len, envz2, new_len);
148 envz2 += new_len;
149 envz2_len -= new_len;
152 return err;
155 /* Remove null entries. */
156 void
157 envz_strip (char **envz, size_t *envz_len)
159 char *entry = *envz;
160 size_t left = *envz_len;
161 while (left)
163 size_t entry_len = strlen (entry) + 1;
164 left -= entry_len;
165 if (! strchr (entry, SEP))
166 /* Null entry. */
167 memmove (entry, entry + entry_len, left);
168 else
169 entry += entry_len;
171 *envz_len = entry - *envz;