usr.bin/rdist/lookup.c
struct namelist *
lookup(char *name, int action, struct namelist *value)
{
	unsigned n;
	char *cp;
	struct syment *s;
	char buf[BUFSIZ];

	if (debug)
		printf("lookup(%s, %d, %p)\n", name, action, value);

	n = 0;
	for (cp = name; *cp; )
		n += *cp++;
	n %= HASHSIZE;

	for (s = hashtab[n]; s != NULL; s = s->s_next) {
		if (strcmp(name, s->s_name))
			continue;
		if (action != LOOKUP) {
			if (action != INSERT || s->s_type != CONST) {
				snprintf(buf, sizeof(buf),"%s redefined", name);
				yyerror(buf);
			}
		}
		return(s->s_value);
	}

	if (action == LOOKUP) {
		snprintf(buf, sizeof(buf), "%s undefined", name);
		yyerror(buf);
		return(NULL);
	}

	s = ALLOC(syment);
	if (s == NULL)
		fatal("ran out of memory\n");
	s->s_next = hashtab[n];
	hashtab[n] = s;
	s->s_type = action == INSERT ? VAR : CONST;
	s->s_name = name;
	s->s_value = value;
	return(value);
}
/*
 * Lookup name in the table and return a pointer to it.
 * LOOKUP - just do lookup, return NULL if not found.
 * INSERT - insert name with value, error if already defined.
 * REPLACE - insert or replace name with value.
 */
Lookup name in the table and return a pointer to it.
LOOKUP - just do lookup, return NULL if not found.
INSERT - insert name with value, error if already defined.
REPLACE - insert or replace name with value.
Lookup name in the table and return a pointer to it . LOOKUP - just do lookup return NULL if not found . INSERT - insert name with value error if already defined . REPLACE - insert or replace name with value .
struct namelist *
lookup(char *name, int action, struct namelist *value)
{
	unsigned n;
	char *cp;
	struct syment *s;
	char buf[BUFSIZ];
	if (debug)
		printf("lookup(%s, %d, %p)\n", name, action, value);
	n = 0;
	for (cp = name; *cp; )
		n += *cp++;
	n %= HASHSIZE;
	for (s = hashtab[n]; s != NULL; s = s->s_next) {
		if (strcmp(name, s->s_name))
			continue;
		if (action != LOOKUP) {
			if (action != INSERT || s->s_type != CONST) {
				snprintf(buf, sizeof(buf),"%s redefined", name);
				yyerror(buf);
			}
		}
		return(s->s_value);
	}
	if (action == LOOKUP) {
		snprintf(buf, sizeof(buf), "%s undefined", name);
		yyerror(buf);
		return(NULL);
	}
	s = ALLOC(syment);
	if (s == NULL)
		fatal("ran out of memory\n");
	s->s_next = hashtab[n];
	hashtab[n] = s;
	s->s_type = action == INSERT ? VAR : CONST;
	s->s_name = name;
	s->s_value = value;
	return(value);
}
struct namelist * lookup ( char * name , int action , struct namelist * value ) { unsigned n ; char * cp ; struct syment * s ; char buf [ BUFSIZ ] ; if ( debug ) printf ( " \n " , name , action , value ) ; n = 0 ; for ( cp = name ; * cp ; ) n += * cp ++ ; n %= HASHSIZE ; for ( s = hashtab [ n ] ; s != NULL ; s = s -> s_next ) { if ( strcmp ( name , s -> s_name ) ) continue ; if ( action != LOOKUP ) { if ( action != INSERT || s -> s_type != CONST ) { snprintf ( buf , sizeof ( buf ) , " " , name ) ; yyerror ( buf ) ; } } return ( s -> s_value ) ; } if ( action == LOOKUP ) { snprintf ( buf , sizeof ( buf ) , " " , name ) ; yyerror ( buf ) ; return ( NULL ) ; } s = ALLOC ( syment ) ; if ( s == NULL ) fatal ( " \n " ) ; s -> s_next = hashtab [ n ] ; hashtab [ n ] = s ; s -> s_type = action == INSERT ? VAR : CONST ; s -> s_name = name ; s -> s_value = value ; return ( value ) ; }
Lookup name in the table and return a pointer to it.
Lookup name in the table and return a pointer to it .