• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/timemachine/db-4.7.25.NC/docs_src/ref/am_conf/
1m4_ignore([
2#include <sys/types.h>
3
4#include <errno.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include <db.h>
9
10#define DATABASE        "access.db"
11#define WORDLIST        "../test/wordlist"
12
13int rec_display __P((DB *, db_recno_t));
14int recno_display __P((DB *, char *));
15
16int
17main()
18{
19	DB *dbp;
20	DBT key, data;
21	DB_BTREE_STAT *statp;
22	FILE *fp;
23	db_recno_t recno;
24	u_int32_t len;
25	int cnt, ret;
26	char *p, *t, buf[1024], rbuf[1024];
27	const char *progname = "ex_btrec";		/* Program name. */
28
29	/* Open the word database. */
30	if ((fp = fopen(WORDLIST, "r")) == NULL) {
31		fprintf(stderr, "%s: open %s: %s\n",
32		    progname, WORDLIST, db_strerror(errno));
33		return (1);
34	}
35
36	/* Remove the previous database. */
37	(void)remove(DATABASE);
38
39	/* Create and initialize database object, open the database. */
40	if ((ret = db_create(&dbp, NULL, 0)) != 0) {
41		fprintf(stderr,
42		    "%s: db_create: %s\n", progname, db_strerror(ret));
43		return (1);
44	}
45	dbp->set_errfile(dbp, stderr);
46	dbp->set_errpfx(dbp, progname);			/* 1K page sizes. */
47	if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
48		dbp->err(dbp, ret, "set_pagesize");
49		return (1);
50	}						/* Record numbers. */
51	if ((ret = dbp->set_flags(dbp, DB_RECNUM)) != 0) {
52		dbp->err(dbp, ret, "set_flags: DB_RECNUM");
53		return (1);
54	}
55	if ((ret = dbp->open(dbp,
56	    NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
57		dbp->err(dbp, ret, "open: %s", DATABASE);
58		return (1);
59	}
60
61	/*
62	 * Insert records into the database, where the key is the word
63	 * preceded by its record number, and the data is the same, but
64	 * in reverse order.
65	 */
66	memset(&key, 0, sizeof(DBT));
67	memset(&data, 0, sizeof(DBT));
68	for (cnt = 1; cnt <= 1000; ++cnt) {
69		(void)sprintf(buf, "%04d_", cnt);
70		if (fgets(buf + 4, sizeof(buf) - 4, fp) == NULL)
71			break;
72		len = strlen(buf);
73		for (t = rbuf, p = buf + (len - 2); p >= buf;)
74			*t++ = *p--;
75		*t++ = '\0';
76
77		key.data = buf;
78		data.data = rbuf;
79		data.size = key.size = len - 1;
80
81		if ((ret =
82		    dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
83			dbp->err(dbp, ret, "DB->put");
84			if (ret != DB_KEYEXIST)
85				goto err1;
86		}
87	}
88
89	/* Close the word database. */
90	(void)fclose(fp);
91
92	/* Print out the number of records in the database. */
93	if ((ret = dbp->stat(dbp, NULL, &statp, 0)) != 0) {
94		dbp->err(dbp, ret, "DB->stat");
95		goto err1;
96	}
97	printf("%s: database contains %lu records\n",
98	    progname, (u_long)statp->bt_ndata);
99	free(statp);
100
101	/*
102	 * Prompt the user for a record number, then retrieve and display
103	 * that record.
104	 */
105	for (;;) {
106		/* Get a record number. */
107		printf("recno #> ");
108		fflush(stdout);
109		if (fgets(buf, sizeof(buf), stdin) == NULL)
110			break;
111		recno = atoi(buf);
112		rec_display(dbp, recno);
113	}
114
115	recno_display(dbp, "0007atoned");
116
117	if ((ret = dbp->close(dbp, 0)) != 0) {
118		fprintf(stderr,
119		    "%s: DB->close: %s\n", progname, db_strerror(ret));
120		return (1);
121	}
122
123	return (0);
124
125err1:	(void)dbp->close(dbp, 0);
126	return (ret);
127
128}])
129m4_indent([dnl
130int
131rec_display(dbp, recno)
132	DB *dbp;
133	db_recno_t recno;
134{
135	DBT key, data;
136	int ret;
137m4_blank
138	memset(&key, 0, sizeof(key));
139	key.data = &recno;
140	key.size = sizeof(recno);
141	memset(&data, 0, sizeof(data));
142m4_blank
143	if ((ret = dbp-__GT__get(dbp, NULL, &key, &data, DB_SET_RECNO)) != 0)
144		return (ret);
145	printf("data for %lu: %.*s\n",
146	    (u_long)recno, (int)data.size, (char *)data.data);
147	return (0);
148}])
149m4_ignore([
150/*
151 * recno_display --
152 *	Display the record number associated with a specific key.
153 */
154int
155recno_display(dbp, keyvalue)
156	DB *dbp;
157	char *keyvalue;
158{
159	DBC *dbcp;
160	DBT key, data;
161	db_recno_t recno;
162	int ret, t_ret;
163
164	/* Acquire a cursor for the database. */
165	if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
166		dbp->err(dbp, ret, "DB->cursor");
167		goto err;
168	}
169
170	/* Position the cursor. */
171	memset(&key, 0, sizeof(key));
172	key.data = keyvalue;
173	key.size = strlen(keyvalue);
174	memset(&data, 0, sizeof(data));
175	if ((ret = dbcp->c_get(dbcp, &key, &data, DB_SET)) != 0) {
176		dbp->err(dbp, ret, "DBcursor->c_get(DB_SET): %s", keyvalue);
177		goto err;
178	}
179
180	/*
181	 * Request the record number, and store it into appropriately
182	 * sized and aligned local memory.
183	 */
184	memset(&data, 0, sizeof(data));
185	data.data = &recno;
186	data.ulen = sizeof(recno);
187	data.flags = DB_DBT_USERMEM;
188	if ((ret = dbcp->c_get(dbcp, &key, &data, DB_GET_RECNO)) != 0) {
189		dbp->err(dbp, ret, "DBcursor->c_get(DB_GET_RECNO)");
190		goto err;
191	}
192
193	printf("key for requested key was %lu\n", (u_long)recno);
194
195err:	/* Close the cursor. */
196	if ((t_ret = dbcp->c_close(dbcp)) != 0) {
197		if (ret == 0)
198			ret = t_ret;
199		dbp->err(dbp, ret, "DBcursor->close");
200	}
201	return (ret);
202}])
203