1 /* $NetBSD: env.c,v 1.14 2005/06/05 19:08:28 chs Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
6 * Distribute freely, except: don't remove my name from the source or
7 * documentation (don't take credit for my work), mark your changes (don't
8 * get me blamed for your possible bugs), don't alter or remove this
9 * notice. May be sold if buildable source is provided to buyer. No
10 * warrantee of any kind, express or implied, is included with this
11 * software; use at your own risk, responsibility for damages (if any) to
12 * anyone resulting from the use of this software rests entirely with the
15 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
16 * I'll try to keep a version up to date. I can be reached as follows:
17 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
20 #include <sys/cdefs.h>
21 #if !defined(lint) && !defined(LINT)
23 static char rcsid
[] = "Id: env.c,v 2.7 1994/01/26 02:25:50 vixie Exp";
25 __RCSID("$NetBSD: env.c,v 1.14 2005/06/05 19:08:28 chs Exp $");
36 char **p
= (char **) malloc(sizeof(char **));
48 for (p
= envp
; *p
; p
++)
60 for (count
= 0; envp
[count
] != NULL
; count
++)
62 p
= (char **) malloc((count
+1) * sizeof(char *)); /* 1 for the NULL */
63 for (i
= 0; i
< count
; i
++)
64 p
[i
] = strdup(envp
[i
]);
71 env_set(char **envp
, char *envstr
)
77 * count the number of elements, including the null pointer;
78 * also set 'found' to -1 or index of entry if already in here.
81 for (count
= 0; envp
[count
] != NULL
; count
++) {
82 if (!strcmp_until(envp
[count
], envstr
, '='))
85 count
++; /* for the NULL */
89 * it exists already, so just free the existing setting,
90 * save our new one there, and return the existing array.
93 envp
[found
] = strdup(envstr
);
98 * it doesn't exist yet, so resize the array, move null pointer over
99 * one, save our string over the old null pointer, and return resized
102 p
= (char **) realloc((void *) envp
,
103 (unsigned) ((count
+1) * sizeof(char **)));
104 p
[count
] = p
[count
-1];
105 p
[count
-1] = strdup(envstr
);
110 /* return ERR = end of file
111 * FALSE = not an env setting (file was repositioned)
112 * TRUE = was an env setting
115 load_env(char *envstr
, FILE *f
)
119 char *name
, *name_end
, *val
, *equal
;
122 s
= name
= name_end
= NULL
; /* XXXGCC -Wuninitialized [sparc64] */
125 fileline
= LineNumber
;
127 if (EOF
== get_string(envstr
, MAX_ENVSTR
, f
, "\n"))
130 Debug(DPARS
, ("load_env, read <%s>\n", envstr
))
135 equal
= strchr(envstr
, '=');
138 * decide if this is an environment variable or not by
139 * checking for spaces in the middle of the variable name.
140 * (it could also be a crontab line of the form
141 * <min> <hour> <day> <month> <weekday> command flag=value)
143 /* space before var name */
144 for (name
= envstr
; name
< equal
&& isspace((unsigned char)*name
); name
++)
148 if (*name
== '"' || *name
== '\'') {
149 s
= strchr(name
+ 1, *name
);
151 if (!s
|| s
> equal
) {
152 Debug(DPARS
, ("load_env, didn't get valid string"));
153 fseek(f
, filepos
, 0);
154 Set_LineNum(fileline
);
159 for (s
= name
; s
< equal
&& !isspace((unsigned char)*s
); s
++)
166 /* space after var name */
167 for ( ; s
< equal
&& isspace((unsigned char)*s
); s
++)
170 * "s" should equal "equal".. otherwise, this is not an
171 * environment set command.
174 if (equal
== NULL
|| name
== name_end
|| s
!= equal
) {
175 Debug(DPARS
, ("load_env, didn't get valid string"));
176 fseek(f
, filepos
, 0);
177 Set_LineNum(fileline
);
182 * process value string
185 while (*val
&& isspace((unsigned char)*val
))
189 if (len
>= 2 && (val
[0] == '\'' || val
[0] == '"') &&
190 val
[len
-1] == val
[0]) {
197 (void) snprintf(envstr
, MAX_ENVSTR
, "%s=%s", name
, val
);
198 Debug(DPARS
, ("load_env, <%s> <%s> -> <%s>\n", name
, val
, envstr
))
204 env_get(const char *name
, char **envp
)
206 int len
= strlen(name
);
209 while ((p
= *envp
++) != NULL
) {
210 if (!(q
= strchr(p
, '=')))
212 if ((q
- p
) == len
&& !strncmp(p
, name
, len
))