drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** A utility for printing all or part of an SQLite database file. |
| 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; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 16 | static int perLine = 32; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 17 | |
| 18 | static void out_of_memory(void){ |
| 19 | fprintf(stderr,"Out of memory...\n"); |
| 20 | exit(1); |
| 21 | } |
| 22 | |
| 23 | static print_page(int iPg){ |
| 24 | unsigned char *aData; |
| 25 | int i, j; |
| 26 | aData = malloc(pagesize); |
| 27 | if( aData==0 ) out_of_memory(); |
| 28 | lseek(db, (iPg-1)*pagesize, SEEK_SET); |
| 29 | read(db, aData, pagesize); |
| 30 | fprintf(stdout, "Page %d:\n", iPg); |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 31 | for(i=0; i<pagesize; i += perLine){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 32 | fprintf(stdout, " %03x: ",i); |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 33 | for(j=0; j<perLine; j++){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 34 | fprintf(stdout,"%02x ", aData[i+j]); |
| 35 | } |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 36 | for(j=0; j<perLine; j++){ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 37 | fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); |
| 38 | } |
| 39 | fprintf(stdout,"\n"); |
| 40 | } |
| 41 | free(aData); |
| 42 | } |
| 43 | |
| 44 | int main(int argc, char **argv){ |
| 45 | struct stat sbuf; |
| 46 | if( argc<2 ){ |
| 47 | fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]); |
| 48 | exit(1); |
| 49 | } |
| 50 | db = open(argv[1], O_RDONLY); |
| 51 | if( db<0 ){ |
| 52 | fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
| 53 | exit(1); |
| 54 | } |
| 55 | fstat(db, &sbuf); |
| 56 | mxPage = sbuf.st_size/pagesize + 1; |
| 57 | if( argc==2 ){ |
| 58 | int i; |
| 59 | for(i=1; i<=mxPage; i++) print_page(i); |
| 60 | }else{ |
| 61 | int i; |
| 62 | for(i=2; i<argc; i++){ |
| 63 | int iStart, iEnd; |
| 64 | char *zLeft; |
| 65 | iStart = strtol(argv[i], &zLeft, 0); |
| 66 | if( zLeft && strcmp(zLeft,"..end")==0 ){ |
| 67 | iEnd = mxPage; |
| 68 | }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
| 69 | iEnd = strtol(&zLeft[2], 0, 0); |
| 70 | }else{ |
| 71 | iEnd = iStart; |
| 72 | } |
| 73 | if( iStart<1 || iEnd<iStart || iEnd>mxPage ){ |
| 74 | fprintf(stderr, |
| 75 | "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
| 76 | mxPage); |
| 77 | exit(1); |
| 78 | } |
| 79 | while( iStart<=iEnd ){ |
| 80 | print_page(iStart); |
| 81 | iStart++; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | close(db); |
| 86 | } |