xref: /freebsd/usr.sbin/cron/lib/env.c (revision 089104e0e01f080c9cd45dc5f34c4f636dea4ca7)
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(void)
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(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 **
51 env_copy(char **envp)
52 {
53 	register int	count, i;
54 	register 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 **
77 env_set(char **envp, char *envstr)
78 {
79 	register int	count, found;
80 	register 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
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 (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
241 		return (FALSE);
242 	(void) sprintf(envstr, "%s=%s", name, val);
243 	Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
244 	return (TRUE);
245 }
246 
247 
248 char *
249 env_get(char *name, char **envp)
250 {
251 	register int	len = strlen(name);
252 	register char	*p, *q;
253 
254 	while ((p = *envp++)) {
255 		if (!(q = strchr(p, '=')))
256 			continue;
257 		if ((q - p) == len && !strncmp(p, name, len))
258 			return (q+1);
259 	}
260 	return (NULL);
261 }
262