1/*++
2/* NAME
3/*	nvtable 3
4/* SUMMARY
5/*	attribute list manager
6/* SYNOPSIS
7/*	#include <nvtable.h>
8/*
9/*	typedef	struct {
10/* .in +4
11/*		char	*key;
12/*		char	*value;
13/*		/* private fields... */
14/* .in -4
15/*	} NVTABLE_INFO;
16/*
17/*	NVTABLE	*nvtable_create(size)
18/*	int	size;
19/*
20/*	NVTABLE_INFO *nvtable_update(table, key, value)
21/*	NVTABLE	*table;
22/*	const char *key;
23/*	const char *value;
24/*
25/*	char	*nvtable_find(table, key)
26/*	NVTABLE	*table;
27/*	const char *key;
28/*
29/*	NVTABLE_INFO *nvtable_locate(table, key)
30/*	NVTABLE	*table;
31/*	const char *key;
32/*
33/*	void	nvtable_delete(table, key)
34/*	NVTABLE	*table;
35/*	const char *key;
36/*
37/*	void	nvtable_free(table)
38/*	NVTABLE	*table;
39/*
40/*	void	nvtable_walk(table, action, ptr)
41/*	NVTABLE	*table;
42/*	void	(*action)(NVTABLE_INFO *, char *ptr);
43/*	char	*ptr;
44/*
45/*	NVTABLE_INFO **nvtable_list(table)
46/*	NVTABLE	*table;
47/* DESCRIPTION
48/*	This module maintains one or more attribute lists. It provides a
49/*	more convenient interface than hash tables, although it uses the
50/*	same underlying implementation. Each attribute list entry consists
51/*	of a unique string-valued lookup key and a string value.
52/*
53/*	nvtable_create() creates a table of the specified size and returns a
54/*	pointer to the result.
55/*
56/*	nvtable_update() stores or updates a (key, value) pair in the specified
57/*	table and returns a pointer to the resulting entry. The key and the
58/*	value are copied.
59/*
60/*	nvtable_find() returns the value that was stored under the given key,
61/*	or a null pointer if it was not found. In order to distinguish
62/*	a null value from a non-existent value, use nvtable_locate().
63/*
64/*	nvtable_locate() returns a pointer to the entry that was stored
65/*	for the given key, or a null pointer if it was not found.
66/*
67/*	nvtable_delete() removes one entry that was stored under the given key.
68/*
69/*	nvtable_free() destroys a hash table, including contents.
70/*
71/*	nvtable_walk() invokes the action function for each table entry, with
72/*	a pointer to the entry as its argument. The ptr argument is passed
73/*	on to the action function.
74/*
75/*	nvtable_list() returns a null-terminated list of pointers to
76/*	all elements in the named table. The list should be passed to
77/*	myfree().
78/* RESTRICTIONS
79/*	A callback function should not modify the attribute list that is
80/*	specified to its caller.
81/* DIAGNOSTICS
82/*	The following conditions are reported and cause the program to
83/*	terminate immediately: memory allocation failure; an attempt
84/*	to delete a non-existent entry.
85/* SEE ALSO
86/*	mymalloc(3) memory management wrapper
87/*	htable(3) hash table manager
88/* LICENSE
89/* .ad
90/* .fi
91/*	The Secure Mailer license must be distributed with this software.
92/* AUTHOR(S)
93/*	Wietse Venema
94/*	IBM T.J. Watson Research
95/*	P.O. Box 704
96/*	Yorktown Heights, NY 10598, USA
97/*--*/
98
99/* C library */
100
101#include <sys_defs.h>
102
103/* Utility library. */
104
105#include <mymalloc.h>
106#include <htable.h>
107#include <nvtable.h>
108
109/* nvtable_update - update or enter (key, value) pair */
110
111NVTABLE_INFO *nvtable_update(NVTABLE * table, const char *key, const char *value)
112{
113    NVTABLE_INFO *ht;
114
115    if ((ht = htable_locate(table, key)) != 0) {
116	myfree(ht->value);
117    } else {
118	ht = htable_enter(table, key, (char *) 0);
119    }
120    ht->value = mystrdup(value);
121    return (ht);
122}
123