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.5 2004/12/18 22:48:03 swildner Exp $
29 p
= malloc(sizeof(char *));
41 for (p
= envp
; *p
; p
++)
53 for (count
= 0; envp
[count
] != NULL
; count
++)
55 p
= (char **) malloc((count
+1) * sizeof(char *)); /* 1 for the NULL */
60 for (i
= 0; i
< count
; i
++)
61 if ((p
[i
] = strdup(envp
[i
])) == NULL
) {
74 env_set(char **envp
, char *envstr
)
81 * count the number of elements, including the null pointer;
82 * also set 'found' to -1 or index of entry if already in here.
85 for (count
= 0; envp
[count
] != NULL
; count
++) {
86 if (!strcmp_until(envp
[count
], envstr
, '='))
89 count
++; /* for the NULL */
93 * it exists already, so just free the existing setting,
94 * save our new one there, and return the existing array.
97 if ((envp
[found
] = strdup(envstr
)) == NULL
) {
99 /* XXX env_free(envp); */
108 * it doesn't exist yet, so resize the array, move null pointer over
109 * one, save our string over the old null pointer, and return resized
112 p
= (char **) realloc((void *) envp
,
113 (unsigned) ((count
+1) * sizeof(char *)));
115 /* XXX env_free(envp); */
119 p
[count
] = p
[count
-1];
120 if ((p
[count
-1] = strdup(envstr
)) == NULL
) {
129 /* return ERR = end of file
130 * FALSE = not an env setting (file was repositioned)
131 * TRUE = was an env setting
134 load_env(char *envstr
, FILE *f
)
138 char name
[MAX_ENVSTR
], val
[MAX_ENVSTR
];
142 fileline
= LineNumber
;
144 if (EOF
== get_string(envstr
, MAX_ENVSTR
, f
, "\n"))
147 Debug(DPARS
, ("load_env, read <%s>\n", envstr
))
149 name
[0] = val
[0] = '\0';
150 fields
= sscanf(envstr
, "%[^ =] = %[^\n#]", name
, val
);
152 Debug(DPARS
, ("load_env, not 2 fields (%d)\n", fields
))
153 fseek(f
, filepos
, 0);
154 Set_LineNum(fileline
);
158 /* 2 fields from scanf; looks like an env setting
162 * process value string
165 int len
= strdtb(val
);
168 if (val
[0] == '\'' || val
[0] == '"') {
169 if (val
[len
-1] == val
[0]) {
177 if (strlen(name
) + 1 + strlen(val
) >= MAX_ENVSTR
-1)
179 sprintf(envstr
, "%s=%s", name
, val
);
180 Debug(DPARS
, ("load_env, <%s> <%s> -> <%s>\n", name
, val
, envstr
))
186 env_get(char *name
, char **envp
)
192 while ((p
= *envp
++)) {
193 if (!(q
= strchr(p
, '=')))
195 if ((q
- p
) == len
&& !strncmp(p
, name
, len
))