1m4_ignore([dnl
2#include <sys/types.h>
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include <db.h>
9
10#define	progname	"t"
11#define	database	"a.db"
12
13int store(DB *);
14
15int
16main()
17{
18	DB *dbp;
19	int ret;
20
21	(void)remove(database);
22
23	(void)db_create(&dbp, NULL, 0);
24	(void)dbp->set_errfile(dbp, stderr);
25	(void)dbp->set_errpfx(dbp, progname);
26	(void)dbp->set_flags(dbp, DB_DUP);
27	if ((ret = dbp->open(dbp, NULL,
28	    database, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
29		dbp->err(dbp, ret, "%s: DB->open", database);
30		return (1);
31	}
32
33	store(dbp);
34
35	(void)dbp->close(dbp, 0);
36
37	return (0);
38}])
39m4_indent([dnl
40int
41store(dbp)
42	DB *dbp;
43{
44	DBC *dbcp;
45	DBT key, data;
46	int ret;
47m4_blank
48	/*
49	 * The DB handle for a Btree database supporting duplicate data
50	 * items is the argument; acquire a cursor for the database.
51	 */
52	if ((ret = dbp-__GT__cursor(dbp, NULL, &dbcp, 0)) != 0) {
53		dbp-__GT__err(dbp, ret, "DB-__GT__cursor");
54		goto err;
55	}
56m4_blank
57	/* Initialize the key. */
58	memset(&key, 0, sizeof(key));
59	key.data = "new key";
60	key.size = strlen(key.data) + 1;
61m4_blank
62	/* Initialize the data to be the first of two duplicate records. */
63	memset(&data, 0, sizeof(data));
64	data.data = "new key's data: entry #1";
65	data.size = strlen(data.data) + 1;
66m4_blank
67	/* Store the first of the two duplicate records. */
68	if ((ret = dbcp-__GT__c_put(dbcp, &key, &data, DB_KEYFIRST)) != 0)
69		dbp-__GT__err(dbp, ret, "DB-__GT__cursor");
70m4_blank
71	/* Initialize the data to be the second of two duplicate records. */
72	data.data = "new key's data: entry #2";
73	data.size = strlen(data.data) + 1;
74m4_blank
75	/*
76	 * Store the second of the two duplicate records.  No duplicate
77	 * record sort function has been specified, so we explicitly
78	 * store the record as the last of the duplicate set.
79	 */
80	if ((ret = dbcp-__GT__c_put(dbcp, &key, &data, DB_KEYLAST)) != 0)
81		dbp-__GT__err(dbp, ret, "DB-__GT__cursor");
82m4_blank
83err:	if ((ret = dbcp-__GT__c_close(dbcp)) != 0)
84		dbp-__GT__err(dbp, ret, "DBcursor-__GT__close");
85m4_blank
86	return (0);
87}])
88