xref: /freebsd/usr.sbin/cron/lib/env.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
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 	int	fields;
149 
150 	filepos = ftell(f);
151 	fileline = LineNumber;
152 	skip_comments(f);
153 	if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
154 		return (ERR);
155 
156 	Debug(DPARS, ("load_env, read <%s>\n", envstr))
157 
158 	name[0] = val[0] = '\0';
159 	fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
160 	if (fields != 2) {
161 		Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields))
162 		fseek(f, filepos, 0);
163 		Set_LineNum(fileline);
164 		return (FALSE);
165 	}
166 
167 	/* 2 fields from scanf; looks like an env setting
168 	 */
169 
170 	/*
171 	 * process value string
172 	 */
173 	/*local*/{
174 		int	len = strdtb(val);
175 
176 		if (len >= 2) {
177 			if (val[0] == '\'' || val[0] == '"') {
178 				if (val[len-1] == val[0]) {
179 					val[len-1] = '\0';
180 					(void) strcpy(val, val+1);
181 				}
182 			}
183 		}
184 	}
185 
186 	if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
187 		return (FALSE);
188 	(void) sprintf(envstr, "%s=%s", name, val);
189 	Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
190 	return (TRUE);
191 }
192 
193 
194 char *
195 env_get(name, envp)
196 	register char	*name;
197 	register char	**envp;
198 {
199 	register int	len = strlen(name);
200 	register char	*p, *q;
201 
202 	while ((p = *envp++)) {
203 		if (!(q = strchr(p, '=')))
204 			continue;
205 		if ((q - p) == len && !strncmp(p, name, len))
206 			return (q+1);
207 	}
208 	return (NULL);
209 }
210