1m4_ignore([dnl 2#include <sys/types.h> 3 4#include <stdlib.h> 5#include <string.h> 6 7#include <db.h> 8 9#define DATABASE "access.db" 10#define progname "t" 11 12int recno_build(DB *); 13 14int 15main() 16{ 17 DB *dbp; 18 int ret; 19 20 /* Remove the previous database. */ 21 (void)remove(DATABASE); 22 23 /* Create and initialize database object, open the database. */ 24 if ((ret = db_create(&dbp, NULL, 0)) != 0) { 25 fprintf(stderr, 26 "%s: db_create: %s\n", progname, db_strerror(ret)); 27 return (1); 28 } 29 dbp->set_errfile(dbp, stderr); 30 dbp->set_errpfx(dbp, progname); 31 if ((ret = dbp->open(dbp, 32 NULL, DATABASE, NULL, DB_RECNO, DB_CREATE, 0664)) != 0) { 33 dbp->err(dbp, ret, "%s: open", DATABASE); 34 return (1); 35 } 36 37 (void)recno_build(dbp); 38 39 return (0); 40}]) 41 42m4_indent([dnl 43int 44recno_build(dbp) 45 DB *dbp; 46{ 47 DBC *dbcp; 48 DBT key, data; 49 db_recno_t recno; 50 u_int32_t len; 51 int ret; 52 char buf__LB__1024__RB__; 53m4_blank 54 /* Insert records into the database. */ 55 memset(&key, 0, sizeof(DBT)); 56 memset(&data, 0, sizeof(DBT)); 57 for (recno = 1;; ++recno) { 58 printf("record #%lu__GT__ ", (u_long)recno); 59 fflush(stdout); 60 if (fgets(buf, sizeof(buf), stdin) == NULL) 61 break; 62 if ((len = strlen(buf)) __LT__= 1) 63 continue; 64m4_blank 65 key.data = &recno; 66 key.size = sizeof(recno); 67 data.data = buf; 68 data.size = len - 1; 69m4_blank 70 switch (ret = dbp-__GT__put(dbp, NULL, &key, &data, 0)) { 71 case 0: 72 break; 73 default: 74 dbp-__GT__err(dbp, ret, "DB-__GT__put"); 75 break; 76 } 77 } 78 printf("\n"); 79m4_blank 80 /* Acquire a cursor for the database. */ 81 if ((ret = dbp-__GT__cursor(dbp, NULL, &dbcp, 0)) != 0) { 82 dbp-__GT__err(dbp, ret, "DB-__GT__cursor"); 83 return (1); 84 } 85m4_blank 86 /* Re-initialize the key/data pair. */ 87 memset(&key, 0, sizeof(key)); 88 memset(&data, 0, sizeof(data)); 89m4_blank 90 /* Walk through the database and print out the key/data pairs. */ 91 while ((ret = dbcp-__GT__c_get(dbcp, &key, &data, DB_NEXT)) == 0) 92 printf("%lu : %.*s\n", 93 *(u_long *)key.data, (int)data.size, (char *)data.data); 94 if (ret != DB_NOTFOUND) 95 dbp-__GT__err(dbp, ret, "DBcursor-__GT__get"); 96m4_blank 97 /* Close the cursor. */ 98 if ((ret = dbcp-__GT__c_close(dbcp)) != 0) { 99 dbp-__GT__err(dbp, ret, "DBcursor-__GT__close"); 100 return (1); 101 } 102 return (0); 103}]) 104