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 * $FreeBSD: src/usr.sbin/cron/lib/env.c,v 1.7.2.1 2000/07/01 10:35:07 ps Exp $ 18 * $DragonFly: src/usr.sbin/cron/lib/env.c,v 1.2 2003/06/17 04:29:53 dillon Exp $ 19 */ 20 21 #include "cron.h" 22 23 24 char ** 25 env_init() 26 { 27 register char **p = (char **) malloc(sizeof(char *)); 28 29 if (p) 30 p[0] = NULL; 31 return (p); 32 } 33 34 35 void 36 env_free(envp) 37 char **envp; 38 { 39 char **p; 40 41 for (p = envp; *p; p++) 42 free(*p); 43 free(envp); 44 } 45 46 47 char ** 48 env_copy(envp) 49 register char **envp; 50 { 51 register int count, i; 52 register char **p; 53 54 for (count = 0; envp[count] != NULL; count++) 55 ; 56 p = (char **) malloc((count+1) * sizeof(char *)); /* 1 for the NULL */ 57 if (p == NULL) { 58 errno = ENOMEM; 59 return NULL; 60 } 61 for (i = 0; i < count; i++) 62 if ((p[i] = strdup(envp[i])) == NULL) { 63 while (--i >= 0) 64 (void) free(p[i]); 65 free(p); 66 errno = ENOMEM; 67 return NULL; 68 } 69 p[count] = NULL; 70 return (p); 71 } 72 73 74 char ** 75 env_set(envp, envstr) 76 char **envp; 77 char *envstr; 78 { 79 register int count, found; 80 register 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 **) realloc((void *) envp, 116 (unsigned) ((count+1) * sizeof(char *))); 117 if (p == NULL) { 118 /* XXX env_free(envp); */ 119 errno = ENOMEM; 120 return NULL; 121 } 122 p[count] = p[count-1]; 123 if ((p[count-1] = strdup(envstr)) == NULL) { 124 env_free(p); 125 errno = ENOMEM; 126 return NULL; 127 } 128 return (p); 129 } 130 131 132 /* return ERR = end of file 133 * FALSE = not an env setting (file was repositioned) 134 * TRUE = was an env setting 135 */ 136 int 137 load_env(envstr, f) 138 char *envstr; 139 FILE *f; 140 { 141 long filepos; 142 int fileline; 143 char name[MAX_ENVSTR], val[MAX_ENVSTR]; 144 int fields; 145 146 filepos = ftell(f); 147 fileline = LineNumber; 148 skip_comments(f); 149 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) 150 return (ERR); 151 152 Debug(DPARS, ("load_env, read <%s>\n", envstr)) 153 154 name[0] = val[0] = '\0'; 155 fields = sscanf(envstr, "%[^ =] = %[^\n#]", name, val); 156 if (fields != 2) { 157 Debug(DPARS, ("load_env, not 2 fields (%d)\n", fields)) 158 fseek(f, filepos, 0); 159 Set_LineNum(fileline); 160 return (FALSE); 161 } 162 163 /* 2 fields from scanf; looks like an env setting 164 */ 165 166 /* 167 * process value string 168 */ 169 /*local*/{ 170 int len = strdtb(val); 171 172 if (len >= 2) { 173 if (val[0] == '\'' || val[0] == '"') { 174 if (val[len-1] == val[0]) { 175 val[len-1] = '\0'; 176 (void) strcpy(val, val+1); 177 } 178 } 179 } 180 } 181 182 if (strlen(name) + 1 + strlen(val) >= MAX_ENVSTR-1) 183 return (FALSE); 184 (void) sprintf(envstr, "%s=%s", name, val); 185 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) 186 return (TRUE); 187 } 188 189 190 char * 191 env_get(name, envp) 192 register char *name; 193 register char **envp; 194 { 195 register int len = strlen(name); 196 register char *p, *q; 197 198 while ((p = *envp++)) { 199 if (!(q = strchr(p, '='))) 200 continue; 201 if ((q - p) == len && !strncmp(p, name, len)) 202 return (q+1); 203 } 204 return (NULL); 205 } 206