From 95b50c2be3e92fdea36bf33f52920bea1a87d3a5 Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Sat, 26 Sep 1998 01:42:40 +0000 Subject: Replace the old and extremely icky Mach/NetBSD allocator with a similarly compact and much better one donated by Matt Dillon. Implement a simple sbrk() which uses the existing setheap() api. Remove the custom allocator from the UFS code. It wasn't working quite right, and it shouldn't be needed with the new allocator. Fix a serious problem with changing the value of already-existent environment variables. Don't attempt to modify the supposedly-const argument to putenv() Fix an off-by-one sizing error in the zipfs code detected by the new allocator. Submitted by: zmalloc from Matt Dillon --- lib/libstand/environment.c | 75 +++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 34 deletions(-) (limited to 'lib/libstand/environment.c') diff --git a/lib/libstand/environment.c b/lib/libstand/environment.c index 007b6a2dc87b8..72a9e06e5e807 100644 --- a/lib/libstand/environment.c +++ b/lib/libstand/environment.c @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: environment.c,v 1.1.1.1 1998/08/20 08:19:55 msmith Exp $ * */ @@ -70,7 +70,6 @@ env_setenv(const char *name, int flags, void *value, ev_sethook_t sethook, struct env_var *ev, *curr, *last; if ((ev = env_getenv(name)) != NULL) { - /* * If there's a set hook, let it do the work (unless we are working * for one already. @@ -78,12 +77,45 @@ env_setenv(const char *name, int flags, void *value, ev_sethook_t sethook, if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK)) return(ev->ev_sethook(ev, flags, value)); } else { + + /* + * New variable; create and sort into list + */ ev = malloc(sizeof(struct env_var)); ev->ev_name = strdup(name); ev->ev_value = NULL; /* hooks can only be set when the variable is instantiated */ ev->ev_sethook = sethook; ev->ev_unsethook = unsethook; + + /* Sort into list */ + ev->ev_prev = NULL; + ev->ev_next = NULL; + /* Search for the record to insert before */ + for (last = NULL, curr = environ; + curr != NULL; + last = curr, curr = curr->ev_next) { + + if (strcmp(ev->ev_name, curr->ev_name) < 0) { + if (curr->ev_prev) { + curr->ev_prev->ev_next = ev; + } else { + environ = ev; + } + ev->ev_next = curr; + ev->ev_prev = curr->ev_prev; + curr->ev_prev = ev; + break; + } + } + if (curr == NULL) { + if (last == NULL) { + environ = ev; + } else { + last->ev_next = ev; + ev->ev_prev = last; + } + } } /* If there is data in the variable, discard it */ @@ -100,35 +132,6 @@ env_setenv(const char *name, int flags, void *value, ev_sethook_t sethook, /* Keep the flag components that are relevant */ ev->ev_flags = flags & (EV_DYNAMIC); - /* Sort into list */ - ev->ev_prev = NULL; - ev->ev_next = NULL; - - /* Search for the record to insert before */ - for (last = NULL, curr = environ; - curr != NULL; - last = curr, curr = curr->ev_next) { - - if (strcmp(ev->ev_name, curr->ev_name) < 0) { - if (curr->ev_prev) { - curr->ev_prev->ev_next = ev; - } else { - environ = ev; - } - ev->ev_next = curr; - ev->ev_prev = curr->ev_prev; - curr->ev_prev = ev; - break; - } - } - if (curr == NULL) { - if (last == NULL) { - environ = ev; - } else { - last->ev_next = ev; - ev->ev_prev = last; - } - } return(0); } @@ -158,11 +161,15 @@ setenv(const char *name, char *value, int overwrite) int putenv(const char *string) { - char *value; + char *value, *copy; + int result; - if ((value = strchr(string, '=')) != NULL) + copy = strdup(string); + if ((value = strchr(copy, '=')) != NULL) *(value++) = 0; - return(setenv(string, value, 1)); + result = setenv(copy, value, 1); + free(copy); + return(result); } int -- cgit v1.2.3