1 /* env.c: Replacement environment handling functions.
3 %%% portions-copyright-cmetz
4 Portions of this software are Copyright 1996 by Craig Metz, All Rights
5 Reserved. The Inner Net License Version 2 applies to these portions of
7 You should have received a copy of the license with this software. If
8 you didn't get a copy, you may request one from <license@inner.net>.
10 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
11 McDonald, All Rights Reserved. All Rights under this copyright are assigned
12 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
13 License Agreement applies to this software.
17 Modified by cmetz for OPIE 2.2. Changed ifdefs for libmissing.
18 Combined all env functions and made _findenv static.
19 Including headers is a good idea, though. Add more headers.
20 Modified at NRL for OPIE 2.0.
24 * Copyright (c) 1987 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms are permitted
28 * provided that the above copyright notice and this paragraph are
29 * duplicated in all such forms and that any documentation,
30 * advertising materials, and other materials related to such
31 * distribution and use acknowledge that the software was developed
32 * by the University of California, Berkeley. The name of the
33 * University may not be used to endorse or promote products derived
34 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
36 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
37 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 #endif /* HAVE_UNISTD_H */
47 #endif /* HAVE_STDLIB_H */
50 static char *_findenv FUNCTION((name, offset), register char *name AND int *offset)
52 extern char **environ;
54 register char **P, *C;
56 for (C = name, len = 0; *C && *C != '='; ++C, ++len);
57 for (P = environ; *P; ++P)
58 if (!strncmp(*P, name, len))
59 if (*(C = *P + len) == '=') {
60 *offset = P - environ;
67 char *getenv FUNCTION((name), char *name)
72 return (_findenv(name, &offset));
74 #endif /* !HAVE_GETENV */
77 int setenv FUNCTION((name, value, rewrite), char *name AND char *value AND int rewrite)
79 extern char **environ;
80 static int alloced; /* if allocated space before */
84 if (*value == '=') /* no `=' in value */
86 l_value = strlen(value);
87 if ((C = _findenv(name, &offset))) { /* find if already exists */
90 if (strlen(C) >= l_value) { /* old larger; copy over */
91 while (*C++ = *value++);
94 } else { /* create new slot */
98 for (P = environ, cnt = 0; *P; ++P, ++cnt);
99 if (alloced) { /* just increase size */
100 environ = (char **) realloc((char *) environ,
101 (u_int) (sizeof(char *) * (cnt + 2)));
105 } else { /* get new space */
106 alloced = 1; /* copy old entries into it */
107 P = (char **) malloc((u_int) (sizeof(char *) *
112 strncpy(P, environ, cnt * sizeof(char *));
116 environ[cnt + 1] = NULL;
119 for (C = name; *C && *C != '='; ++C); /* no `=' in name */
120 if (!(environ[offset] = /* name + `=' + value */
121 malloc((u_int) ((int) (C - name) + l_value + 2))))
123 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
124 for (*C++ = '='; *C++ = *value++;);
127 #endif /* !HAVE_SETENV */
130 VOIDRET unsetenv FUNCTION((name), char *name)
132 extern char **environ;
136 while (_findenv(name, &offset)) /* if set multiple times */
137 for (P = &environ[offset];; ++P)
138 if (!(*P = *(P + 1)))
141 #endif /* !HAVE_UNSETENV */