BSD 3 development
[unix-history] / usr / src / cmd / ratfor / rlook.c
CommitLineData
42d6e430
BJ
1#define NULL 0
2#define EOS 0
3#define HSHSIZ 101
4struct nlist {
5 char *name;
6 char *def;
7 int ydef;
8 struct nlist *next;
9};
10
11struct nlist *hshtab[HSHSIZ];
12struct nlist *lookup();
13char *install();
14char *malloc();
15char *copy();
16int hshval;
17
18struct nlist *lookup(str)
19char *str;
20{
21 register char *s1, *s2;
22 register struct nlist *np;
23 static struct nlist nodef;
24
25 s1 = str;
26 for (hshval = 0; *s1; )
27 hshval += *s1++;
28 hshval %= HSHSIZ;
29 for (np = hshtab[hshval]; np!=NULL; np = np->next) {
30 s1 = str;
31 s2 = np->name;
32 while (*s1++ == *s2)
33 if (*s2++ == EOS)
34 return(np);
35 }
36 return(&nodef);
37}
38
39char *install(nam, val, tran)
40char *nam, *val;
41int tran;
42{
43 register struct nlist *np;
44
45 if ((np = lookup(nam))->name == NULL) {
46 np = (struct nlist *)malloc(sizeof(*np));
47 np->name = copy(nam);
48 np->def = copy(val);
49 np->ydef = tran;
50 np->next = hshtab[hshval];
51 hshtab[hshval] = np;
52 return(np->def);
53 }
54 free(np->def);
55 np->def = copy(val);
56 return(np->def);
57}
58
59char *copy(s)
60register char *s;
61{
62 register char *p, *s1;
63
64 p = s1 = (char *) malloc((unsigned)strlen(s)+1);
65 while (*s1++ = *s++);
66 return(p);
67}