xref: /original-bsd/old/ratfor/rlook.c (revision cd89438c)
1 /*-
2  * %sccs.include.proprietary.c%
3  */
4 
5 #ifndef lint
6 static char sccsid[] = "@(#)rlook.c	1.3 (Berkeley) 04/16/91";
7 #endif /* not lint */
8 
9 #define NULL 0
10 #define EOS 0
11 #define	HSHSIZ	101
12 struct	nlist {
13 	char	*name;
14 	char	*def;
15 	int	ydef;
16 	struct	nlist *next;
17 };
18 
19 struct	nlist	*hshtab[HSHSIZ];
20 struct nlist	*lookup();
21 char	*install();
22 char	*malloc();
23 char	*copy();
24 int	hshval;
25 
26 struct nlist *lookup(str)
27 char *str;
28 {
29 	register char *s1, *s2;
30 	register struct nlist *np;
31 	static struct nlist nodef;
32 
33 	s1 = str;
34 	for (hshval = 0; *s1; )
35 		hshval += *s1++;
36 	hshval %= HSHSIZ;
37 	for (np = hshtab[hshval]; np!=NULL; np = np->next) {
38 		s1 = str;
39 		s2 = np->name;
40 		while (*s1++ == *s2)
41 			if (*s2++ == EOS)
42 				return(np);
43 	}
44 	return(&nodef);
45 }
46 
47 char *install(nam, val, tran)
48 char *nam, *val;
49 int tran;
50 {
51 	register struct nlist *np;
52 
53 	if ((np = lookup(nam))->name == NULL) {
54 		np = (struct nlist *)malloc(sizeof(*np));
55 		np->name = copy(nam);
56 		np->def = copy(val);
57 		np->ydef = tran;
58 		np->next = hshtab[hshval];
59 		hshtab[hshval] = np;
60 		return(np->def);
61 	}
62 	free(np->def);
63 	np->def = copy(val);
64 	return(np->def);
65 }
66 
67 char *copy(s)
68 register char *s;
69 {
70 	register char *p, *s1;
71 
72 	p = s1 = (char *) malloc((unsigned)strlen(s)+1);
73 	while (*s1++ = *s++);
74 	return(p);
75 }
76