|
|
1.1 root 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 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.
16: */
17:
18: #if defined(LIBC_SCCS) && !defined(lint)
19: static char sccsid[] = "@(#)setenv.c 5.2 (Berkeley) 6/27/88";
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: */
30: setenv(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: */
87: void
88: unsetenv(name)
89: char *name;
90: {
91: extern char **environ;
92: register char **P;
93: int offset;
94: char *_findenv();
95:
96: while (_findenv(name, &offset)) /* if set multiple times */
97: for (P = &environ[offset];; ++P)
98: if (!(*P = *(P + 1)))
99: break;
100: }
101: /*
102: * Copyright (c) 1987 Regents of the University of California.
103: * All rights reserved.
104: *
105: * Redistribution and use in source and binary forms are permitted
106: * provided that the above copyright notice and this paragraph are
107: * duplicated in all such forms and that any documentation,
108: * advertising materials, and other materials related to such
109: * distribution and use acknowledge that the software was developed
110: * by the University of California, Berkeley. The name of the
111: * University may not be used to endorse or promote products derived
112: * from this software without specific prior written permission.
113: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
114: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
115: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
116: */
117:
118: #if defined(LIBC_SCCS) && !defined(lint)
119: static char sccsid[] = "@(#)getenv.c 5.5 (Berkeley) 6/27/88";
120: #endif /* LIBC_SCCS and not lint */
121:
122: /*
123: * getenv --
124: * Returns ptr to value associated with name, if any, else NULL.
125: */
126: char *
127: getenv(name)
128: char *name;
129: {
130: int offset;
131: char *_findenv();
132:
133: return(_findenv(name, &offset));
134: }
135:
136: /*
137: * _findenv --
138: * Returns pointer to value associated with name, if any, else NULL.
139: * Sets offset to be the offset of the name/value combination in the
140: * environmental array, for use by setenv(3) and unsetenv(3).
141: * Explicitly removes '=' in argument name.
142: *
143: * This routine *should* be a static; don't use it.
144: */
145: char *
146: _findenv(name, offset)
147: register char *name;
148: int *offset;
149: {
150: extern char **environ;
151: register int len;
152: register char **P, *C;
153:
154: for (C = name, len = 0; *C && *C != '='; ++C, ++len);
155: for (P = environ; *P; ++P)
156: if (!strncmp(*P, name, len))
157: if (*(C = *P + len) == '=') {
158: *offset = P - environ;
159: return(++C);
160: }
161: return(NULL);
162: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.