2
* Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* 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
13
* the documentation and/or other materials provided with the
16
* 3. The name "Carnegie Mellon University" must not be used to
17
* endorse or promote products derived from this software without
18
* prior written permission. For permission or any legal
19
* details, please contact
20
* Office of Technology Transfer
21
* Carnegie Mellon University
23
* Pittsburgh, PA 15213-3890
24
* (412) 268-4387, fax: (412) 268-7395
25
* tech-transfer@andrew.cmu.edu
27
* 4. Redistributions of any form whatsoever must retain the following
29
* "This product includes software developed by Computing Services
30
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
32
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
33
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
34
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
35
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
36
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
37
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
38
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40
* Copyright (c) 1999-2004 Paul Mackerras. All rights reserved.
42
* Redistribution and use in source and binary forms, with or without
43
* modification, are permitted provided that the following conditions
46
* 1. Redistributions of source code must retain the above copyright
47
* notice, this list of conditions and the following disclaimer.
49
* 2. The name(s) of the authors of this software must not be used to
50
* endorse or promote products derived from this software without
51
* prior written permission.
53
* 3. Redistributions of any form whatsoever must retain the following
55
* "This product includes software developed by Paul Mackerras
56
* <paulus@samba.org>".
58
* THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
59
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
60
* AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
61
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
63
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
64
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
66
* Copyright 2010 Ben Hutchings.
68
* Redistribution and use in source and binary forms, with or without
69
* modification, are permitted provided that the following conditions
72
* 1. Redistributions of source code must retain the above copyright
73
* notice, this list of conditions and the above disclaimer.
75
* 2. The name(s) of the authors of this software must not be used to
76
* endorse or promote products derived from this software without
77
* prior written permission.
83
#include "environ_bsd.h"
85
char **script_env; /* Env. variable values for scripts */
86
static int s_env_nalloc; /* # words avail at script_env */
89
* script_setenv - set an environment variable value to be used
90
* for scripts that we run (e.g. ip-up, auth-up, etc.)
92
void script_setenv(char *var, char *value)
94
size_t varl = strlen(var);
95
size_t vl = varl + strlen(value) + 2;
99
newstring = (char *) malloc(vl);
102
sprintf(newstring, "%s=%s", var, value);
104
/* check if this variable is already set */
105
if (script_env != 0) {
106
for (i = 0; (p = script_env[i]) != 0; ++i) {
107
if (strncmp(p, var, varl) == 0 && p[varl] == '=') {
109
script_env[i] = newstring;
114
/* no space allocated for script env. ptrs. yet */
116
script_env = (char **) malloc(16 * sizeof(char *));
122
/* reallocate script_env with more space if needed */
123
if (i + 1 >= s_env_nalloc) {
125
char **newenv = (char **) realloc((void *)script_env,
126
new_n * sizeof(char *));
130
s_env_nalloc = new_n;
133
script_env[i] = newstring;
138
* script_unsetenv - remove a variable from the environment
141
void script_unsetenv(char *var)
143
int vl = strlen(var);
149
for (i = 0; (p = script_env[i]) != 0; ++i) {
150
if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
152
while ((script_env[i] = script_env[i+1]) != 0)
160
* script_unsetenv_prefix - remove variables from the environment
163
void script_unsetenv_prefix(char *prefix)
165
int vl = strlen(prefix);
172
for (i = 0; (p = script_env[i]) != 0; ++i) {
173
if (strncmp(p, prefix, vl) == 0)
176
script_env[j++] = script_env[i];