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