home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / ratfor / rlook.c < prev   
Encoding:
C/C++ Source or Header  |  1979-01-10  |  1.1 KB  |  68 lines

  1. #define NULL 0
  2. #define EOS 0
  3. #define    HSHSIZ    101
  4. struct    nlist {
  5.     char    *name;
  6.     char    *def;
  7.     int    ydef;
  8.     struct    nlist *next;
  9. };
  10.  
  11. struct    nlist    *hshtab[HSHSIZ];
  12. struct nlist    *lookup();
  13. char    *install();
  14. char    *malloc();
  15. char    *copy();
  16. int    hshval;
  17.  
  18. struct nlist *lookup(str)
  19. char *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.  
  39. char *install(nam, val, tran)
  40. char *nam, *val;
  41. int 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.  
  59. char *copy(s)
  60. register 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. }
  68.