666cartman
Programmer
I finally found the solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
void main ()
{
int fd;
int sector_size = 1;
long sectors;
long size;
struct hd_big_geometry g;
fd=open("/dev/hda",O_RDONLY);
if (ioctl(fd, HDIO_GETGEO_BIG_RAW, &g))return -1;
if (ioctl(fd, BLKGETSIZE, §ors))return -1;
if (ioctl(fd, BLKSSZGET, §or_size))return -1;
printf("sectorsize: %d sectors:
%ld\n",sector_size,sectors);
size=sectors/(1024/sector_size);
printf("size: %ld\n",size);
}
the Code is not perfect but it works, notice that it gets the real size of an hd from the driver. Of course it only works if you are root.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
void main ()
{
int fd;
int sector_size = 1;
long sectors;
long size;
struct hd_big_geometry g;
fd=open("/dev/hda",O_RDONLY);
if (ioctl(fd, HDIO_GETGEO_BIG_RAW, &g))return -1;
if (ioctl(fd, BLKGETSIZE, §ors))return -1;
if (ioctl(fd, BLKSSZGET, §or_size))return -1;
printf("sectorsize: %d sectors:
%ld\n",sector_size,sectors);
size=sectors/(1024/sector_size);
printf("size: %ld\n",size);
}
the Code is not perfect but it works, notice that it gets the real size of an hd from the driver. Of course it only works if you are root.