// $Id: getblock.c,v 1.1 2004-12-09 01:40:59+09 kaiya Exp kaiya $ /* Spec Get nth block from a file Usage ./a.out block_number < filesystem_file > output_file Limit block_number: natural number 0<=block_number<1440 filesytem_file should be simple ext2 file system dump; i.e. block size=1024B, number of blocks=1440. Comple gcc thisfile.c */ #include #include #include #include // #include #define BLOCKSIZE 1024 main(int argc, char* argv[]){ unsigned char block[BLOCKSIZE]; int nb, c; if(argc<2) exit(1); nb=atoi(argv[1]); if(nb<0 || nb>1440) exit(2); fprintf(stderr, "get block #\%d\n", nb); for(c=0; read(0, block, BLOCKSIZE)==BLOCKSIZE; c++){ if(c==nb){ write(1, block, BLOCKSIZE); break; } } }