blob: 373b701d9c6feac53635593f5a9db1a238e5944b [file] [log] [blame]
Damien Millerd1a74582011-09-23 11:26:34 +10001/* $OpenBSD: setenv.c,v 1.13 2010/08/23 22:31:50 millert Exp $ */
Damien Miller2c9279f2000-03-26 12:12:34 +10002/*
3 * Copyright (c) 1987 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Miller329638e2003-06-03 12:12:50 +100014 * 3. Neither the name of the University nor the names of its contributors
Damien Miller2c9279f2000-03-26 12:12:34 +100015 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
Darren Tucker7f24a0e2005-11-10 16:18:56 +110031/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
32
Ben Lindstromdd21fe92002-06-27 18:23:20 +000033#include "includes.h"
Damien Millerd1a74582011-09-23 11:26:34 +100034
Darren Tucker86c093d2004-03-08 22:59:03 +110035#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
Damien Miller2c9279f2000-03-26 12:12:34 +100036
Damien Millerd1a74582011-09-23 11:26:34 +100037#include <errno.h>
Damien Miller2c9279f2000-03-26 12:12:34 +100038#include <stdlib.h>
39#include <string.h>
40
Darren Tucker063ba742005-11-10 10:38:45 +110041extern char **environ;
Damien Millerd1a74582011-09-23 11:26:34 +100042static char **lastenv; /* last value of environ */
Darren Tuckerb8c89d12005-11-10 10:10:10 +110043
Darren Tucker063ba742005-11-10 10:38:45 +110044/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */
Damien Miller2c9279f2000-03-26 12:12:34 +100045/*
46 * __findenv --
47 * Returns pointer to value associated with name, if any, else NULL.
Damien Millerd1a74582011-09-23 11:26:34 +100048 * Starts searching within the environmental array at offset.
Damien Miller2c9279f2000-03-26 12:12:34 +100049 * Sets offset to be the offset of the name/value combination in the
Damien Millerd1a74582011-09-23 11:26:34 +100050 * environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
Damien Miller2c9279f2000-03-26 12:12:34 +100051 * Explicitly removes '=' in argument name.
Damien Millerd1a74582011-09-23 11:26:34 +100052 *
53 * This routine *should* be a static; don't use it.
Damien Miller2c9279f2000-03-26 12:12:34 +100054 */
Darren Tucker32b53102005-11-10 10:13:06 +110055static char *
Damien Millerd1a74582011-09-23 11:26:34 +100056__findenv(const char *name, int len, int *offset)
Damien Miller2c9279f2000-03-26 12:12:34 +100057{
58 extern char **environ;
Damien Millerd1a74582011-09-23 11:26:34 +100059 int i;
Darren Tuckerb8c89d12005-11-10 10:10:10 +110060 const char *np;
61 char **p, *cp;
Damien Miller2c9279f2000-03-26 12:12:34 +100062
63 if (name == NULL || environ == NULL)
64 return (NULL);
Damien Millerd1a74582011-09-23 11:26:34 +100065 for (p = environ + *offset; (cp = *p) != NULL; ++p) {
Damien Miller2c9279f2000-03-26 12:12:34 +100066 for (np = name, i = len; i && *cp; i--)
67 if (*cp++ != *np++)
68 break;
69 if (i == 0 && *cp++ == '=') {
70 *offset = p - environ;
71 return (cp);
72 }
73 }
74 return (NULL);
75}
76
Damien Millerd1a74582011-09-23 11:26:34 +100077#if 0 /* nothing uses putenv */
78/*
79 * putenv --
80 * Add a name=value string directly to the environmental, replacing
81 * any current value.
82 */
83int
84putenv(char *str)
85{
86 char **P, *cp;
87 size_t cnt;
88 int offset = 0;
89
90 for (cp = str; *cp && *cp != '='; ++cp)
91 ;
92 if (*cp != '=') {
93 errno = EINVAL;
94 return (-1); /* missing `=' in string */
95 }
96
97 if (__findenv(str, (int)(cp - str), &offset) != NULL) {
98 environ[offset++] = str;
99 /* could be set multiple times */
100 while (__findenv(str, (int)(cp - str), &offset)) {
101 for (P = &environ[offset];; ++P)
102 if (!(*P = *(P + 1)))
103 break;
104 }
105 return (0);
106 }
107
108 /* create new slot for string */
109 for (P = environ; *P != NULL; P++)
110 ;
111 cnt = P - environ;
112 P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
113 if (!P)
114 return (-1);
115 if (lastenv != environ)
116 memcpy(P, environ, cnt * sizeof(char *));
117 lastenv = environ = P;
118 environ[cnt] = str;
119 environ[cnt + 1] = NULL;
120 return (0);
121}
122
123#endif
124
Darren Tucker86c093d2004-03-08 22:59:03 +1100125#ifndef HAVE_SETENV
Damien Miller2c9279f2000-03-26 12:12:34 +1000126/*
127 * setenv --
128 * Set the value of the environmental variable "name" to be
129 * "value". If rewrite is set, replace any current value.
130 */
131int
Darren Tucker063ba742005-11-10 10:38:45 +1100132setenv(const char *name, const char *value, int rewrite)
Damien Miller2c9279f2000-03-26 12:12:34 +1000133{
Damien Millerd1a74582011-09-23 11:26:34 +1000134 char *C, **P;
135 const char *np;
136 int l_value, offset = 0;
Damien Miller2c9279f2000-03-26 12:12:34 +1000137
Damien Millerd1a74582011-09-23 11:26:34 +1000138 for (np = name; *np && *np != '='; ++np)
139 ;
140#ifdef notyet
141 if (*np) {
142 errno = EINVAL;
143 return (-1); /* has `=' in name */
144 }
145#endif
146
Damien Miller2c9279f2000-03-26 12:12:34 +1000147 l_value = strlen(value);
Damien Millerd1a74582011-09-23 11:26:34 +1000148 if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
149 int tmpoff = offset + 1;
Damien Miller2c9279f2000-03-26 12:12:34 +1000150 if (!rewrite)
151 return (0);
Damien Millerd1a74582011-09-23 11:26:34 +1000152#if 0 /* XXX - existing entry may not be writable */
Damien Miller2c9279f2000-03-26 12:12:34 +1000153 if (strlen(C) >= l_value) { /* old larger; copy over */
Damien Miller8e3bdca2002-02-13 16:00:15 +1100154 while ((*C++ = *value++))
155 ;
Damien Miller2c9279f2000-03-26 12:12:34 +1000156 return (0);
157 }
Damien Millerd1a74582011-09-23 11:26:34 +1000158#endif
159 /* could be set multiple times */
160 while (__findenv(name, (int)(np - name), &tmpoff)) {
161 for (P = &environ[tmpoff];; ++P)
162 if (!(*P = *(P + 1)))
163 break;
164 }
Damien Miller2c9279f2000-03-26 12:12:34 +1000165 } else { /* create new slot */
Darren Tucker063ba742005-11-10 10:38:45 +1100166 size_t cnt;
Damien Miller2c9279f2000-03-26 12:12:34 +1000167
Darren Tucker063ba742005-11-10 10:38:45 +1100168 for (P = environ; *P != NULL; P++)
169 ;
170 cnt = P - environ;
171 P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
172 if (!P)
173 return (-1);
174 if (lastenv != environ)
175 memcpy(P, environ, cnt * sizeof(char *));
176 lastenv = environ = P;
Damien Miller2c9279f2000-03-26 12:12:34 +1000177 offset = cnt;
Darren Tucker063ba742005-11-10 10:38:45 +1100178 environ[cnt + 1] = NULL;
Damien Miller2c9279f2000-03-26 12:12:34 +1000179 }
Damien Miller2c9279f2000-03-26 12:12:34 +1000180 if (!(environ[offset] = /* name + `=' + value */
Damien Millerd1a74582011-09-23 11:26:34 +1000181 malloc((size_t)((int)(np - name) + l_value + 2))))
Damien Miller2c9279f2000-03-26 12:12:34 +1000182 return (-1);
183 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
184 ;
185 for (*C++ = '='; (*C++ = *value++); )
186 ;
187 return (0);
188}
Damien Millerd1a74582011-09-23 11:26:34 +1000189
Darren Tucker86c093d2004-03-08 22:59:03 +1100190#endif /* HAVE_SETENV */
Damien Miller2c9279f2000-03-26 12:12:34 +1000191
Darren Tucker86c093d2004-03-08 22:59:03 +1100192#ifndef HAVE_UNSETENV
Damien Miller2c9279f2000-03-26 12:12:34 +1000193/*
194 * unsetenv(name) --
195 * Delete environmental variable "name".
196 */
Damien Millerd1a74582011-09-23 11:26:34 +1000197int
Darren Tucker063ba742005-11-10 10:38:45 +1100198unsetenv(const char *name)
Damien Miller2c9279f2000-03-26 12:12:34 +1000199{
Darren Tucker063ba742005-11-10 10:38:45 +1100200 char **P;
Damien Millerd1a74582011-09-23 11:26:34 +1000201 const char *np;
202 int offset = 0;
Damien Miller2c9279f2000-03-26 12:12:34 +1000203
Damien Millerd1a74582011-09-23 11:26:34 +1000204 if (!name || !*name) {
205 errno = EINVAL;
206 return (-1);
207 }
208 for (np = name; *np && *np != '='; ++np)
209 ;
210 if (*np) {
211 errno = EINVAL;
212 return (-1); /* has `=' in name */
213 }
214
215 /* could be set multiple times */
216 while (__findenv(name, (int)(np - name), &offset)) {
Damien Miller2c9279f2000-03-26 12:12:34 +1000217 for (P = &environ[offset];; ++P)
218 if (!(*P = *(P + 1)))
219 break;
Damien Millerd1a74582011-09-23 11:26:34 +1000220 }
221 return (0);
Damien Miller2c9279f2000-03-26 12:12:34 +1000222}
Darren Tucker86c093d2004-03-08 22:59:03 +1100223#endif /* HAVE_UNSETENV */
Damien Miller2c9279f2000-03-26 12:12:34 +1000224
Darren Tucker86c093d2004-03-08 22:59:03 +1100225#endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */
Damien Millerd1a74582011-09-23 11:26:34 +1000226