xref: /freebsd/usr.sbin/cron/lib/env.c (revision 0afa8e065e14bb8fd338d75690e0238c00167d40)
1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2  * All rights reserved
3  *
4  * Distribute freely, except: don't remove my name from the source or
5  * documentation (don't take credit for my work), mark your changes (don't
6  * get me blamed for your possible bugs), don't alter or remove this
7  * notice.  May be sold if buildable source is provided to buyer.  No
8  * warrantee of any kind, express or implied, is included with this
9  * software; use at your own risk, responsibility for damages (if any) to
10  * anyone resulting from the use of this software rests entirely with the
11  * user.
12  *
13  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14  * I'll try to keep a version up to date.  I can be reached as follows:
15  * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
16  */
17 
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
20   "$FreeBSD$";
21 #endif
22 
23 
24 #include "cron.h"
25 
26 
27 char **
28 env_init()
29 {
30 	register char	**p = (char **) malloc(sizeof(char *));
31 
32 	if (p)
33 		p[0] = NULL;
34 	return (p);
35 }
36 
37 
38 void
39 env_free(envp)
40 	char	**envp;
41 {
42 	char	**p;
43 
44 	if ((p = envp))
45 	    for (;  *p;  p++)
46 		free(*p);
47 	free(envp);
48 }
49 
50 
51 char **
52 env_copy(envp)
53 	register char	**envp;
54 {
55 	register int	count, i;
56 	register char	**p;
57 
58 	for (count = 0;  envp[count] != NULL;  count++)
59 		;
60 	p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
61 	if (p == NULL) {
62 		errno = ENOMEM;
63 		return NULL;
64 	}
65 	for (i = 0;  i < count;  i++)
66 		if ((p[i] = strdup(envp[i])) == NULL) {
67 			while (--i >= 0)
68 				(void) free(p[i]);
69 			free(p);
70 			errno = ENOMEM;
71 			return NULL;
72 		}
73 	p[count] = NULL;
74 	return (p);
75 }
76 
77 
78 char **
79 env_set(envp, envstr)
80 	char	**envp;
81 	char	*envstr;
82 {
83 	register int	count, found;
84 	register char	**p;
85 	char		*q;
86 
87 	/*
88 	 * count the number of elements, including the null pointer;
89 	 * also set 'found' to -1 or index of entry if already in here.
90 	 */
91 	found = -1;
92 	for (count = 0;  envp[count] != NULL;  count++) {
93 		if (!strcmp_until(envp[count], envstr, '='))
94 			found = count;
95 	}
96 	count++;	/* for the NULL */
97 
98 	if (found != -1) {
99 		/*
100 		 * it exists already, so just free the existing setting,
101 		 * save our new one there, and return the existing array.
102 		 */
103 		q = envp[found];
104 		if ((envp[found] = strdup(envstr)) == NULL) {
105 			envp[found] = q;
106 			/* XXX env_free(envp); */
107 			errno = ENOMEM;
108 			return NULL;
109 		}
110 		free(q);
111 		return (envp);
112 	}
113 
114 	/*
115 	 * it doesn't exist yet, so resize the array, move null pointer over
116 	 * one, save our string over the old null pointer, and return resized
117 	 * array.
118 	 */
119 	p = (char **) realloc((void *) envp,
120 			      (unsigned) ((count+1) * sizeof(char *)));
121 	if (p == NULL) 	{
122 		/* XXX env_free(envp); */
123 		errno = ENOMEM;
124 		return NULL;
125 	}
126 	p[count] = p[count-1];
127 	if ((p[count-1] = strdup(envstr)) == NULL) {
128 		env_free(p);
129 		errno = ENOMEM;
130 		return NULL;
131 	}
132 	return (p);
133 }
134 
135 
136 /* return	ERR = end of file
137  *		FALSE = not an env setting (file was repositioned)
138  *		TRUE = was an env setting
139  */
140 int
141 load_env(envstr, f)
142 	char	*envstr;
143 	FILE	*f;
144 {
145 	long	filepos;
146 	int	fileline;
147 	char	name[MAX_ENVSTR], val[MAX_ENVSTR];
148 	char	quotechar, *c, *str;
149 	int	state;
150 
151 	/* The following states are traversed in order: */
152 #define NAMEI	0	/* First char of NAME, may be quote */
153 #define NAME	1	/* Subsequent chars of NAME */
154 #define EQ1	2	/* After end of name, looking for '=' sign */
155 #define EQ2	3	/* After '=', skipping whitespace */
156 #define VALUEI	4	/* First char of VALUE, may be quote */
157 #define VALUE	5	/* Subsequent chars of VALUE */
158 #define FINI	6	/* All done, skipping trailing whitespace */
159 #define ERROR	7	/* Error */
160 
161 	filepos = ftell(f);
162 	fileline = LineNumber;
163 	skip_comments(f);
164 	if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
165 		return (ERR);
166 
167 	Debug(DPARS, ("load_env, read <%s>\n", envstr));
168 
169 	bzero (name, sizeof name);
170 	bzero (val, sizeof val);
171 	str = name;
172 	state = NAMEI;
173 	quotechar = '\0';
174 	c = envstr;
175 	while (state != ERROR && *c) {
176 		switch (state) {
177 		case NAMEI:
178 		case VALUEI:
179 			if (*c == '\'' || *c == '"')
180 				quotechar = *c++;
181 			++state;
182 			/* FALLTHROUGH */
183 		case NAME:
184 		case VALUE:
185 			if (quotechar) {
186 				if (*c == quotechar) {
187 					state++;
188 					c++;
189 					break;
190 				}
191 				if (state == NAME && *c == '=') {
192 					state = ERROR;
193 					break;
194 				}
195 			} else {
196 				if (state == NAME) {
197 					if (isspace (*c)) {
198 						c++;
199 						state++;
200 						break;
201 					}
202 					if (*c == '=') {
203 						state++;
204 						break;
205 					}
206 				}
207 			}
208 			*str++ = *c++;
209 			break;
210 
211 		case EQ1:
212 			if (*c == '=') {
213 				state++;
214 				str = val;
215 				quotechar = '\0';
216 			} else {
217 				if (!isspace (*c))
218 					state = ERROR;
219 			}
220 			c++;
221 			break;
222 		case EQ2:
223 		case FINI:
224 			if (isspace (*c))
225 				c++;
226 			else
227 				state++;
228 			break;
229 		}
230 	}
231 	if (state != FINI && !(state == VALUE && !quotechar)) {
232 		Debug(DPARS, ("load_env, parse error, state = %d\n", state))
233 		fseek(f, filepos, 0);
234 		Set_LineNum(fileline);
235 		return (FALSE);
236 	}
237 	if (state == VALUE) {
238 		/* End of unquoted value: trim trailing whitespace */
239 		c = val + strlen (val);
240 		while (c > val && isspace (*(c - 1)))
241 			*(--c) = '\0';
242 	}
243 
244 	/* 2 fields from parser; looks like an env setting */
245 
246 	if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
247 		return (FALSE);
248 	(void) sprintf(envstr, "%s=%s", name, val);
249 	Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
250 	return (TRUE);
251 }
252 
253 
254 char *
255 env_get(name, envp)
256 	register char	*name;
257 	register char	**envp;
258 {
259 	register int	len = strlen(name);
260 	register char	*p, *q;
261 
262 	while ((p = *envp++)) {
263 		if (!(q = strchr(p, '=')))
264 			continue;
265 		if ((q - p) == len && !strncmp(p, name, len))
266 			return (q+1);
267 	}
268 	return (NULL);
269 }
270