Update.
[glibc.git] / db2 / progs / db_dump / db_dump.c
blobf532bc2779b7b9622eb02429b16c7cfa85724d13
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
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";
15 #endif
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #endif
28 #include "db_int.h"
29 #include "db_page.h"
30 #include "btree.h"
31 #include "hash.h"
32 #include "clib_ext.h"
34 #undef stat
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));
42 const char
43 *progname = "db_dump"; /* Program name. */
45 int
46 main(argc, argv)
47 int argc;
48 char *argv[];
50 extern char *optarg;
51 extern int optind;
52 DB *dbp;
53 DBC *dbcp;
54 DBT key, data;
55 DB_ENV *dbenv;
56 int ch, checkprint, dflag;
57 char *home;
59 home = NULL;
60 checkprint = dflag = 0;
61 while ((ch = getopt(argc, argv, "df:h:p")) != EOF)
62 switch (ch) {
63 case 'd':
64 dflag = 1;
65 break;
66 case 'f':
67 if (freopen(optarg, "w", stdout) == NULL)
68 err(1, "%s", optarg);
69 break;
70 case 'h':
71 home = optarg;
72 break;
73 case 'p':
74 checkprint = 1;
75 break;
76 case '?':
77 default:
78 usage();
80 argc -= optind;
81 argv += optind;
83 if (argc != 1)
84 usage();
86 if (dflag) {
87 if (home != NULL)
88 errx(1,
89 "the -d and -h options may not both be specified");
90 if (checkprint)
91 errx(1,
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. */
98 if ((errno =
99 db_open(argv[0], DB_UNKNOWN, DB_RDONLY, 0, dbenv, NULL, &dbp)) != 0)
100 err(1, "%s", argv[0]);
102 /* DB dump. */
103 if (dflag) {
104 (void)__db_dump(dbp, NULL, 1);
105 if ((errno = dbp->close(dbp, 0)) != 0)
106 err(1, "close");
107 exit (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);
113 err(1, "cursor");
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)
125 break;
126 if ((errno = __db_prdbt(&data, checkprint, stdout)) != 0)
127 break;
130 if (errno != DB_NOTFOUND)
131 err(1, "cursor get");
133 if ((errno = dbp->close(dbp, 0)) != 0)
134 err(1, "close");
135 return (0);
139 * db_init --
140 * Initialize the environment.
142 DB_ENV *
143 db_init(home)
144 char *home;
146 DB_ENV *dbenv;
148 if ((dbenv = (DB_ENV *)calloc(sizeof(DB_ENV), 1)) == NULL) {
149 errno = ENOMEM;
150 err(1, NULL);
152 dbenv->db_errfile = stderr;
153 dbenv->db_errpfx = progname;
155 if ((errno =
156 db_appinit(home, NULL, dbenv, DB_CREATE | DB_USE_ENVIRON)) != 0)
157 err(1, "db_appinit");
158 return (dbenv);
162 * pheader --
163 * Write out the header information.
165 void
166 pheader(dbp, pflag)
167 DB *dbp;
168 int pflag;
170 DB_BTREE_STAT *btsp;
171 HTAB *hashp;
172 HASHHDR *hdr;
173 db_pgno_t pgno;
175 printf("format=%s\n", pflag ? "print" : "bytevalue");
176 switch (dbp->type) {
177 case DB_BTREE:
178 printf("type=btree\n");
179 if ((errno = dbp->stat(dbp, &btsp, NULL, 0)) != 0)
180 err(1, "dbp->stat");
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);
187 break;
188 case DB_HASH:
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);
195 if (hdr->nelem != 0)
196 printf("h_nelem=%lu\n", (u_long)hdr->nelem);
197 (void)memp_fput(dbp->mpf, hdr, 0);
199 break;
200 case DB_RECNO:
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);
208 break;
209 case DB_UNKNOWN:
210 abort();
211 /* NOTREACHED */
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");
227 * usage --
228 * Display the usage message.
230 void
231 usage()
233 (void)fprintf(stderr,
234 "usage: db_dump [-dp] [-f file] [-h home] db_file\n");
235 exit(1);