summaryrefslogtreecommitdiff
path: root/openbsd-compat/setenv.c
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2005-11-10 10:38:45 +1100
committerDarren Tucker <dtucker@zip.com.au>2005-11-10 10:38:45 +1100
commit063ba7455fa6c726771fe0d8495544a38c29fa51 (patch)
tree31331ccbcf739f443e6bed62f0366dbd0a70efa9 /openbsd-compat/setenv.c
parent32b531067dbfaf1570faad9abadc359b1675e324 (diff)
- (dtucker) [openbsd-compat/setenv.c] Sync changes from OpenBSD setenv.c
revs 1.7 - 1.9.
Diffstat (limited to 'openbsd-compat/setenv.c')
-rw-r--r--openbsd-compat/setenv.c62
1 files changed, 23 insertions, 39 deletions
diff --git a/openbsd-compat/setenv.c b/openbsd-compat/setenv.c
index 7894c485a..93a681152 100644
--- a/openbsd-compat/setenv.c
+++ b/openbsd-compat/setenv.c
@@ -1,5 +1,6 @@
1/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */ 1/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
2 2
3/* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */
3/* 4/*
4 * Copyright (c) 1987 Regents of the University of California. 5 * Copyright (c) 1987 Regents of the University of California.
5 * All rights reserved. 6 * All rights reserved.
@@ -32,15 +33,12 @@
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
42/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */ 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.
@@ -80,14 +78,10 @@ __findenv(const char *name, int *offset)
80 * "value". If rewrite is set, replace any current value. 78 * "value". If rewrite is set, replace any current value.
81 */ 79 */
82int 80int
83setenv(name, value, rewrite) 81setenv(const char *name, const char *value, int rewrite)
84 register const char *name;
85 register const char *value;
86 int rewrite;
87{ 82{
88 extern char **environ; 83 static char **lastenv; /* last value of environ */
89 static int alloced; /* if allocated space before */ 84 char *C;
90 register char *C;
91 int l_value, offset; 85 int l_value, offset;
92 86
93 if (*value == '=') /* no `=' in value */ 87 if (*value == '=') /* no `=' in value */
@@ -102,30 +96,23 @@ setenv(name, value, rewrite)
102 return (0); 96 return (0);
103 } 97 }
104 } else { /* create new slot */ 98 } else { /* create new slot */
105 register int cnt; 99 size_t cnt;
106 register char **P; 100 char **P;
107 101
108 for (P = environ, cnt = 0; *P; ++P, ++cnt); 102 for (P = environ; *P != NULL; P++)
109 if (alloced) { /* just increase size */ 103 ;
110 P = (char **)realloc((void *)environ, 104 cnt = P - environ;
111 (size_t)(sizeof(char *) * (cnt + 2))); 105 P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
112 if (!P) 106 if (!P)
113 return (-1); 107 return (-1);
114 environ = P; 108 if (lastenv != environ)
115 } 109 memcpy(P, environ, cnt * sizeof(char *));
116 else { /* get new space */ 110 lastenv = environ = P;
117 alloced = 1; /* copy old entries into it */
118 P = (char **)malloc((size_t)(sizeof(char *) *
119 (cnt + 2)));
120 if (!P)
121 return (-1);
122 memmove(P, environ, cnt * sizeof(char *));
123 environ = P;
124 }
125 environ[cnt + 1] = NULL;
126 offset = cnt; 111 offset = cnt;
112 environ[cnt + 1] = NULL;
127 } 113 }
128 for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ 114 for (C = (char *)name; *C && *C != '='; ++C)
115 ; /* no `=' in name */
129 if (!(environ[offset] = /* name + `=' + value */ 116 if (!(environ[offset] = /* name + `=' + value */
130 malloc((size_t)((int)(C - name) + l_value + 2)))) 117 malloc((size_t)((int)(C - name) + l_value + 2))))
131 return (-1); 118 return (-1);
@@ -143,15 +130,12 @@ setenv(name, value, rewrite)
143 * Delete environmental variable "name". 130 * Delete environmental variable "name".
144 */ 131 */
145void 132void
146unsetenv(name) 133unsetenv(const char *name)
147 const char *name;
148{ 134{
149 extern char **environ; 135 char **P;
150 register char **P;
151 int offset; 136 int offset;
152 char *__findenv();
153 137
154 while (__findenv(name, &offset)) /* if set multiple times */ 138 while (__findenv(name, &offset)) /* if set multiple times */
155 for (P = &environ[offset];; ++P) 139 for (P = &environ[offset];; ++P)
156 if (!(*P = *(P + 1))) 140 if (!(*P = *(P + 1)))
157 break; 141 break;