blob: 15c550ba30bfc9c403acdc65d997d9cf09e848bf [file] [log] [blame]
Elliott Hughes26b06072020-07-31 11:00:10 -07001/* $OpenBSD: setenv.c,v 1.19 2016/09/21 04:38:56 guenther Exp $ */
Elliott Hughes58d9e282014-04-22 17:41:00 -07002/*
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.
14 * 3. Neither the name of the University nor the names of its contributors
15 * 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
31#include <errno.h>
32#include <stdlib.h>
33#include <string.h>
34
Elliott Hughes58d9e282014-04-22 17:41:00 -070035static char **lastenv; /* last value of environ */
36
37/*
38 * putenv --
39 * Add a name=value string directly to the environmental, replacing
40 * any current value.
41 */
42int
43putenv(char *str)
44{
45 char **P, *cp;
Elliott Hughes26b06072020-07-31 11:00:10 -070046 size_t cnt = 0;
Elliott Hughes58d9e282014-04-22 17:41:00 -070047 int offset = 0;
48
49 for (cp = str; *cp && *cp != '='; ++cp)
50 ;
51 if (*cp != '=') {
52 errno = EINVAL;
53 return (-1); /* missing `=' in string */
54 }
55
56 if (__findenv(str, (int)(cp - str), &offset) != NULL) {
57 environ[offset++] = str;
58 /* could be set multiple times */
59 while (__findenv(str, (int)(cp - str), &offset)) {
60 for (P = &environ[offset];; ++P)
61 if (!(*P = *(P + 1)))
62 break;
63 }
64 return (0);
65 }
66
67 /* create new slot for string */
Elliott Hughes26b06072020-07-31 11:00:10 -070068 if (environ != NULL) {
69 for (P = environ; *P != NULL; P++)
70 ;
71 cnt = P - environ;
72 }
Elliott Hughes506c6de2016-01-15 16:30:18 -080073 P = reallocarray(lastenv, cnt + 2, sizeof(char *));
Elliott Hughes58d9e282014-04-22 17:41:00 -070074 if (!P)
75 return (-1);
Elliott Hughes26b06072020-07-31 11:00:10 -070076 if (lastenv != environ && environ != NULL)
Elliott Hughes58d9e282014-04-22 17:41:00 -070077 memcpy(P, environ, cnt * sizeof(char *));
78 lastenv = environ = P;
79 environ[cnt] = str;
80 environ[cnt + 1] = NULL;
81 return (0);
82}
Elliott Hughes506c6de2016-01-15 16:30:18 -080083DEF_WEAK(putenv);
Elliott Hughes58d9e282014-04-22 17:41:00 -070084
85/*
86 * setenv --
87 * Set the value of the environmental variable "name" to be
88 * "value". If rewrite is set, replace any current value.
89 */
90int
91setenv(const char *name, const char *value, int rewrite)
92{
93 char *C, **P;
94 const char *np;
95 int l_value, offset = 0;
96
97 if (!name || !*name) {
98 errno = EINVAL;
99 return (-1);
100 }
101 for (np = name; *np && *np != '='; ++np)
102 ;
103 if (*np) {
104 errno = EINVAL;
105 return (-1); /* has `=' in name */
106 }
107
108 l_value = strlen(value);
109 if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
110 int tmpoff = offset + 1;
111 if (!rewrite)
112 return (0);
113#if 0 /* XXX - existing entry may not be writable */
114 if (strlen(C) >= l_value) { /* old larger; copy over */
115 while ((*C++ = *value++))
116 ;
117 return (0);
118 }
119#endif
120 /* could be set multiple times */
121 while (__findenv(name, (int)(np - name), &tmpoff)) {
122 for (P = &environ[tmpoff];; ++P)
123 if (!(*P = *(P + 1)))
124 break;
125 }
126 } else { /* create new slot */
Elliott Hughes26b06072020-07-31 11:00:10 -0700127 size_t cnt = 0;
Elliott Hughes58d9e282014-04-22 17:41:00 -0700128
Elliott Hughes26b06072020-07-31 11:00:10 -0700129 if (environ != NULL) {
130 for (P = environ; *P != NULL; P++)
131 ;
132 cnt = P - environ;
133 }
Elliott Hughes506c6de2016-01-15 16:30:18 -0800134 P = reallocarray(lastenv, cnt + 2, sizeof(char *));
Elliott Hughes58d9e282014-04-22 17:41:00 -0700135 if (!P)
136 return (-1);
Elliott Hughes26b06072020-07-31 11:00:10 -0700137 if (lastenv != environ && environ != NULL)
Elliott Hughes58d9e282014-04-22 17:41:00 -0700138 memcpy(P, environ, cnt * sizeof(char *));
139 lastenv = environ = P;
140 offset = cnt;
141 environ[cnt + 1] = NULL;
142 }
143 if (!(environ[offset] = /* name + `=' + value */
Elliott Hughes26b06072020-07-31 11:00:10 -0700144 malloc((int)(np - name) + l_value + 2)))
Elliott Hughes58d9e282014-04-22 17:41:00 -0700145 return (-1);
146 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
147 ;
148 for (*C++ = '='; (*C++ = *value++); )
149 ;
150 return (0);
151}
Elliott Hughes506c6de2016-01-15 16:30:18 -0800152DEF_WEAK(setenv);
Elliott Hughes58d9e282014-04-22 17:41:00 -0700153
154/*
155 * unsetenv(name) --
156 * Delete environmental variable "name".
157 */
158int
159unsetenv(const char *name)
160{
161 char **P;
162 const char *np;
163 int offset = 0;
164
165 if (!name || !*name) {
166 errno = EINVAL;
167 return (-1);
168 }
169 for (np = name; *np && *np != '='; ++np)
170 ;
171 if (*np) {
172 errno = EINVAL;
173 return (-1); /* has `=' in name */
174 }
175
176 /* could be set multiple times */
177 while (__findenv(name, (int)(np - name), &offset)) {
178 for (P = &environ[offset];; ++P)
179 if (!(*P = *(P + 1)))
180 break;
181 }
182 return (0);
183}
Elliott Hughes506c6de2016-01-15 16:30:18 -0800184DEF_WEAK(unsetenv);