From bd345c9bfe5ee5596948681aa23aefc09c89b3f4 Mon Sep 17 00:00:00 2001 From: Eric Allman Date: Sat, 27 May 1995 14:47:09 -0800 Subject: [PATCH 1/1] include putenv() implementation that doesn't depend on setenv() SCCS-vsn: usr.sbin/sendmail/src/conf.h 8.172 SCCS-vsn: usr.sbin/sendmail/src/conf.c 8.177 --- usr/src/usr.sbin/sendmail/src/conf.c | 75 ++++++++++++++++++++++++++-- usr/src/usr.sbin/sendmail/src/conf.h | 4 +- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/usr/src/usr.sbin/sendmail/src/conf.c b/usr/src/usr.sbin/sendmail/src/conf.c index e00c406702..b31943f25c 100644 --- a/usr/src/usr.sbin/sendmail/src/conf.c +++ b/usr/src/usr.sbin/sendmail/src/conf.c @@ -7,7 +7,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)conf.c 8.176 (Berkeley) %G%"; +static char sccsid[] = "@(#)conf.c 8.177 (Berkeley) %G%"; #endif /* not lint */ # include "sendmail.h" @@ -377,7 +377,7 @@ setupmaps() dequote_init, null_map_open, null_map_close, dequote_map, null_map_store); -#ifdef USERDB +#if USERDB /* user database */ MAPDEF("userdb", ".db", 0, map_parseargs, null_map_open, null_map_close, @@ -1728,7 +1728,71 @@ reapchild(sig) #ifdef NEEDPUTENV -void +# if NEEDPUTENV == 2 /* no setenv(3) call available */ + +int +putenv(str) + char *str; +{ + char **current; + int matchlen, envlen=0; + char *tmp; + char **newenv; + static int first=1; + extern char **environ; + + /* + * find out how much of str to match when searching + * for a string to replace. + */ + if ((tmp = index(str, '=')) == NULL || tmp == str) + matchlen = strlen(str); + else + matchlen = (int) (tmp - str); + ++matchlen; + + /* + * Search for an existing string in the environment and find the + * length of environ. If found, replace and exit. + */ + for (current=environ; *current; current++) { + ++envlen; + + if (strncmp(str, *current, matchlen) == 0) { + /* found it, now insert the new version */ + *current = (char *)str; + return(0); + } + } + + /* + * There wasn't already a slot so add space for a new slot. + * If this is our first time through, use malloc(), else realloc(). + */ + if (first) { + newenv = (char **) malloc(sizeof(char *) * (envlen + 2)); + if (newenv == NULL) + return(-1); + + first=0; + (void) memcpy(newenv, environ, sizeof(char *) * envlen); + } else { + newenv = (char **) realloc((char *)environ, sizeof(char *) * (envlen + 2)); + if (newenv == NULL) + return(-1); + } + + /* actually add in the new entry */ + environ = newenv; + environ[envlen] = (char *)str; + environ[envlen+1] = NULL; + + return(0); +} + +#else /* implement putenv() in terms of setenv() */ + +int putenv(env) char *env; { @@ -1738,15 +1802,16 @@ putenv(env) p = strchr(env, '='); if (p == NULL) - return; + return 0; l = p - env; if (l > sizeof nbuf - 1) l = sizeof nbuf - 1; bcopy(env, nbuf, l); nbuf[l] = '\0'; - setenv(nbuf, ++p, 1); + return setenv(nbuf, ++p, 1); } +# endif #endif /* ** UNSETENV -- remove a variable from the environment diff --git a/usr/src/usr.sbin/sendmail/src/conf.h b/usr/src/usr.sbin/sendmail/src/conf.h index d53c4e5498..1849540b5b 100644 --- a/usr/src/usr.sbin/sendmail/src/conf.h +++ b/usr/src/usr.sbin/sendmail/src/conf.h @@ -5,7 +5,7 @@ * * %sccs.include.redist.c% * - * @(#)conf.h 8.171 (Berkeley) %G% + * @(#)conf.h 8.172 (Berkeley) %G% */ /* @@ -418,7 +418,7 @@ extern long dgux_inet_addr(); #ifdef NeXT # define HASINITGROUPS 1 /* has initgroups(3) call */ -# define NEEDPUTENV 1 /* need putenv(3) call */ +# define NEEDPUTENV 2 /* need putenv(3) call; no setenv(3) call */ # ifndef HASFLOCK # define HASFLOCK 1 /* has flock(2) call */ # endif -- 2.20.1