]> andersk Git - openssh.git/blobdiff - openbsd-compat/setenv.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / openbsd-compat / setenv.c
index e5c5de62ec2d4b8c7430f6cb97e9c112dd9f439c..e2a8b6dd3ca972e76eb324caac9e8e2146ac8e0a 100644 (file)
@@ -1,3 +1,4 @@
+/*     $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */
 /*
  * Copyright (c) 1987 Regents of the University of California.
  * All rights reserved.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * SUCH DAMAGE.
  */
 
-#include "includes.h"
-#ifndef HAVE_SETENV
+/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: setenv.c,v 1.5 2002/12/10 22:44:13 mickey Exp $";
-#endif /* LIBC_SCCS and not lint */
+#include "includes.h"
+#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
 
 #include <stdlib.h>
 #include <string.h>
 
-char *__findenv(const char *name, int *offset);
+extern char **environ;
 
+/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */
 /*
  * __findenv --
  *     Returns pointer to value associated with name, if any, else NULL.
  *     Sets offset to be the offset of the name/value combination in the
  *     environmental array, for use by setenv(3) and unsetenv(3).
  *     Explicitly removes '=' in argument name.
- *
- *     This routine *should* be a static; don't use it.
  */
-char *
-__findenv(name, offset)
-       register const char *name;
-       int *offset;
+static char *
+__findenv(const char *name, size_t *offset)
 {
        extern char **environ;
-       register int len, i;
-       register const char *np;
-       register char **p, *cp;
+       int len, i;
+       const char *np;
+       char **p, *cp;
 
        if (name == NULL || environ == NULL)
                return (NULL);
@@ -79,21 +71,18 @@ __findenv(name, offset)
        return (NULL);
 }
 
+#ifndef HAVE_SETENV
 /*
  * setenv --
  *     Set the value of the environmental variable "name" to be
  *     "value".  If rewrite is set, replace any current value.
  */
 int
-setenv(name, value, rewrite)
-       register const char *name;
-       register const char *value;
-       int rewrite;
+setenv(const char *name, const char *value, int rewrite)
 {
-       extern char **environ;
-       static int alloced;                     /* if allocated space before */
-       register char *C;
-       int l_value, offset;
+       static char **lastenv;                  /* last value of environ */
+       char *C;
+       size_t l_value, offset;
 
        if (*value == '=')                      /* no `=' in value */
                ++value;
@@ -107,30 +96,23 @@ setenv(name, value, rewrite)
                        return (0);
                }
        } else {                                        /* create new slot */
-               register int    cnt;
-               register char   **P;
+               size_t cnt;
+               char **P;
 
-               for (P = environ, cnt = 0; *P; ++P, ++cnt);
-               if (alloced) {                  /* just increase size */
-                       P = (char **)realloc((void *)environ,
-                           (size_t)(sizeof(char *) * (cnt + 2)));
-                       if (!P)
-                               return (-1);
-                       environ = P;
-               }
-               else {                          /* get new space */
-                       alloced = 1;            /* copy old entries into it */
-                       P = (char **)malloc((size_t)(sizeof(char *) *
-                           (cnt + 2)));
-                       if (!P)
-                               return (-1);
-                       memmove(P, environ, cnt * sizeof(char *));
-                       environ = P;
-               }
-               environ[cnt + 1] = NULL;
+               for (P = environ; *P != NULL; P++)
+                       ;
+               cnt = P - environ;
+               P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
+               if (!P)
+                       return (-1);
+               if (lastenv != environ)
+                       memcpy(P, environ, cnt * sizeof(char *));
+               lastenv = environ = P;
                offset = cnt;
+               environ[cnt + 1] = NULL;
        }
-       for (C = (char *)name; *C && *C != '='; ++C);   /* no `=' in name */
+       for (C = (char *)name; *C && *C != '='; ++C)
+               ;                               /* no `=' in name */
        if (!(environ[offset] =                 /* name + `=' + value */
            malloc((size_t)((int)(C - name) + l_value + 2))))
                return (-1);
@@ -140,24 +122,24 @@ setenv(name, value, rewrite)
                ;
        return (0);
 }
+#endif /* HAVE_SETENV */
 
+#ifndef HAVE_UNSETENV
 /*
  * unsetenv(name) --
  *     Delete environmental variable "name".
  */
 void
-unsetenv(name)
-       const char      *name;
+unsetenv(const char *name)
 {
-       extern char **environ;
-       register char **P;
-       int offset;
-       char *__findenv();
+       char **P;
+       size_t offset;
 
-       while (__findenv(name, &offset))                /* if set multiple times */
+       while (__findenv(name, &offset))        /* if set multiple times */
                for (P = &environ[offset];; ++P)
                        if (!(*P = *(P + 1)))
                                break;
 }
+#endif /* HAVE_UNSETENV */
 
-#endif /* HAVE_SETENV */
+#endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */
This page took 0.789137 seconds and 4 git commands to generate.