#include #include #include #include #include #include int main(int argc, char**argv) { unsigned char buffer[512]; int fildes; /* Use a bigger value if your HD is bigger than 1 TB */ off_t too_far=(off_t)1024*1024*1024*1024; if(argc==1) { printf("%s device_to_read\n", argv[1]); printf("ie: %s /dev/sda\n", argv[1]); return 1; } fildes=open(argv[1], O_RDONLY|O_BINARY); if(fildes==-1) { printf("Can't read %s\n", argv[1]); } { /* Test 1*/ printf("lseek 0 (should be OK): "); if(lseek(fildes, 0, SEEK_SET)==(off_t)(-1)) { printf("Failed\n"); return 1; } else printf("OK\n"); /* Test 2*/ printf("pread 0 (should be OK): "); if(pread(fildes, buffer, 512, 0)==(-1)) { printf("Failed\n"); return 1; } else printf("OK\n"); /* Test 3: Go at the end of the disk */ printf("lseek after the end of the disk (should be Failed): "); if(lseek(fildes, too_far, SEEK_SET)==(off_t)(-1)) printf("Failed\n"); else { printf("OK\n"); printf("You must use a device, not a file\n"); return 1; } /* Test 4*/ printf("pread 0 (should be OK): "); if(pread(fildes, buffer, 512, 0)==(-1)) printf("Failed <= BUG\n"); else printf("OK\n"); close(fildes); } return 0; }