xref: /illumos-gate/usr/src/lib/libsqlite/tool/showdb.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** A utility for printing all or part of an SQLite database file.
37c478bd9Sstevel@tonic-gate */
47c478bd9Sstevel@tonic-gate #include <stdio.h>
57c478bd9Sstevel@tonic-gate #include <ctype.h>
67c478bd9Sstevel@tonic-gate #include <sys/types.h>
77c478bd9Sstevel@tonic-gate #include <sys/stat.h>
87c478bd9Sstevel@tonic-gate #include <fcntl.h>
97c478bd9Sstevel@tonic-gate #include <unistd.h>
107c478bd9Sstevel@tonic-gate #include <stdlib.h>
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate static int pagesize = 1024;
147c478bd9Sstevel@tonic-gate static int db = -1;
157c478bd9Sstevel@tonic-gate static int mxPage = 0;
167c478bd9Sstevel@tonic-gate 
out_of_memory(void)177c478bd9Sstevel@tonic-gate static void out_of_memory(void){
187c478bd9Sstevel@tonic-gate   fprintf(stderr,"Out of memory...\n");
197c478bd9Sstevel@tonic-gate   exit(1);
207c478bd9Sstevel@tonic-gate }
217c478bd9Sstevel@tonic-gate 
print_page(int iPg)227c478bd9Sstevel@tonic-gate static print_page(int iPg){
237c478bd9Sstevel@tonic-gate   unsigned char *aData;
247c478bd9Sstevel@tonic-gate   int i, j;
257c478bd9Sstevel@tonic-gate   aData = malloc(pagesize);
267c478bd9Sstevel@tonic-gate   if( aData==0 ) out_of_memory();
277c478bd9Sstevel@tonic-gate   lseek(db, (iPg-1)*pagesize, SEEK_SET);
287c478bd9Sstevel@tonic-gate   read(db, aData, pagesize);
297c478bd9Sstevel@tonic-gate   fprintf(stdout, "Page %d:\n", iPg);
307c478bd9Sstevel@tonic-gate   for(i=0; i<pagesize; i += 16){
317c478bd9Sstevel@tonic-gate     fprintf(stdout, " %03x: ",i);
327c478bd9Sstevel@tonic-gate     for(j=0; j<16; j++){
337c478bd9Sstevel@tonic-gate       fprintf(stdout,"%02x ", aData[i+j]);
347c478bd9Sstevel@tonic-gate     }
357c478bd9Sstevel@tonic-gate     for(j=0; j<16; j++){
367c478bd9Sstevel@tonic-gate       fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
377c478bd9Sstevel@tonic-gate     }
387c478bd9Sstevel@tonic-gate     fprintf(stdout,"\n");
397c478bd9Sstevel@tonic-gate   }
407c478bd9Sstevel@tonic-gate   free(aData);
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate 
main(int argc,char ** argv)437c478bd9Sstevel@tonic-gate int main(int argc, char **argv){
447c478bd9Sstevel@tonic-gate   struct stat sbuf;
457c478bd9Sstevel@tonic-gate   if( argc<2 ){
467c478bd9Sstevel@tonic-gate     fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]);
477c478bd9Sstevel@tonic-gate     exit(1);
487c478bd9Sstevel@tonic-gate   }
497c478bd9Sstevel@tonic-gate   db = open(argv[1], O_RDONLY);
507c478bd9Sstevel@tonic-gate   if( db<0 ){
517c478bd9Sstevel@tonic-gate     fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
527c478bd9Sstevel@tonic-gate     exit(1);
537c478bd9Sstevel@tonic-gate   }
547c478bd9Sstevel@tonic-gate   fstat(db, &sbuf);
557c478bd9Sstevel@tonic-gate   mxPage = sbuf.st_size/pagesize + 1;
567c478bd9Sstevel@tonic-gate   if( argc==2 ){
577c478bd9Sstevel@tonic-gate     int i;
587c478bd9Sstevel@tonic-gate     for(i=1; i<=mxPage; i++) print_page(i);
597c478bd9Sstevel@tonic-gate   }else{
607c478bd9Sstevel@tonic-gate     int i;
617c478bd9Sstevel@tonic-gate     for(i=2; i<argc; i++){
627c478bd9Sstevel@tonic-gate       int iStart, iEnd;
637c478bd9Sstevel@tonic-gate       char *zLeft;
647c478bd9Sstevel@tonic-gate       iStart = strtol(argv[i], &zLeft, 0);
657c478bd9Sstevel@tonic-gate       if( zLeft && strcmp(zLeft,"..end")==0 ){
667c478bd9Sstevel@tonic-gate         iEnd = mxPage;
677c478bd9Sstevel@tonic-gate       }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
687c478bd9Sstevel@tonic-gate         iEnd = strtol(&zLeft[2], 0, 0);
697c478bd9Sstevel@tonic-gate       }else{
707c478bd9Sstevel@tonic-gate         iEnd = iStart;
717c478bd9Sstevel@tonic-gate       }
727c478bd9Sstevel@tonic-gate       if( iStart<1 || iEnd<iStart || iEnd>mxPage ){
737c478bd9Sstevel@tonic-gate         fprintf(stderr,
747c478bd9Sstevel@tonic-gate           "Page argument should be LOWER?..UPPER?.  Range 1 to %d\n",
757c478bd9Sstevel@tonic-gate           mxPage);
767c478bd9Sstevel@tonic-gate         exit(1);
777c478bd9Sstevel@tonic-gate       }
787c478bd9Sstevel@tonic-gate       while( iStart<=iEnd ){
797c478bd9Sstevel@tonic-gate         print_page(iStart);
807c478bd9Sstevel@tonic-gate         iStart++;
817c478bd9Sstevel@tonic-gate       }
827c478bd9Sstevel@tonic-gate     }
837c478bd9Sstevel@tonic-gate   }
847c478bd9Sstevel@tonic-gate   close(db);
857c478bd9Sstevel@tonic-gate }
86