view ueda/libunet/nethash.c @ 129:3f475a608cc2

HEADER26_Shrouded.fp created
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 16 Jul 2020 19:30:17 +0000
parents 73abf2c13884
children
line wrap: on
line source

/*
 * 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);
}