Created
April 21, 2012 09:41
-
-
Save syucream/2436216 to your computer and use it in GitHub Desktop.
指定のディスクデバイスに、read() システムコール経由で小サイズリードを発行するコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#define BUFSIZE 4096 // 8 sector counts | |
/* | |
* 指定のディスクデバイスに、read() システムコール経由で小サイズリードを発行する | |
* | |
* 例: $ ./dread /dev/sda 30000000 | |
* /dev/sda に、LBA=30000000 を対象にして4kBのリードを発行する | |
* | |
*/ | |
int main( int argc, char *argv[] ) | |
{ | |
int i, fd; | |
char buf[BUFSIZE]; | |
off_t offset = 0; | |
if( argc != 3 ){ | |
printf("Usage: ./dread [target device] [offset LBA]\n"); | |
exit(1); | |
} | |
// open | |
fd = open( argv[1], O_RDONLY ); | |
if( fd == -1 ){ | |
perror( argv[1] ); | |
exit(1); | |
} | |
// lseek | |
offset = atol( argv[2] ); | |
for(i=0; i<512; i++) lseek(fd, offset, SEEK_CUR); | |
// read | |
read(fd, buf, BUFSIZE); | |
// close | |
close(fd); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment