您的位置:首页 > 其它

直接读取硬盘扇区

2013-04-19 14:07 253 查看
Linux系统下一切都是文件,可以像使用普通文件一样使用设备,可直接操作设备扇区内容,这种方式不经过文件系统。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>

static void usage(char *prog_name)
{
fprintf(stderr, "usage: %s device start count\n", prog_name);
fprintf(stderr, "example: %s /dev/sda 0 512\n", prog_name);
}

int main(int argc, char *argv[])
{
char buf[4096];
int fd = 0;
int i = 0;
int j = 0;
int start = 0;
int count = 0;
char *device = NULL;

if(argc != 4){
usage(basename(argv[0]));
exit(1);
}

device = argv[1];
start = atol(argv[2]);
count = atol(argv[3]);

fd = open(device, O_RDONLY);
if(-1 == fd){
fprintf(stderr, "cannot open /dev/hda");
exit(1);
}

if(lseek(fd, start, SEEK_SET) != start){
fprintf(stderr, "cannot seek at %d", start);
exit(1);
}

while(count > 0){
int size = count > sizeof(buf) ? sizeof(buf) : count;

read(fd, buf, size);

/* 每行显示16个字节 每两个字节间以空格分开 */
for(i = 0; i < size/16; i++){
fprintf(stdout,"%08x:", i*16+start);
for(j = 0; j < 16; j++){
fprintf(stdout, " %02x", (int)buf[i*16+j] & 0xFF);
}
fprintf(stdout,"\n");
fflush(stdout);
}
count -= size;
start += size;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: