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