1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
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
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
17 * $FreeBSD: src/usr.sbin/cron/lib/env.c,v 1.7.2.1 2000/07/01 10:35:07 ps Exp $
18 * $DragonFly: src/usr.sbin/cron/lib/env.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
27 register char **p = (char **) malloc(sizeof(char *));
41 for (p = envp; *p; p++)
51 register int count, i;
54 for (count = 0; envp[count] != NULL; count++)
56 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
61 for (i = 0; i < count; i++)
62 if ((p[i] = strdup(envp[i])) == NULL) {
79 register int count, found;
84 * count the number of elements, including the null pointer;
85 * also set 'found' to -1 or index of entry if already in here.
88 for (count = 0; envp[count] != NULL; count++) {
89 if (!strcmp_until(envp[count], envstr, '='))
92 count++; /* for the NULL */
96 * it exists already, so just free the existing setting,
97 * save our new one there, and return the existing array.
100 if ((envp[found] = strdup(envstr)) == NULL) {
102 /* XXX env_free(envp); */
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
115 p = (char **) realloc((void *) envp,
116 (unsigned) ((count+1) * sizeof(char *)));
118 /* XXX env_free(envp); */
122 p[count] = p[count-1];
123 if ((p[count-1] = strdup(envstr)) == NULL) {
132 /* return ERR = end of file
133 * FALSE = not an env setting (file was repositioned)
134 * TRUE = was an env setting
143 char name[MAX_ENVSTR], val[MAX_ENVSTR];
147 fileline = LineNumber;
149 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
152 Debug(DPARS, ("load_env, read <%s>\n", envstr))
154 name[0] = val[0] = '\0';
155 fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
157 Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields))
158 fseek(f, filepos, 0);
159 Set_LineNum(fileline);
163 /* 2 fields from scanf; looks like an env setting
167 * process value string
170 int len = strdtb(val);
173 if (val[0] == '\'' || val[0] == '"') {
174 if (val[len-1] == val[0]) {
176 (void) strcpy(val, val+1);
182 if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
184 (void) sprintf(envstr, "%s=%s", name, val);
185 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
193 register char **envp;
195 register int len = strlen(name);
196 register char *p, *q;
198 while ((p = *envp++)) {
199 if (!(q = strchr(p, '=')))
201 if ((q - p) == len && !strncmp(p, name, len))