FreeCalypso > hg > ueda-linux
changeset 11:73abf2c13884
libunet: nethash functions implemented
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Sat, 01 Aug 2015 22:38:00 +0000 |
parents | 62f8d6874a87 |
children | 51893347bc42 |
files | ueda/libunet/Makefile ueda/libunet/nethash.c ueda/libunet/nethash.h |
diffstat | 3 files changed, 84 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/ueda/libunet/Makefile Sat Aug 01 20:52:33 2015 +0000 +++ b/ueda/libunet/Makefile Sat Aug 01 22:38:00 2015 +0000 @@ -1,6 +1,6 @@ CC= gcc CFLAGS= -O2 -LIBOBJS=unetrd.o +LIBOBJS=nethash.o unetrd.o all: libunet.a
--- /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); +}