drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ** A utility for printing an SQLite database journal. |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <ctype.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <unistd.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | |
| 13 | static int pagesize = 1024; |
| 14 | static int db = -1; |
| 15 | static int mxPage = 0; |
| 16 | |
| 17 | static void out_of_memory(void){ |
| 18 | fprintf(stderr,"Out of memory...\n"); |
| 19 | exit(1); |
| 20 | } |
| 21 | |
| 22 | static print_page(int iPg){ |
| 23 | unsigned char *aData; |
| 24 | int i, j; |
| 25 | aData = malloc(pagesize); |
| 26 | if( aData==0 ) out_of_memory(); |
| 27 | read(db, aData, pagesize); |
| 28 | fprintf(stdout, "Page %d:\n", iPg); |
| 29 | for(i=0; i<pagesize; i += 16){ |
| 30 | fprintf(stdout, " %03x: ",i); |
| 31 | for(j=0; j<16; j++){ |
| 32 | fprintf(stdout,"%02x ", aData[i+j]); |
| 33 | } |
| 34 | for(j=0; j<16; j++){ |
| 35 | fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); |
| 36 | } |
| 37 | fprintf(stdout,"\n"); |
| 38 | } |
| 39 | free(aData); |
| 40 | } |
| 41 | |
| 42 | int main(int argc, char **argv){ |
| 43 | struct stat sbuf; |
| 44 | unsigned int u; |
| 45 | int rc; |
| 46 | char zBuf[100]; |
| 47 | if( argc!=2 ){ |
| 48 | fprintf(stderr,"Usage: %s FILENAME\n", argv[0]); |
| 49 | exit(1); |
| 50 | } |
| 51 | db = open(argv[1], O_RDONLY); |
| 52 | if( db<0 ){ |
| 53 | fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
| 54 | exit(1); |
| 55 | } |
| 56 | read(db, zBuf, 8); |
| 57 | read(db, &u, sizeof(u)); |
| 58 | printf("Database Size: %u\n", u); |
| 59 | while( read(db, &u, sizeof(u))==sizeof(u) ){ |
| 60 | print_page(u); |
| 61 | } |
| 62 | close(db); |
| 63 | } |