look.c (de9b3b9034ee0d39f3bff03b8368361d80cf728f) | look.c (e3d8671772982f3135d8d6997f3cd698cca4df19) |
---|---|
1/* $OpenBSD: look.c,v 1.9 2002/02/16 21:27:48 millert Exp $ */ 2 |
|
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Ozan Yigit at York University. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 22 unchanged lines hidden (view full) --- 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static char sccsid[] = "@(#)look.c 8.1 (Berkeley) 6/6/93"; | 3/* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Ozan Yigit at York University. 9 * 10 * Redistribution and use in source and binary forms, with or without --- 22 unchanged lines hidden (view full) --- 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39#ifndef lint 40static char sccsid[] = "@(#)look.c 8.1 (Berkeley) 6/6/93"; |
39static char rcsid[] = 40 "$FreeBSD$"; | |
41#endif /* not lint */ 42 43/* 44 * look.c 45 * Facility: m4 macro processor 46 * by: oz 47 */ 48 49#include <sys/types.h> | 41#endif /* not lint */ 42 43/* 44 * look.c 45 * Facility: m4 macro processor 46 * by: oz 47 */ 48 49#include <sys/types.h> |
50#include <err.h> | |
51#include <stdio.h> 52#include <stdlib.h> | 50#include <stdio.h> 51#include <stdlib.h> |
52#include <stddef.h> |
|
53#include <string.h> 54#include "mdef.h" 55#include "stdd.h" 56#include "extern.h" 57 | 53#include <string.h> 54#include "mdef.h" 55#include "stdd.h" 56#include "extern.h" 57 |
58int | 58static void freent(ndptr); 59 60unsigned |
59hash(name) | 61hash(name) |
60register char *name; | 62 const char *name; |
61{ | 63{ |
62 register unsigned long h = 0; | 64 unsigned int h = 0; |
63 while (*name) 64 h = (h << 5) + h + *name++; | 65 while (*name) 66 h = (h << 5) + h + *name++; |
65 return (h % HASHSIZE); | 67 return (h); |
66} 67 68/* 69 * find name in the hash table 70 */ | 68} 69 70/* 71 * find name in the hash table 72 */ |
71ndptr | 73ndptr |
72lookup(name) | 74lookup(name) |
73char *name; | 75 const char *name; |
74{ | 76{ |
75 register ndptr p; | 77 ndptr p; 78 unsigned int h; |
76 | 79 |
77 for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr) 78 if (STREQ(name, p->name)) | 80 h = hash(name); 81 for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr) 82 if (h == p->hv && STREQ(name, p->name)) |
79 break; 80 return (p); 81} 82 83/* 84 * hash and create an entry in the hash table. 85 * The new entry is added in front of a hash bucket. 86 */ | 83 break; 84 return (p); 85} 86 87/* 88 * hash and create an entry in the hash table. 89 * The new entry is added in front of a hash bucket. 90 */ |
87ndptr | 91ndptr |
88addent(name) | 92addent(name) |
89char *name; | 93 const char *name; |
90{ | 94{ |
91 register int h; | 95 unsigned int h; |
92 ndptr p; 93 94 h = hash(name); | 96 ndptr p; 97 98 h = hash(name); |
95 if ((p = malloc(sizeof(struct ndblock))) == NULL) 96 err(1, "malloc"); 97 p->nxtptr = hashtab[h]; 98 hashtab[h] = p; 99 if ((p->name = strdup(name)) == NULL) 100 err(1, "strdup"); | 99 p = (ndptr) xalloc(sizeof(struct ndblock)); 100 p->nxtptr = hashtab[h % HASHSIZE]; 101 hashtab[h % HASHSIZE] = p; 102 p->name = xstrdup(name); 103 p->hv = h; |
101 return p; 102} 103 104static void 105freent(p) | 104 return p; 105} 106 107static void 108freent(p) |
106ndptr p; | 109 ndptr p; |
107{ | 110{ |
108 if (!(p->type & STATIC)) { 109 free((char *) p->name); 110 if (p->defn != null) 111 free((char *) p->defn); 112 } | 111 free((char *) p->name); 112 if (p->defn != null) 113 free((char *) p->defn); |
113 free((char *) p); 114} 115 116/* 117 * remove an entry from the hashtable 118 */ 119void 120remhash(name, all) | 114 free((char *) p); 115} 116 117/* 118 * remove an entry from the hashtable 119 */ 120void 121remhash(name, all) |
121char *name; 122int all; | 122 const char *name; 123 int all; |
123{ | 124{ |
124 register int h; 125 register ndptr xp, tp, mp; | 125 unsigned int h; 126 ndptr xp, tp, mp; |
126 127 h = hash(name); | 127 128 h = hash(name); |
128 mp = hashtab[h]; | 129 mp = hashtab[h % HASHSIZE]; |
129 tp = nil; 130 while (mp != nil) { | 130 tp = nil; 131 while (mp != nil) { |
131 if (STREQ(mp->name, name)) { | 132 if (mp->hv == h && STREQ(mp->name, name)) { |
132 mp = mp->nxtptr; 133 if (tp == nil) { | 133 mp = mp->nxtptr; 134 if (tp == nil) { |
134 freent(hashtab[h]); 135 hashtab[h] = mp; | 135 freent(hashtab[h % HASHSIZE]); 136 hashtab[h % HASHSIZE] = mp; |
136 } 137 else { 138 xp = tp->nxtptr; 139 tp->nxtptr = mp; 140 freent(xp); 141 } 142 if (!all) 143 break; 144 } 145 else { 146 tp = mp; 147 mp = mp->nxtptr; 148 } 149 } 150} | 137 } 138 else { 139 xp = tp->nxtptr; 140 tp->nxtptr = mp; 141 freent(xp); 142 } 143 if (!all) 144 break; 145 } 146 else { 147 tp = mp; 148 mp = mp->nxtptr; 149 } 150 } 151} |