1 /* Copyright 1988,1990,1993,1994 by Paul Vixie 2 * All rights reserved 3 */ 4 5 /* 6 * Copyright (c) 1997 by Internet Software Consortium 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 19 * SOFTWARE. 20 */ 21 22 23 24 #include "cron.h" 25 26 27 char ** 28 env_init(void) 29 { 30 char **p = (char **) malloc(sizeof(char **)); 31 32 if (p) 33 p[0] = NULL; 34 return (p); 35 } 36 37 38 void 39 env_free(char **envp) 40 { 41 char **p; 42 43 if ((p = envp)) 44 for (; *p; p++) 45 free(*p); 46 free(envp); 47 } 48 49 50 char ** 51 env_copy(char **envp) 52 { 53 int count, i; 54 char **p; 55 56 for (count = 0; envp[count] != NULL; count++) 57 ; 58 p = (char **) reallocarray(NULL, count+1, sizeof(char *)); /* 1 for the NULL */ 59 if (p == NULL) { 60 errno = ENOMEM; 61 return NULL; 62 } 63 for (i = 0; i < count; i++) 64 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 p[count] = NULL; 72 return (p); 73 } 74 75 76 char ** 77 env_set(char **envp, char *envstr) 78 { 79 int count, found; 80 char **p; 81 char *q; 82 83 /* 84 * count the number of elements, including the null pointer; 85 * also set 'found' to -1 or index of entry if already in here. 86 */ 87 found = -1; 88 for (count = 0; envp[count] != NULL; count++) { 89 if (!strcmp_until(envp[count], envstr, '=')) 90 found = count; 91 } 92 count++; /* for the NULL */ 93 94 if (found != -1) { 95 /* 96 * it exists already, so just free the existing setting, 97 * save our new one there, and return the existing array. 98 */ 99 q = envp[found]; 100 if ((envp[found] = strdup(envstr)) == NULL) { 101 envp[found] = q; 102 /* XXX env_free(envp); */ 103 errno = ENOMEM; 104 return NULL; 105 } 106 free(q); 107 return (envp); 108 } 109 110 /* 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 113 * array. 114 */ 115 p = (char **) reallocarray(envp, count+1, sizeof(char *)); 116 if (p == NULL) { 117 /* XXX env_free(envp); */ 118 errno = ENOMEM; 119 return NULL; 120 } 121 p[count] = p[count-1]; 122 if ((p[count-1] = strdup(envstr)) == NULL) { 123 env_free(p); 124 errno = ENOMEM; 125 return NULL; 126 } 127 return (p); 128 } 129 130 131 /* return ERR = end of file 132 * FALSE = not an env setting (file was repositioned) 133 * TRUE = was an env setting 134 */ 135 int 136 load_env(char *envstr, FILE *f) 137 { 138 long filepos; 139 int fileline; 140 char name[MAX_ENVSTR], val[MAX_ENVSTR]; 141 char quotechar, *c, *str; 142 int state; 143 144 /* The following states are traversed in order: */ 145 #define NAMEI 0 /* First char of NAME, may be quote */ 146 #define NAME 1 /* Subsequent chars of NAME */ 147 #define EQ1 2 /* After end of name, looking for '=' sign */ 148 #define EQ2 3 /* After '=', skipping whitespace */ 149 #define VALUEI 4 /* First char of VALUE, may be quote */ 150 #define VALUE 5 /* Subsequent chars of VALUE */ 151 #define FINI 6 /* All done, skipping trailing whitespace */ 152 #define ERROR 7 /* Error */ 153 154 filepos = ftell(f); 155 fileline = LineNumber; 156 skip_comments(f); 157 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) 158 return (ERR); 159 160 Debug(DPARS, ("load_env, read <%s>\n", envstr)); 161 162 bzero (name, sizeof name); 163 bzero (val, sizeof val); 164 str = name; 165 state = NAMEI; 166 quotechar = '\0'; 167 c = envstr; 168 while (state != ERROR && *c) { 169 switch (state) { 170 case NAMEI: 171 case VALUEI: 172 if (*c == '\'' || *c == '"') 173 quotechar = *c++; 174 ++state; 175 /* FALLTHROUGH */ 176 case NAME: 177 case VALUE: 178 if (quotechar) { 179 if (*c == quotechar) { 180 state++; 181 c++; 182 break; 183 } 184 if (state == NAME && *c == '=') { 185 state = ERROR; 186 break; 187 } 188 } else { 189 if (state == NAME) { 190 if (isspace (*c)) { 191 c++; 192 state++; 193 break; 194 } 195 if (*c == '=') { 196 state++; 197 break; 198 } 199 } 200 } 201 *str++ = *c++; 202 break; 203 204 case EQ1: 205 if (*c == '=') { 206 state++; 207 str = val; 208 quotechar = '\0'; 209 } else { 210 if (!isspace (*c)) 211 state = ERROR; 212 } 213 c++; 214 break; 215 case EQ2: 216 case FINI: 217 if (isspace (*c)) 218 c++; 219 else 220 state++; 221 break; 222 } 223 } 224 if (state != FINI && !(state == VALUE && !quotechar)) { 225 Debug(DPARS, ("load_env, parse error, state = %d\n", state)) 226 fseek(f, filepos, 0); 227 Set_LineNum(fileline); 228 return (FALSE); 229 } 230 if (state == VALUE) { 231 /* End of unquoted value: trim trailing whitespace */ 232 c = val + strlen (val); 233 while (c > val && isspace (*(c - 1))) 234 *(--c) = '\0'; 235 } 236 237 /* 2 fields from parser; looks like an env setting */ 238 239 if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR) 240 return (FALSE); 241 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) 242 return (TRUE); 243 } 244 245 246 char * 247 env_get(char *name, char **envp) 248 { 249 int len = strlen(name); 250 char *p, *q; 251 252 while ((p = *envp++) != NULL) { 253 if (!(q = strchr(p, '='))) 254 continue; 255 if ((q - p) == len && !strncmp(p, name, len)) 256 return (q+1); 257 } 258 return (NULL); 259 } 260