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