xref: /titanic_44/usr/src/lib/libsqlite/tool/showjournal.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292 
2*c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292 
4*c5c4113dSnw141292 /*
5*c5c4113dSnw141292 ** A utility for printing an SQLite database journal.
6*c5c4113dSnw141292 */
7*c5c4113dSnw141292 #include <stdio.h>
8*c5c4113dSnw141292 #include <ctype.h>
9*c5c4113dSnw141292 #include <sys/types.h>
10*c5c4113dSnw141292 #include <sys/stat.h>
11*c5c4113dSnw141292 #include <fcntl.h>
12*c5c4113dSnw141292 #include <unistd.h>
13*c5c4113dSnw141292 #include <stdlib.h>
14*c5c4113dSnw141292 
15*c5c4113dSnw141292 
16*c5c4113dSnw141292 static int pagesize = 1024;
17*c5c4113dSnw141292 static int db = -1;
18*c5c4113dSnw141292 static int mxPage = 0;
19*c5c4113dSnw141292 
out_of_memory(void)20*c5c4113dSnw141292 static void out_of_memory(void){
21*c5c4113dSnw141292   fprintf(stderr,"Out of memory...\n");
22*c5c4113dSnw141292   exit(1);
23*c5c4113dSnw141292 }
24*c5c4113dSnw141292 
print_page(int iPg)25*c5c4113dSnw141292 static print_page(int iPg){
26*c5c4113dSnw141292   unsigned char *aData;
27*c5c4113dSnw141292   int i, j;
28*c5c4113dSnw141292   aData = malloc(pagesize);
29*c5c4113dSnw141292   if( aData==0 ) out_of_memory();
30*c5c4113dSnw141292   read(db, aData, pagesize);
31*c5c4113dSnw141292   fprintf(stdout, "Page %d:\n", iPg);
32*c5c4113dSnw141292   for(i=0; i<pagesize; i += 16){
33*c5c4113dSnw141292     fprintf(stdout, " %03x: ",i);
34*c5c4113dSnw141292     for(j=0; j<16; j++){
35*c5c4113dSnw141292       fprintf(stdout,"%02x ", aData[i+j]);
36*c5c4113dSnw141292     }
37*c5c4113dSnw141292     for(j=0; j<16; j++){
38*c5c4113dSnw141292       fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
39*c5c4113dSnw141292     }
40*c5c4113dSnw141292     fprintf(stdout,"\n");
41*c5c4113dSnw141292   }
42*c5c4113dSnw141292   free(aData);
43*c5c4113dSnw141292 }
44*c5c4113dSnw141292 
main(int argc,char ** argv)45*c5c4113dSnw141292 int main(int argc, char **argv){
46*c5c4113dSnw141292   struct stat sbuf;
47*c5c4113dSnw141292   unsigned int u;
48*c5c4113dSnw141292   int rc;
49*c5c4113dSnw141292   unsigned char zBuf[10];
50*c5c4113dSnw141292   unsigned char zBuf2[sizeof(u)];
51*c5c4113dSnw141292   if( argc!=2 ){
52*c5c4113dSnw141292     fprintf(stderr,"Usage: %s FILENAME\n", argv[0]);
53*c5c4113dSnw141292     exit(1);
54*c5c4113dSnw141292   }
55*c5c4113dSnw141292   db = open(argv[1], O_RDONLY);
56*c5c4113dSnw141292   if( db<0 ){
57*c5c4113dSnw141292     fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
58*c5c4113dSnw141292     exit(1);
59*c5c4113dSnw141292   }
60*c5c4113dSnw141292   read(db, zBuf, 8);
61*c5c4113dSnw141292   if( zBuf[7]==0xd6 ){
62*c5c4113dSnw141292     read(db, &u, sizeof(u));
63*c5c4113dSnw141292     printf("Records in Journal: %u\n", u);
64*c5c4113dSnw141292     read(db, &u, sizeof(u));
65*c5c4113dSnw141292     printf("Magic Number: 0x%08x\n", u);
66*c5c4113dSnw141292   }
67*c5c4113dSnw141292   read(db, zBuf2, sizeof(zBuf2));
68*c5c4113dSnw141292   u = zBuf2[0]<<24 | zBuf2[1]<<16 | zBuf2[2]<<8 | zBuf2[3];
69*c5c4113dSnw141292   printf("Database Size: %u\n", u);
70*c5c4113dSnw141292   while( read(db, zBuf2, sizeof(zBuf2))==sizeof(zBuf2) ){
71*c5c4113dSnw141292     u = zBuf2[0]<<24 | zBuf2[1]<<16 | zBuf2[2]<<8 | zBuf2[3];
72*c5c4113dSnw141292     print_page(u);
73*c5c4113dSnw141292     if( zBuf[7]==0xd6 ){
74*c5c4113dSnw141292       read(db, &u, sizeof(u));
75*c5c4113dSnw141292       printf("Checksum: 0x%08x\n", u);
76*c5c4113dSnw141292     }
77*c5c4113dSnw141292   }
78*c5c4113dSnw141292   close(db);
79*c5c4113dSnw141292 }
80