1

我想从硬盘驱动器中获取基本信息并将其打印出来。最重要的是物理扇区大小正确。

在过去的几个小时里,我一直在ioctl努力获得我想要的东西,但我无法弄清楚。

我以前从未使用ioctl过,我似乎无法找到一个简单的解释来说明你到底要做什么。

无论如何,我的代码看起来像这样

int main () {
    FILE *driveptr;
    int sectorsize;
    struct hd_driveid hd;

    driveptr=fopen("/dev/sda","r");

    if (ioctl(driveptr,HDIO_GET_IDENTITY, &hd)!=0) { 
        printf("Hard disk model: %s\n",hd.model); 
        printf("Serial number: %s\n",hd.serial_no); 
        printf("Sector size: %i\n",hd.sector_bytes);
        sectorsize=hd.sector_bytes;
    } else { 
        printf("Error fetching device data.\n");
    }
}

在编译器中它会抛出这些警告,它会编译但打印时字符串是空的。

gcc -o test source.c
source.c: In function ‘main’:
source.c:67:9: warning: passing argument 1 of ‘ioctl’ makes integer from pointer without a cast [enabled by default]
/usr/include/x86_64-linux-gnu/sys/ioctl.h:42:12: note: expected ‘int’ but argument is of type ‘struct FILE *’

我希望有人可以向我解释出了什么问题!

4

1 回答 1

1

Instead of

if (ioctl(driveptr,HDIO_GET_IDENTITY, &hd)!=0) { 

you probably want

if (ioctl(fileno(driveptr),HDIO_GET_IDENTITY, &hd)!= -1) { 
          ^^^^^^^        ^                           ^^

Because ioctl's first argument need to be an integer file descriptor not a FILE *

fileno() will give you an integer fd from a FILE *.

Note also that ioctl returns -1 on an error and sets errno.

Reading the man pages of the functions you are using is probably quicker than posting to StackOverflow.

See man pages of ioctl, fileno.

于 2013-11-29T17:51:23.513 回答