Update.
[glibc.git] / db2 / progs / db_dump185 / db_dump185.c
blob5ec7673f1bfc55ec2dd41f108047a4c36e503e36
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char copyright[] =
12 "@(#) Copyright (c) 1997\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid[] = "@(#)db_dump185.c 10.8 (Sleepycat) 9/21/97";
15 #endif
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #endif
29 #include "db_185.h"
30 #include "clib_ext.h"
32 /* Hash Table Information */
33 typedef struct hashhdr { /* Disk resident portion */
34 int magic; /* Magic NO for hash tables */
35 int version; /* Version ID */
36 u_int32_t lorder; /* Byte Order */
37 int bsize; /* Bucket/Page Size */
38 int bshift; /* Bucket shift */
39 int dsize; /* Directory Size */
40 int ssize; /* Segment Size */
41 int sshift; /* Segment shift */
42 int ovfl_point; /* Where overflow pages are being
43 * allocated */
44 int last_freed; /* Last overflow page freed */
45 int max_bucket; /* ID of Maximum bucket in use */
46 int high_mask; /* Mask to modulo into entire table */
47 int low_mask; /* Mask to modulo into lower half of
48 * table */
49 int ffactor; /* Fill factor */
50 int nkeys; /* Number of keys in hash table */
51 } HASHHDR;
53 typedef struct htab { /* Memory resident data structure */
54 HASHHDR hdr; /* Header */
55 } HTAB;
57 typedef struct _epgno {
58 u_int32_t pgno; /* the page number */
59 u_int16_t index; /* the index on the page */
60 } EPGNO;
62 typedef struct _epg {
63 void *page; /* the (pinned) page */
64 u_int16_t index; /* the index on the page */
65 } EPG;
67 typedef struct _cursor {
68 EPGNO pg; /* B: Saved tree reference. */
69 DBT key; /* B: Saved key, or key.data == NULL. */
70 u_int32_t rcursor; /* R: recno cursor (1-based) */
72 #define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */
73 #define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */
74 #define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */
75 #define CURS_INIT 0x08 /* RB: Cursor initialized. */
76 u_int8_t flags;
77 } CURSOR;
79 /* The in-memory btree/recno data structure. */
80 typedef struct _btree {
81 void *bt_mp; /* memory pool cookie */
83 void *bt_dbp; /* pointer to enclosing DB */
85 EPG bt_cur; /* current (pinned) page */
86 void *bt_pinned; /* page pinned across calls */
88 CURSOR bt_cursor; /* cursor */
90 EPGNO bt_stack[50]; /* stack of parent pages */
91 EPGNO *bt_sp; /* current stack pointer */
93 DBT bt_rkey; /* returned key */
94 DBT bt_rdata; /* returned data */
96 int bt_fd; /* tree file descriptor */
98 u_int32_t bt_free; /* next free page */
99 u_int32_t bt_psize; /* page size */
100 u_int16_t bt_ovflsize; /* cut-off for key/data overflow */
101 int bt_lorder; /* byte order */
102 /* sorted order */
103 enum { NOT, BACK, FORWARD } bt_order;
104 EPGNO bt_last; /* last insert */
106 /* B: key comparison function */
107 int (*bt_cmp) __P((const DBT *, const DBT *));
108 /* B: prefix comparison function */
109 size_t (*bt_pfx) __P((const DBT *, const DBT *));
110 /* R: recno input function */
111 int (*bt_irec) __P((struct _btree *, u_int32_t));
113 FILE *bt_rfp; /* R: record FILE pointer */
114 int bt_rfd; /* R: record file descriptor */
116 void *bt_cmap; /* R: current point in mapped space */
117 void *bt_smap; /* R: start of mapped space */
118 void *bt_emap; /* R: end of mapped space */
119 size_t bt_msize; /* R: size of mapped region. */
121 u_int32_t bt_nrecs; /* R: number of records */
122 size_t bt_reclen; /* R: fixed record length */
123 u_char bt_bval; /* R: delimiting byte/pad character */
126 * NB:
127 * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
129 #define B_INMEM 0x00001 /* in-memory tree */
130 #define B_METADIRTY 0x00002 /* need to write metadata */
131 #define B_MODIFIED 0x00004 /* tree modified */
132 #define B_NEEDSWAP 0x00008 /* if byte order requires swapping */
133 #define B_RDONLY 0x00010 /* read-only tree */
135 #define B_NODUPS 0x00020 /* no duplicate keys permitted */
136 #define R_RECNO 0x00080 /* record oriented tree */
138 #define R_CLOSEFP 0x00040 /* opened a file pointer */
139 #define R_EOF 0x00100 /* end of input file reached. */
140 #define R_FIXLEN 0x00200 /* fixed length records */
141 #define R_MEMMAPPED 0x00400 /* memory mapped file. */
142 #define R_INMEM 0x00800 /* in-memory file */
143 #define R_MODIFIED 0x01000 /* modified file */
144 #define R_RDONLY 0x02000 /* read-only file */
146 #define B_DB_LOCK 0x04000 /* DB_LOCK specified. */
147 #define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */
148 #define B_DB_TXN 0x10000 /* DB_TXN specified. */
149 u_int32_t flags;
150 } BTREE;
152 void db_185_btree __P((DB *, int));
153 void db_185_hash __P((DB *, int));
154 void dbt_dump __P((DBT *));
155 void dbt_print __P((DBT *));
156 int main __P((int, char *[]));
157 void usage __P((void));
159 const char
160 *progname = "db_dump185"; /* Program name. */
163 main(argc, argv)
164 int argc;
165 char *argv[];
167 extern char *optarg;
168 extern int optind;
169 DB *dbp;
170 DBT key, data;
171 int ch, pflag, rval;
173 pflag = 0;
174 while ((ch = getopt(argc, argv, "f:p")) != EOF)
175 switch (ch) {
176 case 'f':
177 if (freopen(optarg, "w", stdout) == NULL)
178 err(1, "%s", optarg);
179 break;
180 case 'p':
181 pflag = 1;
182 break;
183 case '?':
184 default:
185 usage();
187 argc -= optind;
188 argv += optind;
190 if (argc != 1)
191 usage();
193 if ((dbp = dbopen(argv[0], O_RDONLY, 0, DB_BTREE, NULL)) == NULL) {
194 if ((dbp = dbopen(argv[0], O_RDONLY, 0, DB_HASH, NULL)) == NULL)
195 err(1, "%s", argv[0]);
196 db_185_hash(dbp, pflag);
197 } else
198 db_185_btree(dbp, pflag);
201 * !!!
202 * DB 1.85 DBTs are a subset of DB 2.0 DBTs, so we just use the
203 * new dump/print routines.
205 if (pflag)
206 while (!(rval = dbp->seq(dbp, &key, &data, R_NEXT))) {
207 dbt_print(&key);
208 dbt_print(&data);
210 else
211 while (!(rval = dbp->seq(dbp, &key, &data, R_NEXT))) {
212 dbt_dump(&key);
213 dbt_dump(&data);
216 if (rval == -1)
217 err(1, "seq");
218 return (0);
222 * db_185_hash --
223 * Dump out hash header information.
225 void
226 db_185_hash(dbp, pflag)
227 DB *dbp;
228 int pflag;
230 HTAB *hashp;
232 hashp = dbp->internal;
234 printf("format=%s\n", pflag ? "print" : "bytevalue");
235 printf("type=hash\n");
236 printf("h_ffactor=%lu\n", (u_long)hashp->hdr.ffactor);
237 #ifdef NOT_AVAILABLE_IN_DB_185
238 printf("h_nelem=%lu\n", (u_long)hashp->hdr.nelem);
239 #endif
240 if (hashp->hdr.lorder != 0)
241 printf("db_lorder=%lu\n", (u_long)hashp->hdr.lorder);
242 printf("db_pagesize=%lu\n", (u_long)hashp->hdr.bsize);
243 printf("HEADER=END\n");
247 * db_185_btree --
248 * Dump out btree header information.
250 void
251 db_185_btree(dbp, pflag)
252 DB *dbp;
253 int pflag;
255 BTREE *btp;
257 btp = dbp->internal;
259 printf("format=%s\n", pflag ? "print" : "bytevalue");
260 printf("type=btree\n");
261 #ifdef NOT_AVAILABLE_IN_185
262 printf("bt_minkey=%lu\n", (u_long)XXX);
263 printf("bt_maxkey=%lu\n", (u_long)XXX);
264 #endif
265 if (btp->bt_lorder != 0)
266 printf("db_lorder=%lu\n", (u_long)btp->bt_lorder);
267 printf("db_pagesize=%lu\n", (u_long)btp->bt_psize);
268 if (!(btp->flags & B_NODUPS))
269 printf("duplicates=1\n");
270 printf("HEADER=END\n");
273 static char hex[] = "0123456789abcdef";
276 * dbt_dump --
277 * Write out a key or data item using byte values.
279 void
280 dbt_dump(dbtp)
281 DBT *dbtp;
283 size_t len;
284 u_int8_t *p;
286 for (len = dbtp->size, p = dbtp->data; len--; ++p)
287 (void)printf("%c%c",
288 hex[(*p & 0xf0) >> 4], hex[*p & 0x0f]);
289 printf("\n");
293 * dbt_print --
294 * Write out a key or data item using printable characters.
296 void
297 dbt_print(dbtp)
298 DBT *dbtp;
300 size_t len;
301 u_int8_t *p;
303 for (len = dbtp->size, p = dbtp->data; len--; ++p)
304 if (isprint(*p)) {
305 if (*p == '\\')
306 (void)printf("\\");
307 (void)printf("%c", *p);
308 } else
309 (void)printf("\\%c%c",
310 hex[(*p & 0xf0) >> 4], hex[*p & 0x0f]);
311 printf("\n");
315 * usage --
316 * Display the usage message.
318 void
319 usage()
321 (void)fprintf(stderr, "usage: db_dump [-p] [-f file] db_file\n");
322 exit(1);