1 /* 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 const char rcsid[] = 20 "$FreeBSD$"; 21 #endif 22 23 24 #include "cron.h" 25 26 27 char ** 28 env_init() 29 { 30 register 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(envp) 40 char **envp; 41 { 42 char **p; 43 44 if ((p = envp)) 45 for (; *p; p++) 46 free(*p); 47 free(envp); 48 } 49 50 51 char ** 52 env_copy(envp) 53 register char **envp; 54 { 55 register int count, i; 56 register char **p; 57 58 for (count = 0; envp[count] != NULL; count++) 59 ; 60 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */ 61 if (p == NULL) { 62 errno = ENOMEM; 63 return NULL; 64 } 65 for (i = 0; i < count; i++) 66 if ((p[i] = strdup(envp[i])) == NULL) { 67 while (--i >= 0) 68 (void) free(p[i]); 69 free(p); 70 errno = ENOMEM; 71 return NULL; 72 } 73 p[count] = NULL; 74 return (p); 75 } 76 77 78 char ** 79 env_set(envp, envstr) 80 char **envp; 81 char *envstr; 82 { 83 register int count, found; 84 register char **p; 85 char *q; 86 87 /* 88 * count the number of elements, including the null pointer; 89 * also set 'found' to -1 or index of entry if already in here. 90 */ 91 found = -1; 92 for (count = 0; envp[count] != NULL; count++) { 93 if (!strcmp_until(envp[count], envstr, '=')) 94 found = count; 95 } 96 count++; /* for the NULL */ 97 98 if (found != -1) { 99 /* 100 * it exists already, so just free the existing setting, 101 * save our new one there, and return the existing array. 102 */ 103 q = envp[found]; 104 if ((envp[found] = strdup(envstr)) == NULL) { 105 envp[found] = q; 106 /* XXX env_free(envp); */ 107 errno = ENOMEM; 108 return NULL; 109 } 110 free(q); 111 return (envp); 112 } 113 114 /* 115 * it doesn't exist yet, so resize the array, move null pointer over 116 * one, save our string over the old null pointer, and return resized 117 * array. 118 */ 119 p = (char **) realloc((void *) envp, 120 (unsigned) ((count+1) * sizeof(char *))); 121 if (p == NULL) { 122 /* XXX env_free(envp); */ 123 errno = ENOMEM; 124 return NULL; 125 } 126 p[count] = p[count-1]; 127 if ((p[count-1] = strdup(envstr)) == NULL) { 128 env_free(p); 129 errno = ENOMEM; 130 return NULL; 131 } 132 return (p); 133 } 134 135 136 /* return ERR = end of file 137 * FALSE = not an env setting (file was repositioned) 138 * TRUE = was an env setting 139 */ 140 int 141 load_env(envstr, f) 142 char *envstr; 143 FILE *f; 144 { 145 long filepos; 146 int fileline; 147 char name[MAX_ENVSTR], val[MAX_ENVSTR]; 148 char quotechar, *c, *str; 149 int state; 150 151 /* The following states are traversed in order: */ 152 #define NAMEI 0 /* First char of NAME, may be quote */ 153 #define NAME 1 /* Subsequent chars of NAME */ 154 #define EQ1 2 /* After end of name, looking for '=' sign */ 155 #define EQ2 3 /* After '=', skipping whitespace */ 156 #define VALUEI 4 /* First char of VALUE, may be quote */ 157 #define VALUE 5 /* Subsequent chars of VALUE */ 158 #define FINI 6 /* All done, skipping trailing whitespace */ 159 #define ERROR 7 /* Error */ 160 161 filepos = ftell(f); 162 fileline = LineNumber; 163 skip_comments(f); 164 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) 165 return (ERR); 166 167 Debug(DPARS, ("load_env, read <%s>\n", envstr)); 168 169 bzero (name, sizeof name); 170 bzero (val, sizeof val); 171 str = name; 172 state = NAMEI; 173 quotechar = '\0'; 174 c = envstr; 175 while (state != ERROR && *c) { 176 switch (state) { 177 case NAMEI: 178 case VALUEI: 179 if (*c == '\'' || *c == '"') 180 quotechar = *c++; 181 ++state; 182 /* FALLTHROUGH */ 183 case NAME: 184 case VALUE: 185 if (quotechar) { 186 if (*c == quotechar) { 187 state++; 188 c++; 189 break; 190 } 191 if (state == NAME && *c == '=') { 192 state = ERROR; 193 break; 194 } 195 } else { 196 if (isspace (*c)) { 197 state++; 198 c++; 199 break; 200 } 201 if (state == NAME && *c == '=') { 202 state++; 203 break; 204 } 205 } 206 *str++ = *c++; 207 break; 208 209 case EQ1: 210 if (*c == '=') { 211 state++; 212 str = val; 213 quotechar = '\0'; 214 } else { 215 if (!isspace (*c)) 216 state = ERROR; 217 } 218 c++; 219 break; 220 case EQ2: 221 case FINI: 222 if (isspace (*c)) 223 c++; 224 else 225 state++; 226 break; 227 } 228 } 229 if (state != FINI && !(state == VALUE && !quotechar)) { 230 Debug(DPARS, ("load_env, parse error, state = %d\n", state)) 231 fseek(f, filepos, 0); 232 Set_LineNum(fileline); 233 return (FALSE); 234 } 235 236 /* 2 fields from parser; looks like an env setting 237 */ 238 239 if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1) 240 return (FALSE); 241 (void) sprintf(envstr, "%s=%s", name, val); 242 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) 243 return (TRUE); 244 } 245 246 247 char * 248 env_get(name, envp) 249 register char *name; 250 register char **envp; 251 { 252 register int len = strlen(name); 253 register char *p, *q; 254 255 while ((p = *envp++)) { 256 if (!(q = strchr(p, '='))) 257 continue; 258 if ((q - p) == len && !strncmp(p, name, len)) 259 return (q+1); 260 } 261 return (NULL); 262 } 263