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