xref: /netbsd-src/external/bsd/cron/dist/env.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: env.c,v 1.2 2010/05/06 18:53:17 christos Exp $	*/
2 
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4  * All rights reserved
5  */
6 
7 /*
8  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23 #include <sys/cdefs.h>
24 #if !defined(lint) && !defined(LINT)
25 #if 0
26 static char rcsid[] = "Id: env.c,v 1.10 2004/01/23 18:56:42 vixie Exp";
27 #else
28 __RCSID("$NetBSD: env.c,v 1.2 2010/05/06 18:53:17 christos Exp $");
29 #endif
30 #endif
31 
32 #include "cron.h"
33 
34 char **
35 env_init(void) {
36 	char **p = malloc(sizeof(char **));
37 
38 	if (p != NULL)
39 		p[0] = NULL;
40 	return (p);
41 }
42 
43 void
44 env_free(char **envp) {
45 	char **p;
46 
47 	for (p = envp; *p != NULL; p++)
48 		free(*p);
49 	free(envp);
50 }
51 
52 char **
53 env_copy(char **envp) {
54 	int count, i, save_errno;
55 	char **p;
56 
57 	for (count = 0; envp[count] != NULL; count++)
58 		continue;
59 	p = malloc((count+1) * sizeof(*p));  /* 1 for the NULL */
60 	if (p != NULL) {
61 		for (i = 0; i < count; i++)
62 			if ((p[i] = strdup(envp[i])) == NULL) {
63 				save_errno = errno;
64 				while (--i >= 0)
65 					free(p[i]);
66 				free(p);
67 				errno = save_errno;
68 				return (NULL);
69 			}
70 		p[count] = NULL;
71 	}
72 	return (p);
73 }
74 
75 char **
76 env_set(char **envp, char *envstr) {
77 	int count, found;
78 	char **p, *envtmp;
79 
80 	/*
81 	 * count the number of elements, including the null pointer;
82 	 * also set 'found' to -1 or index of entry if already in here.
83 	 */
84 	found = -1;
85 	for (count = 0; envp[count] != NULL; count++) {
86 		if (!strcmp_until(envp[count], envstr, '='))
87 			found = count;
88 	}
89 	count++;	/* for the NULL */
90 
91 	if (found != -1) {
92 		/*
93 		 * it exists already, so just free the existing setting,
94 		 * save our new one there, and return the existing array.
95 		 */
96 		if ((envtmp = strdup(envstr)) == NULL)
97 			return (NULL);
98 		free(envp[found]);
99 		envp[found] = envtmp;
100 		return (envp);
101 	}
102 
103 	/*
104 	 * it doesn't exist yet, so resize the array, move null pointer over
105 	 * one, save our string over the old null pointer, and return resized
106 	 * array.
107 	 */
108 	if ((envtmp = strdup(envstr)) == NULL)
109 		return (NULL);
110 	p = realloc(envp, (size_t) ((count+1) * sizeof(*p)));
111 	if (p == NULL) {
112 		free(envtmp);
113 		return (NULL);
114 	}
115 	p[count] = p[count-1];
116 	p[count-1] = envtmp;
117 	return (p);
118 }
119 
120 /* The following states are used by load_env(), traversed in order: */
121 enum env_state {
122 	NAMEI,		/* First char of NAME, may be quote */
123 	NAME,		/* Subsequent chars of NAME */
124 	EQ1,		/* After end of name, looking for '=' sign */
125 	EQ2,		/* After '=', skipping whitespace */
126 	VALUEI,		/* First char of VALUE, may be quote */
127 	VALUE,		/* Subsequent chars of VALUE */
128 	FINI,		/* All done, skipping trailing whitespace */
129 	ERROR		/* Error */
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
137 load_env(char *envstr, FILE *f) {
138 	long filepos;
139 	int fileline;
140 	enum env_state state;
141 	char name[MAX_ENVSTR], val[MAX_ENVSTR];
142 	char quotechar, *c, *str;
143 
144 	filepos = ftell(f);
145 	fileline = LineNumber;
146 	skip_comments(f);
147 	if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
148 		return (ERR);
149 
150 	Debug(DPARS, ("load_env, read <%s>\n", envstr));
151 
152 	(void)memset(name, 0, sizeof name);
153 	(void)memset(val, 0, sizeof val);
154 	str = name;
155 	state = NAMEI;
156 	quotechar = '\0';
157 	c = envstr;
158 	while (state != ERROR && *c) {
159 		switch (state) {
160 		case NAMEI:
161 		case VALUEI:
162 			if (*c == '\'' || *c == '"')
163 				quotechar = *c++;
164 			state++;
165 			/* FALLTHROUGH */
166 		case NAME:
167 		case VALUE:
168 			if (quotechar) {
169 				if (*c == quotechar) {
170 					state++;
171 					c++;
172 					break;
173 				}
174 				if (state == NAME && *c == '=') {
175 					state = ERROR;
176 					break;
177 				}
178 			} else {
179 				if (state == NAME) {
180 					if (isspace((unsigned char)*c)) {
181 						c++;
182 						state++;
183 						break;
184 					}
185 					if (*c == '=') {
186 						state++;
187 						break;
188 					}
189 				}
190 			}
191 			*str++ = *c++;
192 			break;
193 
194 		case EQ1:
195 			if (*c == '=') {
196 				state++;
197 				str = val;
198 				quotechar = '\0';
199 			} else {
200 				if (!isspace((unsigned char)*c))
201 					state = ERROR;
202 			}
203 			c++;
204 			break;
205 
206 		case EQ2:
207 		case FINI:
208 			if (isspace((unsigned char)*c))
209 				c++;
210 			else
211 				state++;
212 			break;
213 
214 		default:
215 			abort();
216 		}
217 	}
218 	if (state != FINI && !(state == VALUE && !quotechar)) {
219 		Debug(DPARS, ("load_env, not an env var, state = %d\n", state));
220 		(void)fseek(f, filepos, 0);
221 		Set_LineNum(fileline);
222 		return (FALSE);
223 	}
224 	if (state == VALUE) {
225 		/* End of unquoted value: trim trailing whitespace */
226 		c = val + strlen(val);
227 		while (c > val && isspace((unsigned char)c[-1]))
228 			*(--c) = '\0';
229 	}
230 
231 	/* 2 fields from parser; looks like an env setting */
232 
233 	/*
234 	 * This can't overflow because get_string() limited the size of the
235 	 * name and val fields.  Still, it doesn't hurt to be careful...
236 	 */
237 	if (!glue_strings(envstr, MAX_ENVSTR, name, val, '='))
238 		return (FALSE);
239 	Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr));
240 	return (TRUE);
241 }
242 
243 char *
244 env_get(const char *name, char **envp) {
245 	size_t len = strlen(name);
246 	char *p, *q;
247 
248 	while ((p = *envp++) != NULL) {
249 		if (!(q = strchr(p, '=')))
250 			continue;
251 		if ((size_t)(q - p) == len && !strncmp(p, name, len))
252 			return (q+1);
253 	}
254 	return (NULL);
255 }
256