# Porting code



## osmano807 (Dec 19, 2009)

How to port this code to FreeBSD?

```
#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;
}
```


----------



## SirDice (Dec 19, 2009)

Looking at what smartctl (sysutils/smartmontools) does it looks like you can get that info with a IOCATAREQUEST ioctl. It's defined in sys/ata.h.


----------



## mickey (Dec 19, 2009)

This code looks b0rken by design, as it

Does not close file descriptor 'ide'
Always returns with error status

But here you go:

```
#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;
}
```


----------



## osmano807 (Dec 19, 2009)

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


----------



## mickey (Dec 19, 2009)

You might also find it interesting, to take a look at /usr/src/sbin/atacontrol.


----------

