summaryrefslogtreecommitdiff
path: root/openbsd-compat/setenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/setenv.c')
-rw-r--r--openbsd-compat/setenv.c80
1 files changed, 30 insertions, 50 deletions
diff --git a/openbsd-compat/setenv.c b/openbsd-compat/setenv.c
index c3a86c651..b52a99c2c 100644
--- a/openbsd-compat/setenv.c
+++ b/openbsd-compat/setenv.c
@@ -1,5 +1,4 @@
1/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */ 1/* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */
2
3/* 2/*
4 * Copyright (c) 1987 Regents of the University of California. 3 * Copyright (c) 1987 Regents of the University of California.
5 * All rights reserved. 4 * All rights reserved.
@@ -29,36 +28,31 @@
29 * SUCH DAMAGE. 28 * SUCH DAMAGE.
30 */ 29 */
31 30
31/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
32
32#include "includes.h" 33#include "includes.h"
33#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) 34#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
34 35
35#if defined(LIBC_SCCS) && !defined(lint)
36static char *rcsid = "$OpenBSD: setenv.c,v 1.6 2003/06/02 20:18:38 millert Exp $";
37#endif /* LIBC_SCCS and not lint */
38
39#include <stdlib.h> 36#include <stdlib.h>
40#include <string.h> 37#include <string.h>
41 38
42char *__findenv(const char *name, int *offset); 39extern char **environ;
43 40
41/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */
44/* 42/*
45 * __findenv -- 43 * __findenv --
46 * Returns pointer to value associated with name, if any, else NULL. 44 * Returns pointer to value associated with name, if any, else NULL.
47 * Sets offset to be the offset of the name/value combination in the 45 * Sets offset to be the offset of the name/value combination in the
48 * environmental array, for use by setenv(3) and unsetenv(3). 46 * environmental array, for use by setenv(3) and unsetenv(3).
49 * Explicitly removes '=' in argument name. 47 * Explicitly removes '=' in argument name.
50 *
51 * This routine *should* be a static; don't use it.
52 */ 48 */
53char * 49static char *
54__findenv(name, offset) 50__findenv(const char *name, int *offset)
55 register const char *name;
56 int *offset;
57{ 51{
58 extern char **environ; 52 extern char **environ;
59 register int len, i; 53 int len, i;
60 register const char *np; 54 const char *np;
61 register char **p, *cp; 55 char **p, *cp;
62 56
63 if (name == NULL || environ == NULL) 57 if (name == NULL || environ == NULL)
64 return (NULL); 58 return (NULL);
@@ -84,14 +78,10 @@ __findenv(name, offset)
84 * "value". If rewrite is set, replace any current value. 78 * "value". If rewrite is set, replace any current value.
85 */ 79 */
86int 80int
87setenv(name, value, rewrite) 81setenv(const char *name, const char *value, int rewrite)
88 register const char *name;
89 register const char *value;
90 int rewrite;
91{ 82{
92 extern char **environ; 83 static char **lastenv; /* last value of environ */
93 static int alloced; /* if allocated space before */ 84 char *C;
94 register char *C;
95 int l_value, offset; 85 int l_value, offset;
96 86
97 if (*value == '=') /* no `=' in value */ 87 if (*value == '=') /* no `=' in value */
@@ -106,30 +96,23 @@ setenv(name, value, rewrite)
106 return (0); 96 return (0);
107 } 97 }
108 } else { /* create new slot */ 98 } else { /* create new slot */
109 register int cnt; 99 size_t cnt;
110 register char **P; 100 char **P;
111 101
112 for (P = environ, cnt = 0; *P; ++P, ++cnt); 102 for (P = environ; *P != NULL; P++)
113 if (alloced) { /* just increase size */ 103 ;
114 P = (char **)realloc((void *)environ, 104 cnt = P - environ;
115 (size_t)(sizeof(char *) * (cnt + 2))); 105 P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
116 if (!P) 106 if (!P)
117 return (-1); 107 return (-1);
118 environ = P; 108 if (lastenv != environ)
119 } 109 memcpy(P, environ, cnt * sizeof(char *));
120 else { /* get new space */ 110 lastenv = environ = P;
121 alloced = 1; /* copy old entries into it */
122 P = (char **)malloc((size_t)(sizeof(char *) *
123 (cnt + 2)));
124 if (!P)
125 return (-1);
126 memmove(P, environ, cnt * sizeof(char *));
127 environ = P;
128 }
129 environ[cnt + 1] = NULL;
130 offset = cnt; 111 offset = cnt;
112 environ[cnt + 1] = NULL;
131 } 113 }
132 for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ 114 for (C = (char *)name; *C && *C != '='; ++C)
115 ; /* no `=' in name */
133 if (!(environ[offset] = /* name + `=' + value */ 116 if (!(environ[offset] = /* name + `=' + value */
134 malloc((size_t)((int)(C - name) + l_value + 2)))) 117 malloc((size_t)((int)(C - name) + l_value + 2))))
135 return (-1); 118 return (-1);
@@ -147,15 +130,12 @@ setenv(name, value, rewrite)
147 * Delete environmental variable "name". 130 * Delete environmental variable "name".
148 */ 131 */
149void 132void
150unsetenv(name) 133unsetenv(const char *name)
151 const char *name;
152{ 134{
153 extern char **environ; 135 char **P;
154 register char **P;
155 int offset; 136 int offset;
156 char *__findenv();
157 137
158 while (__findenv(name, &offset)) /* if set multiple times */ 138 while (__findenv(name, &offset)) /* if set multiple times */
159 for (P = &environ[offset];; ++P) 139 for (P = &environ[offset];; ++P)
160 if (!(*P = *(P + 1))) 140 if (!(*P = *(P + 1)))
161 break; 141 break;