"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
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
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)setenv.c 5.4 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 extern char *malloc();
27
28 #ifdef BSD4_2
29 #define memcpy(a,b,c) bcopy(b,a,c)
30 #endif
31
32 /*
33 * setenv --
34 * Set the value of the environmental variable "name" to be
35 * "value". If rewrite is set, replace any current value.
36 */
37 setenv(name, value, rewrite)
38 register char *name, *value;
39 int rewrite;
40 {
41 extern char **environ;
42 static int alloced; /* if allocated space before */
43 register char *C;
44 int l_value, offset;
45 char *_findenv();
46
47 if (*value == '=') /* no `=' in value */
48 ++value;
49 l_value = strlen(value);
50 if ((C = _findenv(name, &offset))) { /* find if already exists */
51 if (!rewrite)
52 return(0);
53 if (strlen(C) >= l_value) { /* old larger; copy over */
54 while (*C++ = *value++);
55 return(0);
56 }
57 }
58 else { /* create new slot */
59 register int cnt;
60 register char **P;
61
62 for (P = environ, cnt = 0; *P; ++P, ++cnt);
63 if (alloced) { /* just increase size */
64 environ = (char **)realloc((char *)environ,
65 (size_t)(sizeof(char *) * (cnt + 2)));
66 if (!environ)
67 return(-1);
68 }
69 else { /* get new space */
70 alloced = 1; /* copy old entries into it */
71 P = (char **)malloc((size_t)(sizeof(char *) *
72 (cnt + 2)));
73 if (!P)
74 return(-1);
75 memcpy(P, environ, cnt * sizeof(char *));
76 environ = P;
77 }
78 environ[cnt + 1] = NULL;
79 offset = cnt;
80 }
81 for (C = name; *C && *C != '='; ++C); /* no `=' in name */
82 if (!(environ[offset] = /* name + `=' + value */
83 malloc((size_t)((int)(C - name) + l_value + 2))))
84 return(-1);
85 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
86 for (*C++ = '='; *C++ = *value++;);
87 return(0);
88 }
89
90 /*
91 * unsetenv(name) --
92 * Delete environmental variable "name".
93 */
94 void
95 unsetenv(name)
96 char *name;
97 {
98 extern char **environ;
99 register char **P;
100 int offset;
101 char *_findenv();
102
103 while (_findenv(name, &offset)) /* if set multiple times */
104 for (P = &environ[offset];; ++P)
105 if (!(*P = *(P + 1)))
106 break;
107 }