env.c (6ced08bfa6bc773baae5d299e6161972f9a33fef) env.c (84f33dea627930e57209583fc83ffb3202babc0a)
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)
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)
19static const char rcsid[] =
20 "$FreeBSD$";
19static char rcsid[] = "$Id: env.c,v 2.6 1994/01/15 20:43:43 vixie Exp $";
21#endif
22
23
24#include "cron.h"
25
26
27char **
28env_init()
29{
20#endif
21
22
23#include "cron.h"
24
25
26char **
27env_init()
28{
30 register char **p = (char **) malloc(sizeof(char *));
29 register char **p = (char **) malloc(sizeof(char **));
31
30
32 if (p)
33 p[0] = NULL;
31 p[0] = NULL;
34 return (p);
35}
36
37
38void
39env_free(envp)
40 char **envp;
41{
42 char **p;
43
32 return (p);
33}
34
35
36void
37env_free(envp)
38 char **envp;
39{
40 char **p;
41
44 if ((p = envp))
45 for (; *p; p++)
42 for (p = envp; *p; p++)
46 free(*p);
47 free(envp);
48}
49
50
51char **
52env_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 ;
43 free(*p);
44 free(envp);
45}
46
47
48char **
49env_copy(envp)
50 register char **envp;
51{
52 register int count, i;
53 register char **p;
54
55 for (count = 0; envp[count] != NULL; count++)
56 ;
60 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
61 if (p == NULL) {
62 errno = ENOMEM;
63 return NULL;
64 }
57 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
65 for (i = 0; i < count; i++)
58 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 }
59 p[i] = strdup(envp[i]);
73 p[count] = NULL;
74 return (p);
75}
76
77
78char **
79env_set(envp, envstr)
80 char **envp;
81 char *envstr;
82{
83 register int count, found;
84 register char **p;
60 p[count] = NULL;
61 return (p);
62}
63
64
65char **
66env_set(envp, envstr)
67 char **envp;
68 char *envstr;
69{
70 register int count, found;
71 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 */
72
73 /*
74 * count the number of elements, including the null pointer;
75 * also set 'found' to -1 or index of entry if already in here.
76 */
77 found = -1;
78 for (count = 0; envp[count] != NULL; count++) {
79 if (!strcmp_until(envp[count], envstr, '='))
80 found = count;
81 }
82 count++; /* for the NULL */
83
84 if (found != -1) {
85 /*
86 * it exists already, so just free the existing setting,
87 * save our new one there, and return the existing array.
88 */
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);
89 free(envp[found]);
90 envp[found] = strdup(envstr);
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,
91 return (envp);
92 }
93
94 /*
95 * it doesn't exist yet, so resize the array, move null pointer over
96 * one, save our string over the old null pointer, and return resized
97 * array.
98 */
99 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 }
100 (unsigned) ((count+1) * sizeof(char **)));
126 p[count] = p[count-1];
101 p[count] = p[count-1];
127 if ((p[count-1] = strdup(envstr)) == NULL) {
128 env_free(p);
129 errno = ENOMEM;
130 return NULL;
131 }
102 p[count-1] = strdup(envstr);
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 */
140int
141load_env(envstr, f)
142 char *envstr;
143 FILE *f;
144{
145 long filepos;
146 int fileline;
103 return (p);
104}
105
106
107/* return ERR = end of file
108 * FALSE = not an env setting (file was repositioned)
109 * TRUE = was an env setting
110 */
111int
112load_env(envstr, f)
113 char *envstr;
114 FILE *f;
115{
116 long filepos;
117 int fileline;
147 char name[MAX_ENVSTR], val[MAX_ENVSTR];
148 char quotechar, *c, *str;
149 int state;
118 char name[MAX_TEMPSTR], val[MAX_ENVSTR];
119 int fields;
150
120
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
121 filepos = ftell(f);
122 fileline = LineNumber;
123 skip_comments(f);
124 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
125 return (ERR);
126
167 Debug(DPARS, ("load_env, read <%s>\n", envstr));
127 Debug(DPARS, ("load_env, read <%s>\n", envstr))
168
128
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))
129 name[0] = val[0] = '\0';
130 fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
131 if (fields != 2) {
132 Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields))
233 fseek(f, filepos, 0);
234 Set_LineNum(fileline);
235 return (FALSE);
236 }
133 fseek(f, filepos, 0);
134 Set_LineNum(fileline);
135 return (FALSE);
136 }
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
137
244 /* 2 fields from parser; looks like an env setting */
138 /* 2 fields from scanf; looks like an env setting
139 */
245
140
246 if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
247 return (FALSE);
141 /*
142 * process value string
143 */
144 /*local*/{
145 int len = strdtb(val);
146
147 if (len >= 2) {
148 if (val[0] == '\'' || val[0] == '"') {
149 if (val[len-1] == val[0]) {
150 val[len-1] = '\0';
151 (void) strcpy(val, val+1);
152 }
153 }
154 }
155 }
156
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
254char *
255env_get(name, envp)
256 register char *name;
257 register char **envp;
258{
259 register int len = strlen(name);
260 register char *p, *q;
261
157 (void) sprintf(envstr, "%s=%s", name, val);
158 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
159 return (TRUE);
160}
161
162
163char *
164env_get(name, envp)
165 register char *name;
166 register char **envp;
167{
168 register int len = strlen(name);
169 register char *p, *q;
170
262 while ((p = *envp++)) {
171 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}
172 if (!(q = strchr(p, '=')))
173 continue;
174 if ((q - p) == len && !strncmp(p, name, len))
175 return (q+1);
176 }
177 return (NULL);
178}