FreeCalypso > hg > ueda-linux
comparison 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 |
comparison
equal
deleted
inserted
replaced
10:62f8d6874a87 | 11:73abf2c13884 |
---|---|
1 /* | |
2 * This module provides library functions for managing the list of NET objects | |
3 * read from unet files. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include "nethash.h" | |
11 | |
12 struct net *net_list_head; | |
13 int longest_net_name; | |
14 | |
15 #define HASH_SIZE 1103 | |
16 static struct net *hashtab[HASH_SIZE]; | |
17 static struct net **global_tailp = &net_list_head; | |
18 | |
19 static int | |
20 hash_netname(str) | |
21 char *str; | |
22 { | |
23 register u_long accum = 0; | |
24 register char *cp; | |
25 register int c, i; | |
26 | |
27 for (cp = str, i = 1; c = *cp; cp++, i++) | |
28 accum += c * i; | |
29 return(accum % HASH_SIZE); | |
30 } | |
31 | |
32 struct net * | |
33 enter_net_object(netname, extra_alloc) | |
34 char *netname; | |
35 unsigned extra_alloc; | |
36 { | |
37 register struct net *n, **np; | |
38 int namelen; | |
39 | |
40 for (np = hashtab + hash_netname(netname); n = *np; np = &n->nextinhash) | |
41 if (!strcmp(n->name, netname)) { | |
42 fprintf(stderr, "error: duplicate NET name %s\n", | |
43 netname); | |
44 exit(1); | |
45 } | |
46 namelen = strlen(netname); | |
47 if (namelen > longest_net_name) | |
48 longest_net_name = namelen; | |
49 n = (struct net *) malloc(sizeof(struct net) + extra_alloc + | |
50 namelen + 1); | |
51 if (!n) { | |
52 perror("malloc"); | |
53 exit(1); | |
54 } | |
55 n->name = (char *)(n + 1) + extra_alloc; | |
56 strcpy(n->name, netname); | |
57 n->nextinlist = 0; | |
58 n->nextinhash = 0; | |
59 *np = n; | |
60 *global_tailp = n; | |
61 global_tailp = &n->nextinlist; | |
62 return n; | |
63 } | |
64 | |
65 struct net * | |
66 find_net_by_name(soughtname) | |
67 register char *soughtname; | |
68 { | |
69 register struct net *n; | |
70 | |
71 for (n = hashtab[hash_netname(soughtname)]; n; n = n->nextinhash) | |
72 if (!strcmp(n->name, soughtname)) | |
73 return(n); | |
74 fprintf(stderr, "error: no net named \"%s\"\n", soughtname); | |
75 exit(1); | |
76 } |