1 /* $NetBSD: setenv.c,v 1.4 2014/12/10 04:37:56 christos Exp $ */
4 static char *rcsid
= "Id: setenv.c,v 1.1 2003/06/04 00:27:01 marka Exp ";
8 * Copyright (c) 2002 Japan Network Information Center.
11 * By using this file, you agree to the terms and conditions set forth bellow.
13 * LICENSE TERMS AND CONDITIONS
15 * The following License Terms and Conditions apply, unless a different
16 * license is obtained from Japan Network Information Center ("JPNIC"),
17 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18 * Chiyoda-ku, Tokyo 101-0047, Japan.
20 * 1. Use, Modification and Redistribution (including distribution of any
21 * modified or derived work) in source and/or binary forms is permitted
22 * under this License Terms and Conditions.
24 * 2. Redistribution of source code must retain the copyright notices as they
25 * appear in each source code file, this License Terms and Conditions.
27 * 3. Redistribution in binary form must reproduce the Copyright Notice,
28 * this License Terms and Conditions, in the documentation and/or other
29 * materials provided with the distribution. For the purposes of binary
30 * distribution the "Copyright Notice" refers to the following language:
31 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
33 * 4. The name of JPNIC may not be used to endorse or promote products
34 * derived from this Software without specific prior written approval of
37 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
54 * We don't include <stdlib.h> here.
55 * Also <stdlib.h> may declare `environ' and its type might be different
58 extern char **environ
;
60 typedef struct myenv myenv_t
;
68 static myenv_t
*myenvs
= NULL
;
71 myunsetenv(const char *name
) {
75 extern void free(void *);
77 namelen
= strlen(name
);
78 for (e
= environ
; *e
!= NULL
; e
++) {
79 if (strncmp(*e
, name
, namelen
) == 0 && (*e
)[namelen
] == '=')
85 for (mye
= myenvs
; mye
!= NULL
; mye
= mye
->next
) {
86 if (mye
->pointer
== *e
) {
87 if (mye
->next
!= NULL
)
88 mye
->next
->prev
= mye
->prev
;
89 if (mye
->prev
!= NULL
)
90 mye
->prev
->next
= mye
->next
;
91 if (mye
->next
== NULL
&& mye
->prev
== NULL
)
99 for ( ; *e
!= NULL
; e
++)
106 mysetenv(const char *name
, const char *value
, int overwrite
) {
111 if (getenv(name
) != NULL
&& !overwrite
)
114 buffer
= (char *) malloc(strlen(name
) + strlen(value
) + 2);
117 strcpy(buffer
, name
);
119 strcat(buffer
, value
);
123 mye
= (myenv_t
*) malloc(sizeof(myenv_t
));
126 mye
->pointer
= buffer
;
133 result
= putenv(buffer
);