<<< Date Index >>>     <<< Thread Index >>>

Re: [PATCH] replace setenv() with putenv()



On Thursday, 06 October 2005 at 14:28, David Champion wrote:
> * On 2005.10.06, in <20051006181856.F3051@xxxxxxxxxxxxxxxxxxxxx>,
> *     "Moritz Barsnick" <moritz+mutt@xxxxxxxxxxxx> wrote:
> > 
> > Are you sure all systems actually have putenv()? I know I had problems
> > with the one or the other in old SunOS (Solaris). Usually there should
> > be an autoconf check for it.
> 
> No, I'm not sure -- but all the systems I use do, and it is POSIX, which
> is a reasonable baseline.  An autoconf check would be better, but I think
> this is safe.
> 
> Solaris 5.9 has putenv() but not setenv(), for example.  (Solaris 5.10
> does have setenv().)
> 
> 
> > OTOH, I haven't checked whether mutt already uses putenv() in other
> > places without alternatives, and whether these "old" systems are
> > considered dying. ;-)
> 
> It had neither until this code was added recently.

I think I'd prefer the attached patch if it works. Does it?

Index: configure.in
===================================================================
RCS file: /home/roessler/cvs/mutt/configure.in,v
retrieving revision 3.41
diff -u -p -r3.41 configure.in
--- configure.in        29 Sep 2005 23:52:16 -0000      3.41
+++ configure.in        9 Oct 2005 04:50:02 -0000
@@ -332,7 +332,7 @@ AC_CHECK_TYPE(ssize_t, int)
 
 AC_CHECK_FUNCS(fgetpos memmove setegid srand48 strerror)
 
-AC_REPLACE_FUNCS(strcasecmp strdup)
+AC_REPLACE_FUNCS([setenv strcasecmp strdup])
 
 AC_CHECK_FUNC(getopt)
 if test $ac_cv_func_getopt = yes; then
Index: setenv.c
===================================================================
RCS file: setenv.c
diff -N setenv.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ setenv.c    9 Oct 2005 04:50:02 -0000
@@ -0,0 +1,47 @@
+/*  $Id: setenv.c,v 1.6 2002/09/01 03:04:10 rra Exp $
+**
+**  Replacement for a missing setenv.
+**
+**  Written by Russ Allbery <rra@xxxxxxxxxxxx>
+**  This work is hereby placed in the public domain by its author.
+**
+**  Provides the same functionality as the standard library routine setenv
+**  for those platforms that don't have it.
+*/
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+int
+setenv(const char *name, const char *value, int overwrite)
+{
+    char *envstring;
+
+    if (!overwrite && getenv(name) != NULL)
+        return 0;
+
+    /* Allocate memory for the environment string.  We intentionally don't
+       use concat here, or the xmalloc family of allocation routines, since
+       the intention is to provide a replacement for the standard library
+       function which sets errno and returns in the event of a memory
+       allocation failure. */
+    envstring = malloc(strlen(name) + 1 + strlen(value) + 1);
+    if (envstring == NULL)
+        return -1;
+
+    /* Build the environment string and add it to the environment using
+       putenv.  Systems without putenv lose, but XPG4 requires it. */
+    strcpy(envstring, name);
+    strcat(envstring, "=");
+    strcat(envstring, value);
+    return putenv(envstring);
+
+    /* Note that the memory allocated is not freed.  This is intentional;
+       many implementations of putenv assume that the string passed to
+       putenv will never be freed and don't make a copy of it.  Repeated use
+       of this function will therefore leak memory, since most
+       implementations of putenv also don't free strings removed from the
+       environment (due to being overwritten). */
+}

Attachment: pgpcljeUNSP7G.pgp
Description: PGP signature