summaryrefslogtreecommitdiffstats
path: root/ctdb/lib/replace/replace.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2007-05-30 18:06:57 +1000
committerAndrew Tridgell <tridge@samba.org>2007-05-30 18:06:57 +1000
commitcc87648933ab85140b1bff2853a61425e8887f11 (patch)
treea14acd3e23bdc8682add4440ec9c9690f8bbaec3 /ctdb/lib/replace/replace.c
parent89ed660067d0226201fef5172d4af4fada400210 (diff)
downloadsamba-cc87648933ab85140b1bff2853a61425e8887f11.tar.gz
samba-cc87648933ab85140b1bff2853a61425e8887f11.tar.xz
samba-cc87648933ab85140b1bff2853a61425e8887f11.zip
merge lib/replace from samba4
(This used to be ctdb commit d1a0bcbe331b05ad7a3f5f7a33e4c9b5242c43f2)
Diffstat (limited to 'ctdb/lib/replace/replace.c')
-rw-r--r--ctdb/lib/replace/replace.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/ctdb/lib/replace/replace.c b/ctdb/lib/replace/replace.c
index db299130e5..87e73d001c 100644
--- a/ctdb/lib/replace/replace.c
+++ b/ctdb/lib/replace/replace.c
@@ -568,20 +568,24 @@ int rep_unsetenv(const char *name)
{
extern char **environ;
size_t len = strlen(name);
- size_t i;
- int found = 0;
+ size_t i, count;
- for (i=0; (environ && environ[i]); i++) {
- if (found) {
- environ[i-1] = environ[i];
- continue;
- }
+ if (environ == NULL || getenv(name) == NULL) {
+ return 0;
+ }
+ for (i=0;environ[i];i++) /* noop */ ;
+
+ count=i;
+
+ for (i=0;i<count;) {
if (strncmp(environ[i], name, len) == 0 && environ[i][len] == '=') {
- free(environ[i]);
- environ[i] = NULL;
- found = 1;
- continue;
+ /* note: we do _not_ free the old variable here. It is unsafe to
+ do so, as the pointer may not have come from malloc */
+ memmove(&environ[i], &environ[i+1], (count-i)*sizeof(char *));
+ count--;
+ } else {
+ i++;
}
}