rewrite to do wait correctly; should eventually use waitpid();
[unix-history] / usr / src / lib / libc / stdlib / setenv.c
CommitLineData
fd08e5b9
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
9e31603f
KB
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.
fd08e5b9
KB
16 */
17
18#if defined(LIBC_SCCS) && !defined(lint)
9e31603f 19static char sccsid[] = "@(#)setenv.c 5.2 (Berkeley) %G%";
fd08e5b9
KB
20#endif /* LIBC_SCCS and not lint */
21
22#include <sys/types.h>
23#include <stdio.h>
24
25/*
26 * setenv --
27 * Set the value of the environmental variable "name" to be
28 * "value". If rewrite is set, replace any current value.
29 */
30setenv(name, value, rewrite)
31 register char *name, *value;
32 int rewrite;
33{
34 extern char **environ;
35 static int alloced; /* if allocated space before */
36 register char *C;
37 int l_value, offset;
38 char *malloc(), *realloc(), *_findenv();
39
40 if (*value == '=') /* no `=' in value */
41 ++value;
42 l_value = strlen(value);
43 if ((C = _findenv(name, &offset))) { /* find if already exists */
44 if (!rewrite)
45 return(0);
46 if (strlen(C) >= l_value) { /* old larger; copy over */
47 while (*C++ = *value++);
48 return(0);
49 }
50 }
51 else { /* create new slot */
52 register int cnt;
53 register char **P;
54
55 for (P = environ, cnt = 0; *P; ++P, ++cnt);
56 if (alloced) { /* just increase size */
57 environ = (char **)realloc((char *)environ,
58 (u_int)(sizeof(char *) * (cnt + 2)));
59 if (!environ)
60 return(-1);
61 }
62 else { /* get new space */
63 alloced = 1; /* copy old entries into it */
64 P = (char **)malloc((u_int)(sizeof(char *) *
65 (cnt + 2)));
66 if (!P)
67 return(-1);
68 bcopy(environ, P, cnt * sizeof(char *));
69 environ = P;
70 }
71 environ[cnt + 1] = NULL;
72 offset = cnt;
73 }
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))))
77 return(-1);
78 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
79 for (*C++ = '='; *C++ = *value++;);
80 return(0);
81}
82
83/*
84 * unsetenv(name) --
85 * Delete environmental variable "name".
86 */
87void
88unsetenv(name)
89 char *name;
90{
91 extern char **environ;
92 register char **P;
93 int offset;
94
95 while (_findenv(name, &offset)) /* if set multiple times */
96 for (P = &environ[offset];; ++P)
97 if (!(*P = *(P + 1)))
98 break;
99}