xref: /openbsd/usr.sbin/cron/env.c (revision f2dfb0a4)
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 char rcsid[] = "$Id: env.c,v 1.5 1998/03/30 06:59:44 deraadt Exp $";
20 #endif
21 
22 
23 #include "cron.h"
24 
25 
26 char **
27 env_init()
28 {
29 	register char	**p = (char **) malloc(sizeof(char **));
30 
31 	if (p)
32 		p[0] = NULL;
33 	return (p);
34 }
35 
36 
37 void
38 env_free(envp)
39 	char	**envp;
40 {
41 	char	**p;
42 
43 	for (p = envp;  *p;  p++)
44 		free(*p);
45 	free(envp);
46 }
47 
48 
49 char **
50 env_copy(envp)
51 	register 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(envp, envstr)
78 	char	**envp;
79 	char	*envstr;
80 {
81 	register int	count, found;
82 	register char	**p;
83 
84 	/*
85 	 * count the number of elements, including the null pointer;
86 	 * also set 'found' to -1 or index of entry if already in here.
87 	 */
88 	found = -1;
89 	for (count = 0;  envp[count] != NULL;  count++) {
90 		if (!strcmp_until(envp[count], envstr, '='))
91 			found = count;
92 	}
93 	count++;	/* for the NULL */
94 
95 	if (found != -1) {
96 		/*
97 		 * it exists already, so just free the existing setting,
98 		 * save our new one there, and return the existing array.
99 		 */
100 		free(envp[found]);
101 		if ((envp[found] = strdup(envstr)) == NULL) {
102 			envp[found] = "";
103 			errno = ENOMEM;
104 			return NULL;
105 		}
106 		return (envp);
107 	}
108 
109 	/*
110 	 * it doesn't exist yet, so resize the array, move null pointer over
111 	 * one, save our string over the old null pointer, and return resized
112 	 * array.
113 	 */
114 	p = (char **) realloc((void *) envp,
115 			      (unsigned) ((count+1) * sizeof(char **)));
116 	if (p == NULL) 	{
117 		errno = ENOMEM;
118 		return NULL;
119 	}
120 	p[count] = p[count-1];
121 	if ((p[count-1] = strdup(envstr)) == NULL) {
122 		errno = ENOMEM;
123 		return NULL;
124 	}
125 	return (p);
126 }
127 
128 
129 /* return	ERR = end of file
130  *		FALSE = not an env setting (file was repositioned)
131  *		TRUE = was an env setting
132  */
133 int
134 load_env(envstr, f)
135 	char	*envstr;
136 	FILE	*f;
137 {
138 	long	filepos;
139 	int	fileline;
140 	char	name[MAX_ENVSTR], val[MAX_ENVSTR];
141 	int	fields;
142 
143 	filepos = ftell(f);
144 	fileline = LineNumber;
145 	skip_comments(f);
146 	if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
147 		return (ERR);
148 
149 	Debug(DPARS, ("load_env, read <%s>\n", envstr))
150 
151 	name[0] = val[0] = '\0';
152 	fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
153 	if (fields != 2) {
154 		Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields))
155 		fseek(f, filepos, 0);
156 		Set_LineNum(fileline);
157 		return (FALSE);
158 	}
159 
160 	/* 2 fields from scanf; looks like an env setting
161 	 */
162 
163 	/*
164 	 * process value string
165 	 */
166 	/*local*/{
167 		int	len = strdtb(val);
168 
169 		if (len >= 2) {
170 			if (val[0] == '\'' || val[0] == '"') {
171 				if (val[len-1] == val[0]) {
172 					val[len-1] = '\0';
173 					(void) strcpy(val, val+1);
174 				}
175 			}
176 		}
177 	}
178 
179 	if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
180 		return (FALSE);
181 	(void) sprintf(envstr, "%s=%s", name, val);
182 	Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
183 	return (TRUE);
184 }
185 
186 
187 char *
188 env_get(name, envp)
189 	register char	*name;
190 	register char	**envp;
191 {
192 	register int	len = strlen(name);
193 	register char	*p, *q;
194 
195 	while ((p = *envp++) != NULL) {
196 		if (!(q = strchr(p, '=')))
197 			continue;
198 		if ((q - p) == len && !strncmp(p, name, len))
199 			return (q+1);
200 	}
201 	return (NULL);
202 }
203