]>
sourceware.org Git - glibc.git/blob - string/envz.c
1 /* Routines for dealing with '\0' separated environment vectors
3 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
5 Written by Miles Bader <miles@gnu.ai.mit.edu>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* The character separating names from values in an envz. */
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. */
33 envz_entry (const char *envz
, size_t envz_len
, const char *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
))
46 return (char *) entry
;
48 /* No match, skip to the next entry. */
49 while (envz_len
&& *envz
)
52 envz
++, envz_len
--; /* skip '\0' */
58 /* Returns a pointer to the value portion of the entry in ENVZ for NAME, or 0
61 envz_get (const char *envz
, size_t envz_len
, const char *name
)
63 char *entry
= envz_entry (envz
, envz_len
, name
);
66 while (*entry
&& *entry
!= SEP
)
71 entry
= 0; /* A null entry. */
76 /* Remove the entry for NAME from ENVZ & ENVZ_LEN, if any. */
78 envz_remove (char **envz
, size_t *envz_len
, const char *name
)
80 char *entry
= envz_entry (*envz
, *envz_len
, name
);
82 argz_delete (envz
, envz_len
, entry
);
85 /* Adds an entry for NAME with value VALUE to ENVZ & ENVZ_LEN. If an entry
86 with the same name already exists in ENVZ, it is removed. If VALUE is
87 NULL, then the new entry will a special null one, for which envz_get will
88 return NULL, although envz_entry will still return an entry; this is handy
89 because when merging with another envz, the null entry can override an
90 entry in the other one. Null entries can be removed with envz_strip (). */
92 envz_add (char **envz
, size_t *envz_len
, const char *name
, const char *value
)
94 envz_remove (envz
, envz_len
, name
);
97 /* Add the new value, if there is one. */
99 size_t name_len
= strlen (name
);
100 size_t value_len
= strlen (value
);
101 size_t old_envz_len
= *envz_len
;
102 size_t new_envz_len
= old_envz_len
+ name_len
+ 1 + value_len
+ 1;
103 char *new_envz
= realloc (*envz
, new_envz_len
);
107 bcopy (name
, new_envz
+ old_envz_len
, name_len
);
108 new_envz
[old_envz_len
+ name_len
] = SEP
;
109 bcopy (value
, new_envz
+ old_envz_len
+ name_len
+ 1, value_len
);
110 new_envz
[new_envz_len
- 1] = 0;
113 *envz_len
= new_envz_len
;
121 /* Add a null entry. */
122 return argz_add (envz
, envz_len
, name
);
125 /* Adds each entry in ENVZ2 to ENVZ & ENVZ_LEN, as if with envz_add(). If
126 OVERRIDE is true, then values in ENVZ2 will supercede those with the same
127 name in ENV, otherwise not. */
129 envz_merge (char **envz
, size_t *envz_len
, const char *envz2
,
130 size_t envz2_len
, int override
)
134 while (envz2_len
&& ! err
)
136 char *old
= envz_entry (*envz
, *envz_len
, envz2
);
137 size_t new_len
= strlen (envz2
) + 1;
140 err
= argz_append (envz
, envz_len
, envz2
, new_len
);
143 argz_delete (envz
, envz_len
, old
);
144 err
= argz_append (envz
, envz_len
, envz2
, new_len
);
148 envz2_len
-= new_len
;
154 /* Remove null entries. */
156 envz_strip (char **envz
, size_t *envz_len
)
159 size_t left
= *envz_len
;
162 size_t entry_len
= strlen (entry
) + 1;
164 if (! index (entry
, SEP
))
166 bcopy (entry
, entry
+ entry_len
, left
);
170 *envz_len
= entry
- *envz
;
This page took 0.039888 seconds and 5 git commands to generate.