Skip to content

Instantly share code, notes, and snippets.

@syucream
Created April 21, 2012 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syucream/2436216 to your computer and use it in GitHub Desktop.
Save syucream/2436216 to your computer and use it in GitHub Desktop.
指定のディスクデバイスに、read() システムコール経由で小サイズリードを発行するコード
#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