annotate ueda/libunet/nethash.c @ 11:73abf2c13884

libunet: nethash functions implemented
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sat, 01 Aug 2015 22:38:00 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module provides library functions for managing the list of NET objects
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
3 * read from unet files.
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
4 */
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
5
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <stdio.h>
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdlib.h>
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <string.h>
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <strings.h>
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
10 #include "nethash.h"
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
11
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
12 struct net *net_list_head;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
13 int longest_net_name;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
14
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
15 #define HASH_SIZE 1103
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
16 static struct net *hashtab[HASH_SIZE];
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
17 static struct net **global_tailp = &net_list_head;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
18
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
19 static int
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
20 hash_netname(str)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
21 char *str;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
22 {
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
23 register u_long accum = 0;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
24 register char *cp;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
25 register int c, i;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
26
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
27 for (cp = str, i = 1; c = *cp; cp++, i++)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
28 accum += c * i;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
29 return(accum % HASH_SIZE);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
30 }
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
31
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
32 struct net *
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
33 enter_net_object(netname, extra_alloc)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
34 char *netname;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
35 unsigned extra_alloc;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
36 {
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
37 register struct net *n, **np;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
38 int namelen;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
39
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
40 for (np = hashtab + hash_netname(netname); n = *np; np = &n->nextinhash)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
41 if (!strcmp(n->name, netname)) {
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
42 fprintf(stderr, "error: duplicate NET name %s\n",
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
43 netname);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
44 exit(1);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
45 }
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
46 namelen = strlen(netname);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
47 if (namelen > longest_net_name)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
48 longest_net_name = namelen;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
49 n = (struct net *) malloc(sizeof(struct net) + extra_alloc +
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
50 namelen + 1);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
51 if (!n) {
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
52 perror("malloc");
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53 exit(1);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
54 }
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
55 n->name = (char *)(n + 1) + extra_alloc;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
56 strcpy(n->name, netname);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
57 n->nextinlist = 0;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
58 n->nextinhash = 0;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
59 *np = n;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
60 *global_tailp = n;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
61 global_tailp = &n->nextinlist;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
62 return n;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
63 }
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
64
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
65 struct net *
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
66 find_net_by_name(soughtname)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
67 register char *soughtname;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
68 {
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
69 register struct net *n;
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
70
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
71 for (n = hashtab[hash_netname(soughtname)]; n; n = n->nextinhash)
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
72 if (!strcmp(n->name, soughtname))
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
73 return(n);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
74 fprintf(stderr, "error: no net named \"%s\"\n", soughtname);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
75 exit(1);
73abf2c13884 libunet: nethash functions implemented
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
76 }