1*c5c4113dSnw141292
2*c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292 /*
5*c5c4113dSnw141292 ** A utility for printing all or part of an SQLite database file.
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 lseek(db, (iPg-1)*pagesize, SEEK_SET);
31*c5c4113dSnw141292 read(db, aData, pagesize);
32*c5c4113dSnw141292 fprintf(stdout, "Page %d:\n", iPg);
33*c5c4113dSnw141292 for(i=0; i<pagesize; i += 16){
34*c5c4113dSnw141292 fprintf(stdout, " %03x: ",i);
35*c5c4113dSnw141292 for(j=0; j<16; j++){
36*c5c4113dSnw141292 fprintf(stdout,"%02x ", aData[i+j]);
37*c5c4113dSnw141292 }
38*c5c4113dSnw141292 for(j=0; j<16; j++){
39*c5c4113dSnw141292 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
40*c5c4113dSnw141292 }
41*c5c4113dSnw141292 fprintf(stdout,"\n");
42*c5c4113dSnw141292 }
43*c5c4113dSnw141292 free(aData);
44*c5c4113dSnw141292 }
45*c5c4113dSnw141292
main(int argc,char ** argv)46*c5c4113dSnw141292 int main(int argc, char **argv){
47*c5c4113dSnw141292 struct stat sbuf;
48*c5c4113dSnw141292 if( argc<2 ){
49*c5c4113dSnw141292 fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]);
50*c5c4113dSnw141292 exit(1);
51*c5c4113dSnw141292 }
52*c5c4113dSnw141292 db = open(argv[1], O_RDONLY);
53*c5c4113dSnw141292 if( db<0 ){
54*c5c4113dSnw141292 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
55*c5c4113dSnw141292 exit(1);
56*c5c4113dSnw141292 }
57*c5c4113dSnw141292 fstat(db, &sbuf);
58*c5c4113dSnw141292 mxPage = sbuf.st_size/pagesize + 1;
59*c5c4113dSnw141292 if( argc==2 ){
60*c5c4113dSnw141292 int i;
61*c5c4113dSnw141292 for(i=1; i<=mxPage; i++) print_page(i);
62*c5c4113dSnw141292 }else{
63*c5c4113dSnw141292 int i;
64*c5c4113dSnw141292 for(i=2; i<argc; i++){
65*c5c4113dSnw141292 int iStart, iEnd;
66*c5c4113dSnw141292 char *zLeft;
67*c5c4113dSnw141292 iStart = strtol(argv[i], &zLeft, 0);
68*c5c4113dSnw141292 if( zLeft && strcmp(zLeft,"..end")==0 ){
69*c5c4113dSnw141292 iEnd = mxPage;
70*c5c4113dSnw141292 }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
71*c5c4113dSnw141292 iEnd = strtol(&zLeft[2], 0, 0);
72*c5c4113dSnw141292 }else{
73*c5c4113dSnw141292 iEnd = iStart;
74*c5c4113dSnw141292 }
75*c5c4113dSnw141292 if( iStart<1 || iEnd<iStart || iEnd>mxPage ){
76*c5c4113dSnw141292 fprintf(stderr,
77*c5c4113dSnw141292 "Page argument should be LOWER?..UPPER?. Range 1 to %d\n",
78*c5c4113dSnw141292 mxPage);
79*c5c4113dSnw141292 exit(1);
80*c5c4113dSnw141292 }
81*c5c4113dSnw141292 while( iStart<=iEnd ){
82*c5c4113dSnw141292 print_page(iStart);
83*c5c4113dSnw141292 iStart++;
84*c5c4113dSnw141292 }
85*c5c4113dSnw141292 }
86*c5c4113dSnw141292 }
87*c5c4113dSnw141292 close(db);
88*c5c4113dSnw141292 }
89