blob: b2ed562e950dae3698b018df2f8496723ee2df05 [file] [log] [blame]
drh0de8c112002-07-06 16:32:14 +00001/*
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
13static int pagesize = 1024;
14static int db = -1;
15static int mxPage = 0;
drhc9ac5ca2005-11-04 22:03:30 +000016static int perLine = 32;
drh0de8c112002-07-06 16:32:14 +000017
18static void out_of_memory(void){
19 fprintf(stderr,"Out of memory...\n");
20 exit(1);
21}
22
23static 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);
drhc9ac5ca2005-11-04 22:03:30 +000031 for(i=0; i<pagesize; i += perLine){
drh0de8c112002-07-06 16:32:14 +000032 fprintf(stdout, " %03x: ",i);
drhc9ac5ca2005-11-04 22:03:30 +000033 for(j=0; j<perLine; j++){
drh0de8c112002-07-06 16:32:14 +000034 fprintf(stdout,"%02x ", aData[i+j]);
35 }
drhc9ac5ca2005-11-04 22:03:30 +000036 for(j=0; j<perLine; j++){
drh0de8c112002-07-06 16:32:14 +000037 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
38 }
39 fprintf(stdout,"\n");
40 }
41 free(aData);
42}
43
44int 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}