Makefile.inc1: don't build aicasm
[dragonfly.git] / contrib / opie / libmissing / env.c
blob9a445a0849319b291ffd0f23048a92674494673b
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
6 the software.
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.
15 History:
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.
21 Originally from BSD.
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.
40 #include "opie_cfg.h"
41 #include <stdio.h>
42 #if HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif /* HAVE_UNISTD_H */
45 #if HAVE_STDLIB_H
46 #include <stdlib.h>
47 #endif /* HAVE_STDLIB_H */
48 #include "opie.h"
50 static char *_findenv FUNCTION((name, offset), register char *name AND int *offset)
52 extern char **environ;
53 register int len;
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;
61 return (++C);
63 return (NULL);
66 #if !HAVE_GETENV
67 char *getenv FUNCTION((name), char *name)
69 int offset;
70 char *_findenv();
72 return (_findenv(name, &offset));
74 #endif /* !HAVE_GETENV */
76 #if !HAVE_SETENV
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 */
81 register char *C;
82 int l_value, offset;
84 if (*value == '=') /* no `=' in value */
85 ++value;
86 l_value = strlen(value);
87 if ((C = _findenv(name, &offset))) { /* find if already exists */
88 if (!rewrite)
89 return (0);
90 if (strlen(C) >= l_value) { /* old larger; copy over */
91 while (*C++ = *value++);
92 return (0);
94 } else { /* create new slot */
95 register int cnt;
96 register char **P;
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)));
103 if (!environ)
104 return (-1);
105 } else { /* get new space */
106 alloced = 1; /* copy old entries into it */
107 P = (char **) malloc((u_int) (sizeof(char *) *
108 (cnt + 2)));
110 if (!P)
111 return (-1);
112 strncpy(P, environ, cnt * sizeof(char *));
114 environ = P;
116 environ[cnt + 1] = NULL;
117 offset = cnt;
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))))
122 return (-1);
123 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
124 for (*C++ = '='; *C++ = *value++;);
125 return (0);
127 #endif /* !HAVE_SETENV */
129 #if !HAVE_UNSETENV
130 VOIDRET unsetenv FUNCTION((name), char *name)
132 extern char **environ;
133 register char **P;
134 int offset;
136 while (_findenv(name, &offset)) /* if set multiple times */
137 for (P = &environ[offset];; ++P)
138 if (!(*P = *(P + 1)))
139 break;
141 #endif /* !HAVE_UNSETENV */