Annotation of 42BSD/usr.bin/refer/thash.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char *sccsid = "@(#)thash.c     4.1 (Berkeley) 5/6/83";
        !             3: #endif
        !             4: 
        !             5: #include <stdio.h>
        !             6: #define MAXLINE 750
        !             7: 
        !             8: int nh 500;
        !             9: int saw[6000];
        !            10: char *comname "/usr/lib/eign";
        !            11: 
        !            12: main (argc,argv)
        !            13: char *argv[];
        !            14: {
        !            15: 
        !            16:        int i, z;
        !            17:        char *name;
        !            18: 
        !            19:        FILE *f;
        !            20: 
        !            21:        while (argc>1 && argv[1][0] == '-')
        !            22:        {
        !            23:                switch(argv[1][1])
        !            24:                {
        !            25:                case 'h':
        !            26:                        nh = atoi(argv[1]+2); 
        !            27:                        break;
        !            28:                }
        !            29:                argc--; 
        !            30:                argv++;
        !            31:        }
        !            32:        if (argc<=1)
        !            33:                dofile(stdin, "");
        !            34:        else
        !            35:                for(i=1; i<argc; i++)
        !            36:                {
        !            37:                        f = fopen(name=argv[i], "r");
        !            38:                        if (f==NULL)
        !            39:                                err("No file %s",name);
        !            40:                        else
        !            41:                                dofile(f, name);
        !            42:                }
        !            43:        for(z=i=0; i<nh; i++)
        !            44:        {
        !            45:                if (saw[i]) z++;
        !            46:        }
        !            47:        printf("hashes %d used %d\n",nh,z);
        !            48: }
        !            49: 
        !            50: dofile(f, name)
        !            51: FILE *f;
        !            52: char *name;
        !            53: {
        !            54:        /* read file f & spit out keys & ptrs */
        !            55:        char line[MAXLINE], *s;
        !            56:        char key[20], *p;
        !            57:        int k 0;
        !            58:        int c, lim;
        !            59:        int alph 0;
        !            60:        int used 0;
        !            61:        long lp 0;
        !            62: 
        !            63:        while (fgets(line, MAXLINE, f))
        !            64:        {
        !            65:                k++;
        !            66:                used=alph=0;
        !            67:                lim = strlen(line);
        !            68:                p = key;
        !            69:                for(s=line; c= *s; s++)
        !            70:                {
        !            71:                        if (isalpha(c) || isdigit(c))
        !            72:                        {
        !            73:                                if (alph++ < 6)
        !            74:                                        *p++ = c;
        !            75:                        }
        !            76:                        else
        !            77:                        {
        !            78:                                *p = 0;
        !            79:                                if (outkey(p=key))
        !            80:                                {
        !            81:                                        tkey(key,k);
        !            82:                                        used=1;
        !            83:                                }
        !            84:                                alph=0;
        !            85:                        }
        !            86:                }
        !            87:                lp += lim;
        !            88:        }
        !            89: }
        !            90: 
        !            91: outkey( ky)
        !            92: char *ky;
        !            93: {
        !            94:        int n;
        !            95:        n = strlen(ky);
        !            96:        if (n<3) return(0);
        !            97:        if (isdigit(ky[0]))
        !            98:                if (ky[0] != '1' || ky[1] != '9' || n!= 4) return(0);
        !            99:        return(1);
        !           100: }
        !           101: 
        !           102: hash (s)
        !           103: char *s;
        !           104: {
        !           105:        int c, n, q;
        !           106:        for(q=n=0; c= *s; s++)
        !           107:                n += (c*n + c << (n%4));
        !           108:        return(n);
        !           109: }
        !           110: 
        !           111: err (s, a)
        !           112: char *s;
        !           113: {
        !           114:        fprintf(stderr, "Error: ");
        !           115:        fprintf(stderr, s, a);
        !           116:        putc('\n', stderr);
        !           117: }
        !           118: 
        !           119: prefix(t, s)
        !           120: char *t, *s;
        !           121: {
        !           122:        int c, d;
        !           123:        while ( (c= *t++) == *s++)
        !           124:                if (c==0) return(1);
        !           125:        return(c==0 ? 1: 0);
        !           126: }
        !           127: 
        !           128: mindex(s, c)
        !           129: char *s;
        !           130: {
        !           131:        register char *p;
        !           132:        for( p=s; *p; p++)
        !           133:                if (*p ==c)
        !           134:                        return(p);
        !           135:        return(0);
        !           136: }
        !           137: 
        !           138: tkey(s,nw)
        !           139: char *s;
        !           140: {
        !           141:        int x;
        !           142:        x = abs(hash(s)) % nh;
        !           143:        /* if (saw[x]) printf("%d %d\n", x, nw); */
        !           144:        saw[x]= nw;
        !           145: }
        !           146: 
        !           147: abs(n)
        !           148: {
        !           149:        return(n>0 ? n : -n);
        !           150: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.