1 /*
2 * dba dbm analysis/recovery
3 */
4
5 #include <stdio.h>
6 #include <sys/file.h>
7 #include "EXTERN.h"
8 #include "sdbm.h"
9
10 char *progname;
11 extern void oops();
12
13 int
main(int argc,char ** argv)14 main(int argc, char **argv)
15 {
16 int n;
17 char *p;
18 char *name;
19 int pagf;
20
21 progname = argv[0];
22
23 if (p = argv[1]) {
24 name = (char *) malloc((n = strlen(p)) + 5);
25 if (!name)
26 oops("cannot get memory");
27
28 strcpy(name, p);
29 strcpy(name + n, ".pag");
30
31 if ((pagf = open(name, O_RDONLY)) < 0)
32 oops("cannot open %s.", name);
33
34 sdump(pagf);
35 }
36 else
37 oops("usage: %s dbname", progname);
38
39 return 0;
40 }
41
42 void
sdump(int pagf)43 sdump(int pagf)
44 {
45 int b;
46 int n = 0;
47 int t = 0;
48 int o = 0;
49 int e;
50 char pag[PBLKSIZ];
51
52 while ((b = read(pagf, pag, PBLKSIZ)) > 0) {
53 printf("#%d: ", n);
54 if (!okpage(pag))
55 printf("bad\n");
56 else {
57 printf("ok. ");
58 if (!(e = pagestat(pag)))
59 o++;
60 else
61 t += e;
62 }
63 n++;
64 }
65
66 if (b == 0)
67 printf("%d pages (%d holes): %d entries\n", n, o, t);
68 else
69 oops("read failed: block %d", n);
70 }
71
72 int
pagestat(char * pag)73 pagestat(char *pag)
74 {
75 int n;
76 int free;
77 short *ino = (short *) pag;
78
79 if (!(n = ino[0]))
80 printf("no entries.\n");
81 else {
82 free = ino[n] - (n + 1) * sizeof(short);
83 printf("%3d entries %2d%% used free %d.\n",
84 n / 2, ((PBLKSIZ - free) * 100) / PBLKSIZ, free);
85 }
86 return n / 2;
87 }
88