184f33deaSJordan K. Hubbard /* Copyright 1988,1990,1993,1994 by Paul Vixie
284f33deaSJordan K. Hubbard * All rights reserved
3*fe590ffeSEric van Gyzen */
4*fe590ffeSEric van Gyzen
5*fe590ffeSEric van Gyzen /*
6*fe590ffeSEric van Gyzen * Copyright (c) 1997 by Internet Software Consortium
784f33deaSJordan K. Hubbard *
8*fe590ffeSEric van Gyzen * Permission to use, copy, modify, and distribute this software for any
9*fe590ffeSEric van Gyzen * purpose with or without fee is hereby granted, provided that the above
10*fe590ffeSEric van Gyzen * copyright notice and this permission notice appear in all copies.
1184f33deaSJordan K. Hubbard *
12*fe590ffeSEric van Gyzen * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13*fe590ffeSEric van Gyzen * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14*fe590ffeSEric van Gyzen * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15*fe590ffeSEric van Gyzen * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16*fe590ffeSEric van Gyzen * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17*fe590ffeSEric van Gyzen * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18*fe590ffeSEric van Gyzen * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19*fe590ffeSEric van Gyzen * SOFTWARE.
2084f33deaSJordan K. Hubbard */
2184f33deaSJordan K. Hubbard
2284f33deaSJordan K. Hubbard
2384f33deaSJordan K. Hubbard
2484f33deaSJordan K. Hubbard #include "cron.h"
2584f33deaSJordan K. Hubbard
2684f33deaSJordan K. Hubbard
2784f33deaSJordan K. Hubbard char **
env_init(void)28e93f27e3SJohn Baldwin env_init(void)
2984f33deaSJordan K. Hubbard {
30*fe590ffeSEric van Gyzen char **p = (char **) malloc(sizeof(char **));
3184f33deaSJordan K. Hubbard
32bdddbd2fSPaul Traina if (p)
3384f33deaSJordan K. Hubbard p[0] = NULL;
3484f33deaSJordan K. Hubbard return (p);
3584f33deaSJordan K. Hubbard }
3684f33deaSJordan K. Hubbard
3784f33deaSJordan K. Hubbard
3884f33deaSJordan K. Hubbard void
env_free(char ** envp)39e93f27e3SJohn Baldwin env_free(char **envp)
4084f33deaSJordan K. Hubbard {
4184f33deaSJordan K. Hubbard char **p;
4284f33deaSJordan K. Hubbard
434a9e66b5SDavid Nugent if ((p = envp))
444a9e66b5SDavid Nugent for (; *p; p++)
4584f33deaSJordan K. Hubbard free(*p);
4684f33deaSJordan K. Hubbard free(envp);
4784f33deaSJordan K. Hubbard }
4884f33deaSJordan K. Hubbard
4984f33deaSJordan K. Hubbard
5084f33deaSJordan K. Hubbard char **
env_copy(char ** envp)51e93f27e3SJohn Baldwin env_copy(char **envp)
5284f33deaSJordan K. Hubbard {
53*fe590ffeSEric van Gyzen int count, i;
54*fe590ffeSEric van Gyzen char **p;
5584f33deaSJordan K. Hubbard
5684f33deaSJordan K. Hubbard for (count = 0; envp[count] != NULL; count++)
5784f33deaSJordan K. Hubbard ;
58d02e530bSGuy Helmer p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
59bdddbd2fSPaul Traina if (p == NULL) {
60bdddbd2fSPaul Traina errno = ENOMEM;
61bdddbd2fSPaul Traina return NULL;
62bdddbd2fSPaul Traina }
6384f33deaSJordan K. Hubbard for (i = 0; i < count; i++)
64bdddbd2fSPaul Traina if ((p[i] = strdup(envp[i])) == NULL) {
65bdddbd2fSPaul Traina while (--i >= 0)
66bdddbd2fSPaul Traina (void) free(p[i]);
67bdddbd2fSPaul Traina free(p);
68bdddbd2fSPaul Traina errno = ENOMEM;
69bdddbd2fSPaul Traina return NULL;
70bdddbd2fSPaul Traina }
7184f33deaSJordan K. Hubbard p[count] = NULL;
7284f33deaSJordan K. Hubbard return (p);
7384f33deaSJordan K. Hubbard }
7484f33deaSJordan K. Hubbard
7584f33deaSJordan K. Hubbard
7684f33deaSJordan K. Hubbard char **
env_set(char ** envp,char * envstr)77e93f27e3SJohn Baldwin env_set(char **envp, char *envstr)
7884f33deaSJordan K. Hubbard {
79*fe590ffeSEric van Gyzen int count, found;
80*fe590ffeSEric van Gyzen char **p;
818261236dSGuy Helmer char *q;
8284f33deaSJordan K. Hubbard
8384f33deaSJordan K. Hubbard /*
8484f33deaSJordan K. Hubbard * count the number of elements, including the null pointer;
8584f33deaSJordan K. Hubbard * also set 'found' to -1 or index of entry if already in here.
8684f33deaSJordan K. Hubbard */
8784f33deaSJordan K. Hubbard found = -1;
8884f33deaSJordan K. Hubbard for (count = 0; envp[count] != NULL; count++) {
8984f33deaSJordan K. Hubbard if (!strcmp_until(envp[count], envstr, '='))
9084f33deaSJordan K. Hubbard found = count;
9184f33deaSJordan K. Hubbard }
9284f33deaSJordan K. Hubbard count++; /* for the NULL */
9384f33deaSJordan K. Hubbard
9484f33deaSJordan K. Hubbard if (found != -1) {
9584f33deaSJordan K. Hubbard /*
9684f33deaSJordan K. Hubbard * it exists already, so just free the existing setting,
9784f33deaSJordan K. Hubbard * save our new one there, and return the existing array.
9884f33deaSJordan K. Hubbard */
998261236dSGuy Helmer q = envp[found];
100bdddbd2fSPaul Traina if ((envp[found] = strdup(envstr)) == NULL) {
1018261236dSGuy Helmer envp[found] = q;
1028261236dSGuy Helmer /* XXX env_free(envp); */
103bdddbd2fSPaul Traina errno = ENOMEM;
104bdddbd2fSPaul Traina return NULL;
105bdddbd2fSPaul Traina }
1068261236dSGuy Helmer free(q);
10784f33deaSJordan K. Hubbard return (envp);
10884f33deaSJordan K. Hubbard }
10984f33deaSJordan K. Hubbard
11084f33deaSJordan K. Hubbard /*
11184f33deaSJordan K. Hubbard * it doesn't exist yet, so resize the array, move null pointer over
11284f33deaSJordan K. Hubbard * one, save our string over the old null pointer, and return resized
11384f33deaSJordan K. Hubbard * array.
11484f33deaSJordan K. Hubbard */
11584f33deaSJordan K. Hubbard p = (char **) realloc((void *) envp,
116d02e530bSGuy Helmer (unsigned) ((count+1) * sizeof(char *)));
117bdddbd2fSPaul Traina if (p == NULL) {
1188261236dSGuy Helmer /* XXX env_free(envp); */
119bdddbd2fSPaul Traina errno = ENOMEM;
120bdddbd2fSPaul Traina return NULL;
121bdddbd2fSPaul Traina }
12284f33deaSJordan K. Hubbard p[count] = p[count-1];
123bdddbd2fSPaul Traina if ((p[count-1] = strdup(envstr)) == NULL) {
1248261236dSGuy Helmer env_free(p);
125bdddbd2fSPaul Traina errno = ENOMEM;
126bdddbd2fSPaul Traina return NULL;
127bdddbd2fSPaul Traina }
12884f33deaSJordan K. Hubbard return (p);
12984f33deaSJordan K. Hubbard }
13084f33deaSJordan K. Hubbard
13184f33deaSJordan K. Hubbard
13284f33deaSJordan K. Hubbard /* return ERR = end of file
13384f33deaSJordan K. Hubbard * FALSE = not an env setting (file was repositioned)
13484f33deaSJordan K. Hubbard * TRUE = was an env setting
13584f33deaSJordan K. Hubbard */
13684f33deaSJordan K. Hubbard int
load_env(char * envstr,FILE * f)137e93f27e3SJohn Baldwin load_env(char *envstr, FILE *f)
13884f33deaSJordan K. Hubbard {
13984f33deaSJordan K. Hubbard long filepos;
14084f33deaSJordan K. Hubbard int fileline;
141482bfcccSPaul Traina char name[MAX_ENVSTR], val[MAX_ENVSTR];
142bb817201SOllivier Robert char quotechar, *c, *str;
143bb817201SOllivier Robert int state;
144bb817201SOllivier Robert
145bb817201SOllivier Robert /* The following states are traversed in order: */
146bb817201SOllivier Robert #define NAMEI 0 /* First char of NAME, may be quote */
147bb817201SOllivier Robert #define NAME 1 /* Subsequent chars of NAME */
148bb817201SOllivier Robert #define EQ1 2 /* After end of name, looking for '=' sign */
149bb817201SOllivier Robert #define EQ2 3 /* After '=', skipping whitespace */
150bb817201SOllivier Robert #define VALUEI 4 /* First char of VALUE, may be quote */
151bb817201SOllivier Robert #define VALUE 5 /* Subsequent chars of VALUE */
152bb817201SOllivier Robert #define FINI 6 /* All done, skipping trailing whitespace */
153bb817201SOllivier Robert #define ERROR 7 /* Error */
15484f33deaSJordan K. Hubbard
15584f33deaSJordan K. Hubbard filepos = ftell(f);
15684f33deaSJordan K. Hubbard fileline = LineNumber;
15784f33deaSJordan K. Hubbard skip_comments(f);
15884f33deaSJordan K. Hubbard if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
15984f33deaSJordan K. Hubbard return (ERR);
16084f33deaSJordan K. Hubbard
161bb817201SOllivier Robert Debug(DPARS, ("load_env, read <%s>\n", envstr));
16284f33deaSJordan K. Hubbard
163bb817201SOllivier Robert bzero (name, sizeof name);
164bb817201SOllivier Robert bzero (val, sizeof val);
165bb817201SOllivier Robert str = name;
166bb817201SOllivier Robert state = NAMEI;
167bb817201SOllivier Robert quotechar = '\0';
168bb817201SOllivier Robert c = envstr;
169bb817201SOllivier Robert while (state != ERROR && *c) {
170bb817201SOllivier Robert switch (state) {
171bb817201SOllivier Robert case NAMEI:
172bb817201SOllivier Robert case VALUEI:
173bb817201SOllivier Robert if (*c == '\'' || *c == '"')
174bb817201SOllivier Robert quotechar = *c++;
175bb817201SOllivier Robert ++state;
176bb817201SOllivier Robert /* FALLTHROUGH */
177bb817201SOllivier Robert case NAME:
178bb817201SOllivier Robert case VALUE:
179bb817201SOllivier Robert if (quotechar) {
180bb817201SOllivier Robert if (*c == quotechar) {
181bb817201SOllivier Robert state++;
182bb817201SOllivier Robert c++;
183bb817201SOllivier Robert break;
184bb817201SOllivier Robert }
185bb817201SOllivier Robert if (state == NAME && *c == '=') {
186bb817201SOllivier Robert state = ERROR;
187bb817201SOllivier Robert break;
188bb817201SOllivier Robert }
189bb817201SOllivier Robert } else {
1906ced08bfSThomas Quinot if (state == NAME) {
191bb817201SOllivier Robert if (isspace (*c)) {
192bb817201SOllivier Robert c++;
1936ced08bfSThomas Quinot state++;
194bb817201SOllivier Robert break;
195bb817201SOllivier Robert }
1966ced08bfSThomas Quinot if (*c == '=') {
197bb817201SOllivier Robert state++;
198bb817201SOllivier Robert break;
199bb817201SOllivier Robert }
200bb817201SOllivier Robert }
2016ced08bfSThomas Quinot }
202bb817201SOllivier Robert *str++ = *c++;
203bb817201SOllivier Robert break;
204bb817201SOllivier Robert
205bb817201SOllivier Robert case EQ1:
206bb817201SOllivier Robert if (*c == '=') {
207bb817201SOllivier Robert state++;
208bb817201SOllivier Robert str = val;
209bb817201SOllivier Robert quotechar = '\0';
210bb817201SOllivier Robert } else {
211bb817201SOllivier Robert if (!isspace (*c))
212bb817201SOllivier Robert state = ERROR;
213bb817201SOllivier Robert }
214bb817201SOllivier Robert c++;
215bb817201SOllivier Robert break;
216bb817201SOllivier Robert case EQ2:
217bb817201SOllivier Robert case FINI:
218bb817201SOllivier Robert if (isspace (*c))
219bb817201SOllivier Robert c++;
220bb817201SOllivier Robert else
221bb817201SOllivier Robert state++;
222bb817201SOllivier Robert break;
223bb817201SOllivier Robert }
224bb817201SOllivier Robert }
225bb817201SOllivier Robert if (state != FINI && !(state == VALUE && !quotechar)) {
226bb817201SOllivier Robert Debug(DPARS, ("load_env, parse error, state = %d\n", state))
22784f33deaSJordan K. Hubbard fseek(f, filepos, 0);
22884f33deaSJordan K. Hubbard Set_LineNum(fileline);
22984f33deaSJordan K. Hubbard return (FALSE);
23084f33deaSJordan K. Hubbard }
2316ced08bfSThomas Quinot if (state == VALUE) {
2326ced08bfSThomas Quinot /* End of unquoted value: trim trailing whitespace */
2336ced08bfSThomas Quinot c = val + strlen (val);
2346ced08bfSThomas Quinot while (c > val && isspace (*(c - 1)))
2356ced08bfSThomas Quinot *(--c) = '\0';
2366ced08bfSThomas Quinot }
23784f33deaSJordan K. Hubbard
2386ced08bfSThomas Quinot /* 2 fields from parser; looks like an env setting */
23984f33deaSJordan K. Hubbard
240*fe590ffeSEric van Gyzen if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR)
241bdddbd2fSPaul Traina return (FALSE);
24284f33deaSJordan K. Hubbard Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
24384f33deaSJordan K. Hubbard return (TRUE);
24484f33deaSJordan K. Hubbard }
24584f33deaSJordan K. Hubbard
24684f33deaSJordan K. Hubbard
24784f33deaSJordan K. Hubbard char *
env_get(char * name,char ** envp)248e93f27e3SJohn Baldwin env_get(char *name, char **envp)
24984f33deaSJordan K. Hubbard {
250*fe590ffeSEric van Gyzen int len = strlen(name);
251*fe590ffeSEric van Gyzen char *p, *q;
25284f33deaSJordan K. Hubbard
253*fe590ffeSEric van Gyzen while ((p = *envp++) != NULL) {
25484f33deaSJordan K. Hubbard if (!(q = strchr(p, '=')))
25584f33deaSJordan K. Hubbard continue;
25684f33deaSJordan K. Hubbard if ((q - p) == len && !strncmp(p, name, len))
25784f33deaSJordan K. Hubbard return (q+1);
25884f33deaSJordan K. Hubbard }
25984f33deaSJordan K. Hubbard return (NULL);
26084f33deaSJordan K. Hubbard }
261