1999-03-23 01:18:45 +03:00
|
|
|
/* $NetBSD: env.c,v 1.11 1999/03/22 22:18:45 aidan Exp $ */
|
1997-03-13 09:19:07 +03:00
|
|
|
|
1994-01-12 21:35:59 +03:00
|
|
|
/* Copyright 1988,1990,1993,1994 by Paul Vixie
|
1994-01-05 23:40:12 +03:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
1998-01-31 17:40:13 +03:00
|
|
|
#include <sys/cdefs.h>
|
1994-01-05 23:40:12 +03:00
|
|
|
#if !defined(lint) && !defined(LINT)
|
1998-01-31 17:40:13 +03:00
|
|
|
#if 0
|
|
|
|
static char rcsid[] = "Id: env.c,v 2.7 1994/01/26 02:25:50 vixie Exp";
|
|
|
|
#else
|
1999-03-23 01:18:45 +03:00
|
|
|
__RCSID("$NetBSD: env.c,v 1.11 1999/03/22 22:18:45 aidan Exp $");
|
1998-01-31 17:40:13 +03:00
|
|
|
#endif
|
1994-01-05 23:40:12 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "cron.h"
|
1997-04-14 12:18:36 +04:00
|
|
|
#include <string.h>
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
char **
|
|
|
|
env_init()
|
|
|
|
{
|
1998-01-31 17:40:13 +03:00
|
|
|
char **p = (char **) malloc(sizeof(char **));
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
p[0] = NULL;
|
1994-01-26 22:09:20 +03:00
|
|
|
return (p);
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-01-11 22:10:45 +03:00
|
|
|
void
|
|
|
|
env_free(envp)
|
|
|
|
char **envp;
|
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = envp; *p; p++)
|
|
|
|
free(*p);
|
|
|
|
free(envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char **
|
|
|
|
env_copy(envp)
|
1998-01-31 17:40:13 +03:00
|
|
|
char **envp;
|
1994-01-11 22:10:45 +03:00
|
|
|
{
|
1998-01-31 17:40:13 +03:00
|
|
|
int count, i;
|
|
|
|
char **p;
|
1994-01-11 22:10:45 +03:00
|
|
|
|
|
|
|
for (count = 0; envp[count] != NULL; count++)
|
|
|
|
;
|
|
|
|
p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
p[i] = strdup(envp[i]);
|
|
|
|
p[count] = NULL;
|
1994-01-26 22:09:20 +03:00
|
|
|
return (p);
|
1994-01-11 22:10:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-01-05 23:40:12 +03:00
|
|
|
char **
|
|
|
|
env_set(envp, envstr)
|
|
|
|
char **envp;
|
|
|
|
char *envstr;
|
|
|
|
{
|
1998-01-31 17:40:13 +03:00
|
|
|
int count, found;
|
|
|
|
char **p;
|
1994-01-05 23:40:12 +03: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;
|
|
|
|
}
|
1994-01-11 22:10:45 +03:00
|
|
|
count++; /* for the NULL */
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
if (found != -1) {
|
|
|
|
/*
|
|
|
|
* it exists already, so just free the existing setting,
|
|
|
|
* save our new one there, and return the existing array.
|
|
|
|
*/
|
|
|
|
free(envp[found]);
|
|
|
|
envp[found] = strdup(envstr);
|
1994-01-26 22:09:20 +03:00
|
|
|
return (envp);
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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,
|
|
|
|
(unsigned) ((count+1) * sizeof(char **)));
|
|
|
|
p[count] = p[count-1];
|
|
|
|
p[count-1] = strdup(envstr);
|
1994-01-26 22:09:20 +03:00
|
|
|
return (p);
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
1997-04-14 12:18:36 +04:00
|
|
|
int fileline, len;
|
1999-03-23 01:18:45 +03:00
|
|
|
char *name, *name_end, *val, *equal;
|
|
|
|
char *s;
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
filepos = ftell(f);
|
|
|
|
fileline = LineNumber;
|
|
|
|
skip_comments(f);
|
|
|
|
if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
|
1994-01-26 22:09:20 +03:00
|
|
|
return (ERR);
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
Debug(DPARS, ("load_env, read <%s>\n", envstr))
|
|
|
|
|
1999-03-23 01:18:45 +03:00
|
|
|
equal = strchr(envstr, '=');
|
|
|
|
if (equal) {
|
1998-10-08 03:18:29 +04:00
|
|
|
/*
|
|
|
|
* decide if this is an environment variable or not by
|
|
|
|
* checking for spaces in the middle of the variable name.
|
|
|
|
* (it could also be a crontab line of the form
|
|
|
|
* <min> <hour> <day> <month> <weekday> command flag=value)
|
|
|
|
*/
|
|
|
|
/* space before var name */
|
1999-03-23 01:18:45 +03:00
|
|
|
for (name = envstr; name < equal && isspace(*name); name++)
|
1998-10-08 03:18:29 +04:00
|
|
|
;
|
1999-03-23 01:18:45 +03:00
|
|
|
|
1998-10-08 03:18:29 +04:00
|
|
|
/* var name */
|
1999-03-23 01:18:45 +03:00
|
|
|
if (*name == '"' || *name == '\'') {
|
|
|
|
s = strchr(name + 1, *name);
|
|
|
|
name++;
|
|
|
|
if (!s || s > equal) {
|
|
|
|
Debug(DPARS, ("load_env, didn't get valid string"));
|
|
|
|
fseek(f, filepos, 0);
|
|
|
|
Set_LineNum(fileline);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
name_end = s++;
|
|
|
|
} else {
|
|
|
|
for (s = name ; s < equal && !isspace(*s); s++)
|
|
|
|
;
|
|
|
|
name_end = s;
|
|
|
|
if (s < equal)
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
1998-10-08 03:18:29 +04:00
|
|
|
/* space after var name */
|
1999-03-23 01:18:45 +03:00
|
|
|
for ( ; s < equal && isspace(*s); s++)
|
1998-10-08 03:18:29 +04:00
|
|
|
;
|
|
|
|
/*
|
1999-03-23 01:18:45 +03:00
|
|
|
* "s" should equal "equal".. otherwise, this is not an
|
1998-10-08 03:18:29 +04:00
|
|
|
* environment set command.
|
|
|
|
*/
|
|
|
|
}
|
1999-03-23 01:18:45 +03:00
|
|
|
if (equal == NULL || name == name_end || s != equal) {
|
1997-04-14 12:18:36 +04:00
|
|
|
Debug(DPARS, ("load_env, didn't get valid string"));
|
1994-01-05 23:40:12 +03:00
|
|
|
fseek(f, filepos, 0);
|
|
|
|
Set_LineNum(fileline);
|
1994-01-26 22:09:20 +03:00
|
|
|
return (FALSE);
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process value string
|
|
|
|
*/
|
1999-03-23 01:18:45 +03:00
|
|
|
val = equal + 1;
|
|
|
|
while (*val && isspace(*val))
|
|
|
|
val++;
|
1997-04-14 12:18:36 +04:00
|
|
|
if (*val) {
|
|
|
|
len = strdtb(val);
|
|
|
|
if (len >= 2 && (val[0] == '\'' || val[0] == '"') &&
|
|
|
|
val[len-1] == val[0]) {
|
|
|
|
val[len-1] = '\0';
|
|
|
|
val++;
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-23 01:18:45 +03:00
|
|
|
*name_end = '\0';
|
1996-12-17 01:30:43 +03:00
|
|
|
(void) snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val);
|
1994-01-05 23:40:12 +03:00
|
|
|
Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr))
|
1994-01-26 22:09:20 +03:00
|
|
|
return (TRUE);
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
env_get(name, envp)
|
1998-01-31 17:40:13 +03:00
|
|
|
char *name;
|
|
|
|
char **envp;
|
1994-01-05 23:40:12 +03:00
|
|
|
{
|
1998-01-31 17:40:13 +03:00
|
|
|
int len = strlen(name);
|
|
|
|
char *p, *q;
|
1994-01-26 22:09:20 +03:00
|
|
|
|
1998-01-31 17:40:13 +03:00
|
|
|
while ((p = *envp++) != NULL) {
|
1994-01-26 22:09:20 +03:00
|
|
|
if (!(q = strchr(p, '=')))
|
|
|
|
continue;
|
|
|
|
if ((q - p) == len && !strncmp(p, name, len))
|
|
|
|
return (q+1);
|
1994-01-11 22:10:45 +03:00
|
|
|
}
|
1994-01-26 22:09:20 +03:00
|
|
|
return (NULL);
|
1994-01-05 23:40:12 +03:00
|
|
|
}
|