当前位置: 技术问答>linux和unix
获取硬件信息的源代码:在遍历文件链表时出现段错误,请高手指教!
来源: 互联网 发布时间:2016-09-10
本文导语: 问题描述:运行时出现segment fault 用gdb 看了一下,发现是在cur_node = cur_node->next处出错的. 可能是越界了,但是我在构造文件链表时已经考虑到这个问题了~还是出错. 请指教~ ------------------------------------- 代码 ---------------...
问题描述:运行时出现segment fault
用gdb 看了一下,发现是在cur_node = cur_node->next处出错的.
可能是越界了,但是我在构造文件链表时已经考虑到这个问题了~还是出错.
请指教~
-------------------------------------
代码
-------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*
IDE硬盘的子程序
*/
static void dump_identity(const struct hd_driveid *id)
{
const unsigned short int *id_regs= (const void*)id;
printf("Model=%.40s,FwRev=%.8s,SerialNo=%.20sn",id->model,id->fw_rev,id->serial_no);
};
void GetIdeNum(const char *t_devname)
{
struct hd_driveid id;
char *ide_devname = *t_devname;
int fd = -1;
fd = open(ide_devname, O_RDONLY|O_NONBLOCK);
if (fd d_name,".") == 0 ) || (strcmp(drt->d_name,"..") == 0) )
{
continue; //忽略.目录和..目录
}
next_node = (dirlink*)malloc(sizeof(dirlink));
//链表操作
if(NULL == head_node)
{
head_node = next_node; //首节点
}
else
{
cur_node->next = next_node;//当前节点
}
cur_node = next_node;
// copy the filename which get from drt into the cur_node
strcpy(cur_node->d_name,drt->d_name);
}
cur_node->next = NULL; //最后一个节点
closedir(dir);
return head_node; //返回链表头指针
}
void print_file_info(dirlink* head_node)
{
struct stat file_stat;
dirlink* cur_node = head_node;
info file_info;
char devname[30] = {"/dev/"};
char *ide_table[9] = {"hda", "hdb", "hdc", "hdd", "hde", "hdf", "hdg", "hdh", "hdi"};
char *scsi_table[9] = {"sda", "sdb", "sdc", "sdd", "sde", "sdf", "sdg", "sdh", "sdi"};
while( NULL != cur_node ) //遍历链表
{
//may be problem in here!
//check the stat()
if ( stat(cur_node->d_name,&file_stat ) == -1 )
{
perror("Can't get the information of the file.n");
cur_node = cur_node->next;
continue;
}
//根据文件类型和权限来判断是普通文件还是目录。
if ( S_ISBLK(file_stat.st_mode ) ) //是块设备
{
int i = 0;
int j = 0;
strcpy(file_info.name,cur_node->d_name);
printf("%s n", file_info.name);
while( i d_name);
printf("%s n", file_info.name);
while( i
用gdb 看了一下,发现是在cur_node = cur_node->next处出错的.
可能是越界了,但是我在构造文件链表时已经考虑到这个问题了~还是出错.
请指教~
-------------------------------------
代码
-------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*
IDE硬盘的子程序
*/
static void dump_identity(const struct hd_driveid *id)
{
const unsigned short int *id_regs= (const void*)id;
printf("Model=%.40s,FwRev=%.8s,SerialNo=%.20sn",id->model,id->fw_rev,id->serial_no);
};
void GetIdeNum(const char *t_devname)
{
struct hd_driveid id;
char *ide_devname = *t_devname;
int fd = -1;
fd = open(ide_devname, O_RDONLY|O_NONBLOCK);
if (fd d_name,".") == 0 ) || (strcmp(drt->d_name,"..") == 0) )
{
continue; //忽略.目录和..目录
}
next_node = (dirlink*)malloc(sizeof(dirlink));
//链表操作
if(NULL == head_node)
{
head_node = next_node; //首节点
}
else
{
cur_node->next = next_node;//当前节点
}
cur_node = next_node;
// copy the filename which get from drt into the cur_node
strcpy(cur_node->d_name,drt->d_name);
}
cur_node->next = NULL; //最后一个节点
closedir(dir);
return head_node; //返回链表头指针
}
void print_file_info(dirlink* head_node)
{
struct stat file_stat;
dirlink* cur_node = head_node;
info file_info;
char devname[30] = {"/dev/"};
char *ide_table[9] = {"hda", "hdb", "hdc", "hdd", "hde", "hdf", "hdg", "hdh", "hdi"};
char *scsi_table[9] = {"sda", "sdb", "sdc", "sdd", "sde", "sdf", "sdg", "sdh", "sdi"};
while( NULL != cur_node ) //遍历链表
{
//may be problem in here!
//check the stat()
if ( stat(cur_node->d_name,&file_stat ) == -1 )
{
perror("Can't get the information of the file.n");
cur_node = cur_node->next;
continue;
}
//根据文件类型和权限来判断是普通文件还是目录。
if ( S_ISBLK(file_stat.st_mode ) ) //是块设备
{
int i = 0;
int j = 0;
strcpy(file_info.name,cur_node->d_name);
printf("%s n", file_info.name);
while( i d_name);
printf("%s n", file_info.name);
while( i