Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 1 | /* $OpenBSD: setenv.c,v 1.13 2010/08/23 22:31:50 millert Exp $ */ |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 2 | /* |
| 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 Miller | 329638e | 2003-06-03 12:12:50 +1000 | [diff] [blame] | 14 | * 3. Neither the name of the University nor the names of its contributors |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 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 | |
Darren Tucker | 7f24a0e | 2005-11-10 16:18:56 +1100 | [diff] [blame] | 31 | /* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */ |
| 32 | |
Ben Lindstrom | dd21fe9 | 2002-06-27 18:23:20 +0000 | [diff] [blame] | 33 | #include "includes.h" |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 34 | |
Darren Tucker | 86c093d | 2004-03-08 22:59:03 +1100 | [diff] [blame] | 35 | #if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 36 | |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 37 | #include <errno.h> |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 41 | extern char **environ; |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 42 | static char **lastenv; /* last value of environ */ |
Darren Tucker | b8c89d1 | 2005-11-10 10:10:10 +1100 | [diff] [blame] | 43 | |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 44 | /* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */ |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 45 | /* |
| 46 | * __findenv -- |
| 47 | * Returns pointer to value associated with name, if any, else NULL. |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 48 | * Starts searching within the environmental array at offset. |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 49 | * Sets offset to be the offset of the name/value combination in the |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 50 | * environmental array, for use by putenv(3), setenv(3) and unsetenv(3). |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 51 | * Explicitly removes '=' in argument name. |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 52 | * |
| 53 | * This routine *should* be a static; don't use it. |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 54 | */ |
Darren Tucker | 32b5310 | 2005-11-10 10:13:06 +1100 | [diff] [blame] | 55 | static char * |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 56 | __findenv(const char *name, int len, int *offset) |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 57 | { |
| 58 | extern char **environ; |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 59 | int i; |
Darren Tucker | b8c89d1 | 2005-11-10 10:10:10 +1100 | [diff] [blame] | 60 | const char *np; |
| 61 | char **p, *cp; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 62 | |
| 63 | if (name == NULL || environ == NULL) |
| 64 | return (NULL); |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 65 | for (p = environ + *offset; (cp = *p) != NULL; ++p) { |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 66 | 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 Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 77 | #if 0 /* nothing uses putenv */ |
| 78 | /* |
| 79 | * putenv -- |
| 80 | * Add a name=value string directly to the environmental, replacing |
| 81 | * any current value. |
| 82 | */ |
| 83 | int |
| 84 | putenv(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 Tucker | 86c093d | 2004-03-08 22:59:03 +1100 | [diff] [blame] | 125 | #ifndef HAVE_SETENV |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 126 | /* |
| 127 | * setenv -- |
| 128 | * Set the value of the environmental variable "name" to be |
| 129 | * "value". If rewrite is set, replace any current value. |
| 130 | */ |
| 131 | int |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 132 | setenv(const char *name, const char *value, int rewrite) |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 133 | { |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 134 | char *C, **P; |
| 135 | const char *np; |
| 136 | int l_value, offset = 0; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 137 | |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 138 | 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 Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 147 | l_value = strlen(value); |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 148 | if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) { |
| 149 | int tmpoff = offset + 1; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 150 | if (!rewrite) |
| 151 | return (0); |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 152 | #if 0 /* XXX - existing entry may not be writable */ |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 153 | if (strlen(C) >= l_value) { /* old larger; copy over */ |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 154 | while ((*C++ = *value++)) |
| 155 | ; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 156 | return (0); |
| 157 | } |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 158 | #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 Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 165 | } else { /* create new slot */ |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 166 | size_t cnt; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 167 | |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 168 | 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 Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 177 | offset = cnt; |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 178 | environ[cnt + 1] = NULL; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 179 | } |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 180 | if (!(environ[offset] = /* name + `=' + value */ |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 181 | malloc((size_t)((int)(np - name) + l_value + 2)))) |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 182 | return (-1); |
| 183 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) |
| 184 | ; |
| 185 | for (*C++ = '='; (*C++ = *value++); ) |
| 186 | ; |
| 187 | return (0); |
| 188 | } |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 189 | |
Darren Tucker | 86c093d | 2004-03-08 22:59:03 +1100 | [diff] [blame] | 190 | #endif /* HAVE_SETENV */ |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 191 | |
Darren Tucker | 86c093d | 2004-03-08 22:59:03 +1100 | [diff] [blame] | 192 | #ifndef HAVE_UNSETENV |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 193 | /* |
| 194 | * unsetenv(name) -- |
| 195 | * Delete environmental variable "name". |
| 196 | */ |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 197 | int |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 198 | unsetenv(const char *name) |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 199 | { |
Darren Tucker | 063ba74 | 2005-11-10 10:38:45 +1100 | [diff] [blame] | 200 | char **P; |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 201 | const char *np; |
| 202 | int offset = 0; |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 203 | |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 204 | 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 Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 217 | for (P = &environ[offset];; ++P) |
| 218 | if (!(*P = *(P + 1))) |
| 219 | break; |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 220 | } |
| 221 | return (0); |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 222 | } |
Darren Tucker | 86c093d | 2004-03-08 22:59:03 +1100 | [diff] [blame] | 223 | #endif /* HAVE_UNSETENV */ |
Damien Miller | 2c9279f | 2000-03-26 12:12:34 +1000 | [diff] [blame] | 224 | |
Darren Tucker | 86c093d | 2004-03-08 22:59:03 +1100 | [diff] [blame] | 225 | #endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */ |
Damien Miller | d1a7458 | 2011-09-23 11:26:34 +1000 | [diff] [blame] | 226 | |