Annotation of embedaddon/sqlite3/tool/extract.c, revision 1.1.1.1

1.1       misho       1: /*
                      2: ** Extract a range of bytes from a file.
                      3: **
                      4: ** Usage:
                      5: **
                      6: **    extract FILENAME OFFSET AMOUNT
                      7: **
                      8: ** The bytes are written to standard output.
                      9: */
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
                     12: 
                     13: int main(int argc, char **argv){
                     14:   FILE *f;
                     15:   char *zBuf;
                     16:   int ofst;
                     17:   int n;
                     18:   size_t got;
                     19: 
                     20:   if( argc!=4 ){
                     21:     fprintf(stderr, "Usage: %s FILENAME OFFSET AMOUNT\n", *argv);
                     22:     return 1;
                     23:   }
                     24:   f = fopen(argv[1], "rb");
                     25:   if( f==0 ){
                     26:     fprintf(stderr, "cannot open \"%s\"\n", argv[1]);
                     27:     return 1;
                     28:   }
                     29:   ofst = atoi(argv[2]);
                     30:   n = atoi(argv[3]);
                     31:   zBuf = malloc( n );
                     32:   if( zBuf==0 ){
                     33:     fprintf(stderr, "out of memory\n");
                     34:     return 1;
                     35:   }
                     36:   fseek(f, ofst, SEEK_SET);
                     37:   got = fread(zBuf, 1, n, f);
                     38:   fclose(f);
                     39:   if( got<n ){
                     40:     fprintf(stderr, "got only %d of %d bytes\n", got, n);
                     41:     return 1;
                     42:   }else{
                     43:     fwrite(zBuf, 1, n, stdout);
                     44:   }
                     45:   return 0;
                     46: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>