1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
6 * Copyright (c) 1997 by Internet Software Consortium
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30 char **p = (char **) malloc(sizeof(char **));
56 for (count = 0; envp[count] != NULL; count++)
58 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
63 for (i = 0; i < count; i++)
64 if ((p[i] = strdup(envp[i])) == NULL) {
77 env_set(char **envp, char *envstr)
84 * count the number of elements, including the null pointer;
85 * also set 'found' to -1 or index of entry if already in here.
88 for (count = 0; envp[count] != NULL; count++) {
89 if (!strcmp_until(envp[count], envstr, '='))
92 count++; /* for the NULL */
96 * it exists already, so just free the existing setting,
97 * save our new one there, and return the existing array.
100 if ((envp[found] = strdup(envstr)) == NULL) {
102 /* XXX env_free(envp); */
111 * it doesn't exist yet, so resize the array, move null pointer over
112 * one, save our string over the old null pointer, and return resized
115 p = (char **) realloc((void *) envp,
116 (unsigned) ((count+1) * sizeof(char *)));
118 /* XXX env_free(envp); */
122 p[count] = p[count-1];
123 if ((p[count-1] = strdup(envstr)) == NULL) {
132 /* return ERR = end of file
133 * FALSE = not an env setting (file was repositioned)
134 * TRUE = was an env setting
137 load_env(char *envstr, FILE *f)
141 char name[MAX_ENVSTR], val[MAX_ENVSTR];
142 char quotechar, *c, *str;
145 /* The following states are traversed in order: */
146 #define NAMEI 0 /* First char of NAME, may be quote */
147 #define NAME 1 /* Subsequent chars of NAME */
148 #define EQ1 2 /* After end of name, looking for '=' sign */
149 #define EQ2 3 /* After '=', skipping whitespace */
150 #define VALUEI 4 /* First char of VALUE, may be quote */
151 #define VALUE 5 /* Subsequent chars of VALUE */
152 #define FINI 6 /* All done, skipping trailing whitespace */
153 #define ERROR 7 /* Error */
156 fileline = LineNumber;
158 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
161 Debug(DPARS, ("load_env, read <%s>\n", envstr));
163 bzero (name, sizeof name);
164 bzero (val, sizeof val);
169 while (state != ERROR && *c) {
173 if (*c == '\'' || *c == '"')
180 if (*c == quotechar) {
185 if (state == NAME && *c == '=') {
225 if (state != FINI && !(state == VALUE && !quotechar)) {
226 Debug(DPARS, ("load_env, parse error, state = %d\n", state))
227 fseek(f, filepos, 0);
228 Set_LineNum(fileline);
231 if (state == VALUE) {
232 /* End of unquoted value: trim trailing whitespace */
233 c = val + strlen (val);
234 while (c > val && isspace (*(c - 1)))
238 /* 2 fields from parser; looks like an env setting */
240 if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR)
242 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
248 env_get(char *name, char **envp)
250 int len = strlen(name);
253 while ((p = *envp++) != NULL) {
254 if (!(q = strchr(p, '=')))
256 if ((q - p) == len && !strncmp(p, name, len))