1994-08-27 13:43:04 +00:00
|
|
|
/* Copyright 1988,1990,1993,1994 by Paul Vixie
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* Distribute freely, except: don't remove my name from the source or
|
|
|
|
* documentation (don't take credit for my work), mark your changes (don't
|
|
|
|
* get me blamed for your possible bugs), don't alter or remove this
|
|
|
|
* notice. May be sold if buildable source is provided to buyer. No
|
|
|
|
* warrantee of any kind, express or implied, is included with this
|
|
|
|
* software; use at your own risk, responsibility for damages (if any) to
|
|
|
|
* anyone resulting from the use of this software rests entirely with the
|
|
|
|
* user.
|
|
|
|
*
|
|
|
|
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and
|
|
|
|
* I'll try to keep a version up to date. I can be reached as follows:
|
|
|
|
* Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(lint) && !defined(LINT)
|
1997-09-15 06:39:25 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1994-08-27 13:43:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "cron.h"
|
|
|
|
|
|
|
|
|
|
|
|
char **
|
|
|
|
env_init()
|
|
|
|
{
|
2000-04-30 15:57:00 +00:00
|
|
|
register char **p = (char **) malloc(sizeof(char *));
|
1994-08-27 13:43:04 +00:00
|
|
|
|
1996-12-17 00:55:20 +00:00
|
|
|
if (p)
|
|
|
|
p[0] = NULL;
|
1994-08-27 13:43:04 +00:00
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
env_free(envp)
|
|
|
|
char **envp;
|
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = envp; *p; p++)
|
|
|
|
free(*p);
|
|
|
|
free(envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char **
|
|
|
|
env_copy(envp)
|
|
|
|
register char **envp;
|
|
|
|
{
|
|
|
|
register int count, i;
|
|
|
|
register char **p;
|
|
|
|
|
|
|
|
for (count = 0; envp[count] != NULL; count++)
|
|
|
|
;
|
2000-04-30 15:57:00 +00:00
|
|
|
p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
|
1996-12-17 00:55:20 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
for (i = 0; i < count; i++)
|
1996-12-17 00:55:20 +00:00
|
|
|
if ((p[i] = strdup(envp[i])) == NULL) {
|
|
|
|
while (--i >= 0)
|
|
|
|
(void) free(p[i]);
|
|
|
|
free(p);
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
p[count] = NULL;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char **
|
|
|
|
env_set(envp, envstr)
|
|
|
|
char **envp;
|
|
|
|
char *envstr;
|
|
|
|
{
|
|
|
|
register int count, found;
|
|
|
|
register char **p;
|
2000-04-28 15:31:28 +00:00
|
|
|
char *q;
|
1994-08-27 13:43:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* count the number of elements, including the null pointer;
|
|
|
|
* also set 'found' to -1 or index of entry if already in here.
|
|
|
|
*/
|
|
|
|
found = -1;
|
|
|
|
for (count = 0; envp[count] != NULL; count++) {
|
|
|
|
if (!strcmp_until(envp[count], envstr, '='))
|
|
|
|
found = count;
|
|
|
|
}
|
|
|
|
count++; /* for the NULL */
|
|
|
|
|
|
|
|
if (found != -1) {
|
|
|
|
/*
|
|
|
|
* it exists already, so just free the existing setting,
|
|
|
|
* save our new one there, and return the existing array.
|
|
|
|
*/
|
2000-04-28 15:31:28 +00:00
|
|
|
q = envp[found];
|
1996-12-17 00:55:20 +00:00
|
|
|
if ((envp[found] = strdup(envstr)) == NULL) {
|
2000-04-28 15:31:28 +00:00
|
|
|
envp[found] = q;
|
|
|
|
/* XXX env_free(envp); */
|
1996-12-17 00:55:20 +00:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-04-28 15:31:28 +00:00
|
|
|
free(q);
|
1994-08-27 13:43:04 +00:00
|
|
|
return (envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* it doesn't exist yet, so resize the array, move null pointer over
|
|
|
|
* one, save our string over the old null pointer, and return resized
|
|
|
|
* array.
|
|
|
|
*/
|
|
|
|
p = (char **) realloc((void *) envp,
|
2000-04-30 15:57:00 +00:00
|
|
|
(unsigned) ((count+1) * sizeof(char *)));
|
1996-12-17 00:55:20 +00:00
|
|
|
if (p == NULL) {
|
2000-04-28 15:31:28 +00:00
|
|
|
/* XXX env_free(envp); */
|
1996-12-17 00:55:20 +00:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
p[count] = p[count-1];
|
1996-12-17 00:55:20 +00:00
|
|
|
if ((p[count-1] = strdup(envstr)) == NULL) {
|
2000-04-28 15:31:28 +00:00
|
|
|
env_free(p);
|
1996-12-17 00:55:20 +00:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* return ERR = end of file
|
|
|
|
* FALSE = not an env setting (file was repositioned)
|
|
|
|
* TRUE = was an env setting
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
load_env(envstr, f)
|
|
|
|
char *envstr;
|
|
|
|
FILE *f;
|
|
|
|
{
|
|
|
|
long filepos;
|
|
|
|
int fileline;
|
1996-12-16 18:21:00 +00:00
|
|
|
char name[MAX_ENVSTR], val[MAX_ENVSTR];
|
1994-08-27 13:43:04 +00:00
|
|
|
int fields;
|
|
|
|
|
|
|
|
filepos = ftell(f);
|
|
|
|
fileline = LineNumber;
|
|
|
|
skip_comments(f);
|
|
|
|
if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
|
|
|
|
return (ERR);
|
|
|
|
|
|
|
|
Debug(DPARS, ("load_env, read <%s>\n", envstr))
|
|
|
|
|
|
|
|
name[0] = val[0] = '\0';
|
|
|
|
fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val);
|
|
|
|
if (fields != 2) {
|
|
|
|
Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields))
|
|
|
|
fseek(f, filepos, 0);
|
|
|
|
Set_LineNum(fileline);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2 fields from scanf; looks like an env setting
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process value string
|
|
|
|
*/
|
|
|
|
/*local*/{
|
|
|
|
int len = strdtb(val);
|
|
|
|
|
|
|
|
if (len >= 2) {
|
|
|
|
if (val[0] == '\'' || val[0] == '"') {
|
|
|
|
if (val[len-1] == val[0]) {
|
|
|
|
val[len-1] = '\0';
|
|
|
|
(void) strcpy(val, val+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-12-17 00:55:20 +00:00
|
|
|
if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1)
|
|
|
|
return (FALSE);
|
1994-08-27 13:43:04 +00:00
|
|
|
(void) sprintf(envstr, "%s=%s", name, val);
|
|
|
|
Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
env_get(name, envp)
|
|
|
|
register char *name;
|
|
|
|
register char **envp;
|
|
|
|
{
|
|
|
|
register int len = strlen(name);
|
|
|
|
register char *p, *q;
|
|
|
|
|
1997-09-15 06:39:25 +00:00
|
|
|
while ((p = *envp++)) {
|
1994-08-27 13:43:04 +00:00
|
|
|
if (!(q = strchr(p, '=')))
|
|
|
|
continue;
|
|
|
|
if ((q - p) == len && !strncmp(p, name, len))
|
|
|
|
return (q+1);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|