1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 */
4
5 /*
6 * Copyright (c) 1997 by Internet Software Consortium
7 *
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.
11 *
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
19 * SOFTWARE.
20 */
21
22
23
24 #include "cron.h"
25
26
27 char **
env_init(void)28 env_init(void)
29 {
30 char **p = (char **) malloc(sizeof(char **));
31
32 if (p)
33 p[0] = NULL;
34 return (p);
35 }
36
37
38 void
env_free(char ** envp)39 env_free(char **envp)
40 {
41 char **p;
42
43 if ((p = envp))
44 for (; *p; p++)
45 free(*p);
46 free(envp);
47 }
48
49
50 char **
env_copy(char ** envp)51 env_copy(char **envp)
52 {
53 int count, i;
54 char **p;
55
56 for (count = 0; envp[count] != NULL; count++)
57 ;
58 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
59 if (p == NULL) {
60 errno = ENOMEM;
61 return NULL;
62 }
63 for (i = 0; i < count; i++)
64 if ((p[i] = strdup(envp[i])) == NULL) {
65 while (--i >= 0)
66 (void) free(p[i]);
67 free(p);
68 errno = ENOMEM;
69 return NULL;
70 }
71 p[count] = NULL;
72 return (p);
73 }
74
75
76 char **
env_set(char ** envp,char * envstr)77 env_set(char **envp, char *envstr)
78 {
79 int count, found;
80 char **p;
81 char *q;
82
83 /*
84 * count the number of elements, including the null pointer;
85 * also set 'found' to -1 or index of entry if already in here.
86 */
87 found = -1;
88 for (count = 0; envp[count] != NULL; count++) {
89 if (!strcmp_until(envp[count], envstr, '='))
90 found = count;
91 }
92 count++; /* for the NULL */
93
94 if (found != -1) {
95 /*
96 * it exists already, so just free the existing setting,
97 * save our new one there, and return the existing array.
98 */
99 q = envp[found];
100 if ((envp[found] = strdup(envstr)) == NULL) {
101 envp[found] = q;
102 /* XXX env_free(envp); */
103 errno = ENOMEM;
104 return NULL;
105 }
106 free(q);
107 return (envp);
108 }
109
110 /*
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
113 * array.
114 */
115 p = (char **) realloc((void *) envp,
116 (unsigned) ((count+1) * sizeof(char *)));
117 if (p == NULL) {
118 /* XXX env_free(envp); */
119 errno = ENOMEM;
120 return NULL;
121 }
122 p[count] = p[count-1];
123 if ((p[count-1] = strdup(envstr)) == NULL) {
124 env_free(p);
125 errno = ENOMEM;
126 return NULL;
127 }
128 return (p);
129 }
130
131
132 /* return ERR = end of file
133 * FALSE = not an env setting (file was repositioned)
134 * TRUE = was an env setting
135 */
136 int
load_env(char * envstr,FILE * f)137 load_env(char *envstr, FILE *f)
138 {
139 long filepos;
140 int fileline;
141 char name[MAX_ENVSTR], val[MAX_ENVSTR];
142 char quotechar, *c, *str;
143 int state;
144
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 */
154
155 filepos = ftell(f);
156 fileline = LineNumber;
157 skip_comments(f);
158 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
159 return (ERR);
160
161 Debug(DPARS, ("load_env, read <%s>\n", envstr));
162
163 bzero (name, sizeof name);
164 bzero (val, sizeof val);
165 str = name;
166 state = NAMEI;
167 quotechar = '\0';
168 c = envstr;
169 while (state != ERROR && *c) {
170 switch (state) {
171 case NAMEI:
172 case VALUEI:
173 if (*c == '\'' || *c == '"')
174 quotechar = *c++;
175 ++state;
176 /* FALLTHROUGH */
177 case NAME:
178 case VALUE:
179 if (quotechar) {
180 if (*c == quotechar) {
181 state++;
182 c++;
183 break;
184 }
185 if (state == NAME && *c == '=') {
186 state = ERROR;
187 break;
188 }
189 } else {
190 if (state == NAME) {
191 if (isspace (*c)) {
192 c++;
193 state++;
194 break;
195 }
196 if (*c == '=') {
197 state++;
198 break;
199 }
200 }
201 }
202 *str++ = *c++;
203 break;
204
205 case EQ1:
206 if (*c == '=') {
207 state++;
208 str = val;
209 quotechar = '\0';
210 } else {
211 if (!isspace (*c))
212 state = ERROR;
213 }
214 c++;
215 break;
216 case EQ2:
217 case FINI:
218 if (isspace (*c))
219 c++;
220 else
221 state++;
222 break;
223 }
224 }
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);
229 return (FALSE);
230 }
231 if (state == VALUE) {
232 /* End of unquoted value: trim trailing whitespace */
233 c = val + strlen (val);
234 while (c > val && isspace (*(c - 1)))
235 *(--c) = '\0';
236 }
237
238 /* 2 fields from parser; looks like an env setting */
239
240 if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR)
241 return (FALSE);
242 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
243 return (TRUE);
244 }
245
246
247 char *
env_get(char * name,char ** envp)248 env_get(char *name, char **envp)
249 {
250 int len = strlen(name);
251 char *p, *q;
252
253 while ((p = *envp++) != NULL) {
254 if (!(q = strchr(p, '=')))
255 continue;
256 if ((q - p) == len && !strncmp(p, name, len))
257 return (q+1);
258 }
259 return (NULL);
260 }
261