2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char copyright
[] =
12 "@(#) Copyright (c) 1996, 1997, 1998\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid
[] = "@(#)db_dump.c 10.19 (Sleepycat) 5/23/98";
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
36 void configure
__P((char *));
37 DB_ENV
*db_init
__P((char *));
38 int main
__P((int, char *[]));
39 void pheader
__P((DB
*, int));
40 void usage
__P((void));
43 *progname
= "db_dump"; /* Program name. */
56 int ch
, checkprint
, dflag
;
60 checkprint
= dflag
= 0;
61 while ((ch
= getopt(argc
, argv
, "df:h:p")) != EOF
)
67 if (freopen(optarg
, "w", stdout
) == NULL
)
89 "the -d and -h options may not both be specified");
92 "the -d and -p options may not both be specified");
94 /* Initialize the environment. */
95 dbenv
= dflag
? NULL
: db_init(home
);
97 /* Open the DB file. */
99 db_open(argv
[0], DB_UNKNOWN
, DB_RDONLY
, 0, dbenv
, NULL
, &dbp
)) != 0)
100 err(1, "%s", argv
[0]);
104 (void)__db_dump(dbp
, NULL
, 1);
105 if ((errno
= dbp
->close(dbp
, 0)) != 0)
110 /* Get a cursor and step through the database. */
111 if ((errno
= dbp
->cursor(dbp
, NULL
, &dbcp
)) != 0) {
112 (void)dbp
->close(dbp
, 0);
116 /* Print out the header. */
117 pheader(dbp
, checkprint
);
119 /* Print out the key/data pairs. */
120 memset(&key
, 0, sizeof(key
));
121 memset(&data
, 0, sizeof(data
));
122 while ((errno
= dbcp
->c_get(dbcp
, &key
, &data
, DB_NEXT
)) == 0) {
123 if (dbp
->type
!= DB_RECNO
&&
124 (errno
= __db_prdbt(&key
, checkprint
, stdout
)) != 0)
126 if ((errno
= __db_prdbt(&data
, checkprint
, stdout
)) != 0)
130 if (errno
!= DB_NOTFOUND
)
131 err(1, "cursor get");
133 if ((errno
= dbp
->close(dbp
, 0)) != 0)
140 * Initialize the environment.
148 if ((dbenv
= (DB_ENV
*)calloc(sizeof(DB_ENV
), 1)) == NULL
) {
152 dbenv
->db_errfile
= stderr
;
153 dbenv
->db_errpfx
= progname
;
156 db_appinit(home
, NULL
, dbenv
, DB_CREATE
| DB_USE_ENVIRON
)) != 0)
157 err(1, "db_appinit");
163 * Write out the header information.
175 printf("format=%s\n", pflag
? "print" : "bytevalue");
178 printf("type=btree\n");
179 if ((errno
= dbp
->stat(dbp
, &btsp
, NULL
, 0)) != 0)
181 if (F_ISSET(dbp
, DB_BT_RECNUM
))
182 printf("recnum=1\n");
183 if (btsp
->bt_maxkey
!= 0)
184 printf("bt_maxkey=%lu\n", (u_long
)btsp
->bt_maxkey
);
185 if (btsp
->bt_minkey
!= 0)
186 printf("bt_minkey=%lu\n", (u_long
)btsp
->bt_minkey
);
189 printf("type=hash\n");
190 hashp
= dbp
->internal
;
191 pgno
= PGNO_METADATA
;
192 if (memp_fget(dbp
->mpf
, &pgno
, 0, &hdr
) == 0) {
193 if (hdr
->ffactor
!= 0)
194 printf("h_ffactor=%lu\n", (u_long
)hdr
->ffactor
);
196 printf("h_nelem=%lu\n", (u_long
)hdr
->nelem
);
197 (void)memp_fput(dbp
->mpf
, hdr
, 0);
201 printf("type=recno\n");
202 if (F_ISSET(dbp
, DB_RE_RENUMBER
))
203 printf("renumber=1\n");
204 if (F_ISSET(dbp
, DB_RE_FIXEDLEN
))
205 printf("re_len=%lu\n", (u_long
)btsp
->bt_re_len
);
206 if (F_ISSET(dbp
, DB_RE_PAD
))
207 printf("re_pad=%#x\n", btsp
->bt_re_pad
);
214 if (F_ISSET(dbp
, DB_AM_DUP
))
215 printf("duplicates=1\n");
217 if (dbp
->dbenv
->db_lorder
!= 0)
218 printf("db_lorder=%lu\n", (u_long
)dbp
->dbenv
->db_lorder
);
220 if (!F_ISSET(dbp
, DB_AM_PGDEF
))
221 printf("db_pagesize=%lu\n", (u_long
)dbp
->pgsize
);
223 printf("HEADER=END\n");
228 * Display the usage message.
233 (void)fprintf(stderr
,
234 "usage: db_dump [-dp] [-f file] [-h home] db_file\n");