2 * Copyright (c) 1987 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 #include <afsconfig.h>
19 #include <afs/param.h>
24 #include <sys/types.h>
27 extern char *malloc(), *realloc(), *_findenv();
30 * Set the value of the environmental variable "name" to be
31 * "value". If rewrite is set, replace any current value.
33 setenv(name, value, rewrite)
34 register char *name, *value;
37 extern char **environ;
38 static int alloced; /* if allocated space before */
42 if (*value == '=') /* no `=' in value */
44 l_value = strlen(value);
45 if ((C = _findenv(name, &offset))) { /* find if already exists */
48 if (strlen(C) >= l_value) { /* old larger; copy over */
49 while (*C++ = *value++);
52 } else { /* create new slot */
56 for (P = environ, cnt = 0; *P; ++P, ++cnt);
57 if (alloced) { /* just increase size */
59 (char **)realloc((char *)environ,
60 (u_int) (sizeof(char *) * (cnt + 2)));
63 } else { /* get new space */
64 alloced = 1; /* copy old entries into it */
65 P = (char **)malloc((u_int) (sizeof(char *) * (cnt + 2)));
68 memcpy(P, environ, cnt * sizeof(char *));
71 environ[cnt + 1] = NULL;
74 for (C = name; *C && *C != '='; ++C); /* no `=' in name */
75 if (!(environ[offset] = /* name + `=' + value */
76 malloc((u_int) ((int)(C - name) + l_value + 2))))
78 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
79 for (*C++ = '='; *C++ = *value++;);
85 * Delete environmental variable "name".
91 extern char **environ;
95 while (_findenv(name, &offset)) /* if set multiple times */
96 for (P = &environ[offset];; ++P)