diff options
Diffstat (limited to 'src/lib/libc/stdlib/setenv.c')
| -rw-r--r-- | src/lib/libc/stdlib/setenv.c | 72 |
1 files changed, 29 insertions, 43 deletions
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c index a36669888d..ce0d3f9699 100644 --- a/src/lib/libc/stdlib/setenv.c +++ b/src/lib/libc/stdlib/setenv.c | |||
| @@ -10,11 +10,7 @@ | |||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the | 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. | 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software | 13 | * 3. Neither the name of the University nor the names of its contributors |
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | 14 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. | 15 | * without specific prior written permission. |
| 20 | * | 16 | * |
| @@ -32,29 +28,27 @@ | |||
| 32 | */ | 28 | */ |
| 33 | 29 | ||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | 30 | #if defined(LIBC_SCCS) && !defined(lint) |
| 35 | /*static char *sccsid = "from: @(#)setenv.c 5.6 (Berkeley) 6/4/91";*/ | 31 | static char *rcsid = "$OpenBSD: setenv.c,v 1.8 2005/03/30 18:51:49 pat Exp $"; |
| 36 | static char *rcsid = "$Id: setenv.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | 32 | #endif /* LIBC_SCCS and not lint */ |
| 38 | 33 | ||
| 39 | #include <stdlib.h> | 34 | #include <stdlib.h> |
| 40 | #include <string.h> | 35 | #include <string.h> |
| 41 | 36 | ||
| 37 | char *__findenv(const char *name, int *offset); | ||
| 38 | |||
| 39 | extern char **environ; | ||
| 40 | |||
| 42 | /* | 41 | /* |
| 43 | * setenv -- | 42 | * setenv -- |
| 44 | * Set the value of the environmental variable "name" to be | 43 | * Set the value of the environmental variable "name" to be |
| 45 | * "value". If rewrite is set, replace any current value. | 44 | * "value". If rewrite is set, replace any current value. |
| 46 | */ | 45 | */ |
| 47 | int | 46 | int |
| 48 | setenv(name, value, rewrite) | 47 | setenv(const char *name, const char *value, int rewrite) |
| 49 | register const char *name; | ||
| 50 | register const char *value; | ||
| 51 | int rewrite; | ||
| 52 | { | 48 | { |
| 53 | extern char **environ; | 49 | static char **lastenv; /* last value of environ */ |
| 54 | static int alloced; /* if allocated space before */ | 50 | char *C; |
| 55 | register char *C; | ||
| 56 | int l_value, offset; | 51 | int l_value, offset; |
| 57 | char *__findenv(); | ||
| 58 | 52 | ||
| 59 | if (*value == '=') /* no `=' in value */ | 53 | if (*value == '=') /* no `=' in value */ |
| 60 | ++value; | 54 | ++value; |
| @@ -63,39 +57,34 @@ setenv(name, value, rewrite) | |||
| 63 | if (!rewrite) | 57 | if (!rewrite) |
| 64 | return (0); | 58 | return (0); |
| 65 | if (strlen(C) >= l_value) { /* old larger; copy over */ | 59 | if (strlen(C) >= l_value) { /* old larger; copy over */ |
| 66 | while (*C++ = *value++); | 60 | while ((*C++ = *value++)) |
| 61 | ; | ||
| 67 | return (0); | 62 | return (0); |
| 68 | } | 63 | } |
| 69 | } else { /* create new slot */ | 64 | } else { /* create new slot */ |
| 70 | register int cnt; | 65 | size_t cnt; |
| 71 | register char **P; | 66 | char **P; |
| 72 | 67 | ||
| 73 | for (P = environ, cnt = 0; *P; ++P, ++cnt); | 68 | for (P = environ; *P != NULL; P++) |
| 74 | if (alloced) { /* just increase size */ | 69 | ; |
| 75 | environ = (char **)realloc((char *)environ, | 70 | cnt = P - environ; |
| 76 | (size_t)(sizeof(char *) * (cnt + 2))); | 71 | P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2)); |
| 77 | if (!environ) | 72 | if (!P) |
| 78 | return (-1); | 73 | return (-1); |
| 79 | } | 74 | if (lastenv != environ) |
| 80 | else { /* get new space */ | 75 | memcpy(P, environ, cnt * sizeof(char *)); |
| 81 | alloced = 1; /* copy old entries into it */ | 76 | lastenv = environ = P; |
| 82 | P = (char **)malloc((size_t)(sizeof(char *) * | ||
| 83 | (cnt + 2))); | ||
| 84 | if (!P) | ||
| 85 | return (-1); | ||
| 86 | bcopy(environ, P, cnt * sizeof(char *)); | ||
| 87 | environ = P; | ||
| 88 | } | ||
| 89 | environ[cnt + 1] = NULL; | ||
| 90 | offset = cnt; | 77 | offset = cnt; |
| 78 | environ[cnt + 1] = NULL; | ||
| 91 | } | 79 | } |
| 92 | for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ | 80 | for (C = (char *)name; *C && *C != '='; ++C) |
| 81 | ; /* no `=' in name */ | ||
| 93 | if (!(environ[offset] = /* name + `=' + value */ | 82 | if (!(environ[offset] = /* name + `=' + value */ |
| 94 | malloc((size_t)((int)(C - name) + l_value + 2)))) | 83 | malloc((size_t)((int)(C - name) + l_value + 2)))) |
| 95 | return (-1); | 84 | return (-1); |
| 96 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) | 85 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) |
| 97 | ; | 86 | ; |
| 98 | for (*C++ = '='; *C++ = *value++; ) | 87 | for (*C++ = '='; (*C++ = *value++); ) |
| 99 | ; | 88 | ; |
| 100 | return (0); | 89 | return (0); |
| 101 | } | 90 | } |
| @@ -105,15 +94,12 @@ setenv(name, value, rewrite) | |||
| 105 | * Delete environmental variable "name". | 94 | * Delete environmental variable "name". |
| 106 | */ | 95 | */ |
| 107 | void | 96 | void |
| 108 | unsetenv(name) | 97 | unsetenv(const char *name) |
| 109 | const char *name; | ||
| 110 | { | 98 | { |
| 111 | extern char **environ; | 99 | char **P; |
| 112 | register char **P; | ||
| 113 | int offset; | 100 | int offset; |
| 114 | char *__findenv(); | ||
| 115 | 101 | ||
| 116 | while (__findenv(name, &offset)) /* if set multiple times */ | 102 | while (__findenv(name, &offset)) /* if set multiple times */ |
| 117 | for (P = &environ[offset];; ++P) | 103 | for (P = &environ[offset];; ++P) |
| 118 | if (!(*P = *(P + 1))) | 104 | if (!(*P = *(P + 1))) |
| 119 | break; | 105 | break; |
