Porting code

How to port this code to FreeBSD?
Code:
#include <stdio.h>
#include <fcntl.h>
#include <linux/hdreg.h>

int main()
{
struct hd_driveid hd;
int ide;
ide=open("/dev/hda",O_RDONLY);
ioctl(ide,HDIO_GET_IDENTITY,&hd);
printf("Serial number - %s\n",hd.serial_no);
return 1;
}
 
This code looks b0rken by design, as it
  1. Does not close file descriptor 'ide'
  2. Always returns with error status

But here you go:
PHP:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ata.h>

int main(int argc, char *argv[])
{
        int fd;
        int rv = -1;
        const char *dvc = "/dev/ad0";

        if((fd = open(dvc, O_RDONLY)) != -1)
        {
                struct ata_params atap;

                if(ioctl(fd, IOCATAGPARM, &atap) != -1)
                {
                        printf("Drive serial number: %s\n", atap.serial);
                        rv = 0;
                }
                else
                {
                        fprintf(stderr, "ioctl(IOCATAGPARM) failed (%s)\n",
                                strerror(errno));
                }

                close(fd);
        }
        else
        {
                fprintf(stderr, "failed to open %s (%s)\n",
                        dvc, strerror(errno));
        }

        return rv;
}
 
Thank you very much!
I had looked at the smartctl, but as I'm a little new to FreeBSD, I had not found it.
 
Back
Top