FreeCalypso > hg > ueda-linux
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ueda/libunet/nethash.c Sat Aug 01 22:38:00 2015 +0000 @@ -0,0 +1,76 @@ +/* + * This module provides library functions for managing the list of NET objects + * read from unet files. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include "nethash.h" + +struct net *net_list_head; +int longest_net_name; + +#define HASH_SIZE 1103 +static struct net *hashtab[HASH_SIZE]; +static struct net **global_tailp = &net_list_head; + +static int +hash_netname(str) + char *str; +{ + register u_long accum = 0; + register char *cp; + register int c, i; + + for (cp = str, i = 1; c = *cp; cp++, i++) + accum += c * i; + return(accum % HASH_SIZE); +} + +struct net * +enter_net_object(netname, extra_alloc) + char *netname; + unsigned extra_alloc; +{ + register struct net *n, **np; + int namelen; + + for (np = hashtab + hash_netname(netname); n = *np; np = &n->nextinhash) + if (!strcmp(n->name, netname)) { + fprintf(stderr, "error: duplicate NET name %s\n", + netname); + exit(1); + } + namelen = strlen(netname); + if (namelen > longest_net_name) + longest_net_name = namelen; + n = (struct net *) malloc(sizeof(struct net) + extra_alloc + + namelen + 1); + if (!n) { + perror("malloc"); + exit(1); + } + n->name = (char *)(n + 1) + extra_alloc; + strcpy(n->name, netname); + n->nextinlist = 0; + n->nextinhash = 0; + *np = n; + *global_tailp = n; + global_tailp = &n->nextinlist; + return n; +} + +struct net * +find_net_by_name(soughtname) + register char *soughtname; +{ + register struct net *n; + + for (n = hashtab[hash_netname(soughtname)]; n; n = n->nextinhash) + if (!strcmp(n->name, soughtname)) + return(n); + fprintf(stderr, "error: no net named \"%s\"\n", soughtname); + exit(1); +}