1: /* $Header$ */ 2: 3: /* 4: * Author: Peter J. Nicklin 5: */ 6: 7: /* 8: * htinstall() installs a new entry in a hash table if it doesn't already 9: * exist. If it does, the old definition and value is superseded. Returns 10: * a pointer to the entry, or null if out of memory. 11: */ 12: #include "null.h" 13: #include "hash.h" 14: 15: HASHBLK * 16: htinstall(key, def, val, hash) 17: char *key; /* key for hash table entry */ 18: char *def; /* definition string */ 19: int val; /* integer value */ 20: HASH *hash; /* hash table */ 21: { 22: char *malloc(); /* memory allocator */ 23: char *strsav(); /* save string somewhere */ 24: HASHBLK *htb; /* hash table entry block */ 25: HASHBLK *htlookup(); /* find hash table entry */ 26: int hashval; /* hash value for key */ 27: int hthash(); /* calculate hash value */ 28: 29: if ((htb = htlookup(key, hash)) == NULL) 30: { /* not found */ 31: if ((htb = (HASHBLK *) malloc(sizeof(HASHBLK))) == NULL) 32: return(NULL); 33: if ((htb->h_key = strsav(key)) == NULL) 34: return(NULL); 35: hashval = hthash(key, hash); 36: htb->h_next = (hash->hashtab)[hashval]; 37: (hash->hashtab)[hashval] = htb; 38: } 39: else { /* found */ 40: free(htb->h_def); /* free previous definition */ 41: } 42: if ((htb->h_def = strsav(def)) == NULL) 43: return(NULL); 44: htb->h_val = val; 45: return(htb); 46: }