etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / lib / tests / setenv.c
blob1c79079213b6270f3fcf9ae2cc4f15362afffa92
1 /* $NetBSD: setenv.c,v 1.4 2014/12/10 04:37:56 christos Exp $ */
3 #ifndef lint
4 static char *rcsid = "Id: setenv.c,v 1.1 2003/06/04 00:27:01 marka Exp ";
5 #endif
7 /*
8 * Copyright (c) 2002 Japan Network Information Center.
9 * All rights reserved.
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
35 * JPNIC.
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.
50 #include <stddef.h>
51 #include <string.h>
54 * We don't include <stdlib.h> here.
55 * Also <stdlib.h> may declare `environ' and its type might be different
56 * from ours.
58 extern char **environ;
60 typedef struct myenv myenv_t;
62 struct myenv {
63 char *pointer;
64 myenv_t *next;
65 myenv_t *prev;
68 static myenv_t *myenvs = NULL;
70 void
71 myunsetenv(const char *name) {
72 char **e;
73 myenv_t *mye;
74 size_t namelen;
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] == '=')
80 break;
82 if (*e == NULL)
83 return;
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)
92 myenvs = NULL;
93 free(mye);
94 free(*e);
95 break;
99 for ( ; *e != NULL; e++)
100 *e = *(e + 1);
103 #include <stdlib.h>
106 mysetenv(const char *name, const char *value, int overwrite) {
107 myenv_t *mye;
108 char *buffer;
109 int result;
111 if (getenv(name) != NULL && !overwrite)
112 return 0;
114 buffer = (char *) malloc(strlen(name) + strlen(value) + 2);
115 if (buffer == NULL)
116 return -1;
117 strcpy(buffer, name);
118 strcat(buffer, "=");
119 strcat(buffer, value);
121 myunsetenv(name);
123 mye = (myenv_t *) malloc(sizeof(myenv_t));
124 if (mye == NULL)
125 return -1;
126 mye->pointer = buffer;
127 mye->next = myenvs;
128 mye->prev = NULL;
129 if (myenvs != NULL)
130 myenvs->prev = mye;
131 myenvs = mye;
133 result = putenv(buffer);
135 return result;