2010-06-27 5 views

Répondre

2

"#include <hdreg.h>" et d'utiliser ioctl HDIO_GET_IDENTITY pour obtenir un struct hd_driveid.
Dans cette structure, le champ x->sector_bytes correspond à la taille du secteur.

#include <stdlib.h> 
#include <stdio.h> 
#include <sys/ioctl.h> 
#include <linux/hdreg.h> 
#include <fcntl.h> 
#include <errno.h> 
#include <string.h> 
#include <cctype> 
#include <unistd.h> 

int main(){ 
    struct hd_driveid id; 
    char *dev = "/dev/hdb"; 
    int fd; 

    fd = open(dev, O_RDONLY|O_NONBLOCK); 
    if(fd < 0) { 
     perror("cannot open"); 
    } 
    if (ioctl(fd, HDIO_GET_IDENTITY, &id) < 0) { 
     close(fd); 
     perror("ioctl error"); 
    } else { 
     close(fd); 
     printf("Sector size: %du\n", id.sector_bytes); 
    } 
} 
+0

Merci beaucoup. Une question, pourquoi l'ouverture a-t-elle besoin du drapeau O_NONBLOCK? – IUnknownPointer

+0

Voir ici: http://opengroup.org/onlinepubs/007908799/xsh/open.html – clyfe

+0

Hey, @clyfe, ça ne marche pas ... ioctl retourne toujours une erreur. – IUnknownPointer

Questions connexes