annotate ueda/sverp-bind/insthash.c @ 20:dda8e455c863

unet-bind works to the point of reporting unbound instances
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sun, 02 Aug 2015 03:23:44 +0000
parents 1f3283f8e482
children f7b09a54c2ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
1 #include <stdio.h>
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
2 #include <stdlib.h>
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
3 #include <string.h>
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
4 #include <strings.h>
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
5 #include "struct.h"
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
6
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
7 #define HASH_SIZE 1103
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
8 static struct instance *hashtab[HASH_SIZE];
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
9
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
10 static int
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
11 hash_instname(str)
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
12 char *str;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
13 {
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
14 register u_long accum = 0;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
15 register char *cp;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
16 register int c, i;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
17
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
18 for (cp = str, i = 1; c = *cp; cp++, i++)
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
19 accum += c * i;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
20 return(accum % HASH_SIZE);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
21 }
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
22
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
23 struct instance *
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
24 enter_instance(newname)
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
25 char *newname;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
26 {
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
27 register struct instance *n, **np;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
28 int namelen;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
29
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
30 for (np = hashtab + hash_instname(newname); n = *np;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
31 np = &n->nextinhash)
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
32 if (!strcmp(n->name, newname)) {
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
33 fprintf(stderr, "error: duplicate instance name %s\n",
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
34 newname);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
35 exit(1);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
36 }
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
37 namelen = strlen(newname);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
38 n = (struct instance *) malloc(sizeof(struct instance) + namelen + 1);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
39 if (!n) {
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
40 perror("malloc");
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
41 exit(1);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
42 }
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
43 bzero(n, sizeof(struct instance));
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
44 n->name = (char *)(n + 1);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
45 strcpy(n->name, newname);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
46 *np = n;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
47 return n;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
48 }
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
49
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
50 struct instance *
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
51 find_instance(soughtname)
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
52 register char *soughtname;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53 {
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
54 register struct instance *n;
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
55
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
56 for (n = hashtab[hash_instname(soughtname)]; n; n = n->nextinhash)
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
57 if (!strcmp(n->name, soughtname))
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
58 return(n);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
59 return(0);
1f3283f8e482 unet-bind: instance hash implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
60 }