/* 
    Copyright (C) 2005 Patrick Leslie Polzer <polzer@gnu.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/

/* cc -o hdiogetid hdiogetid.c */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/ioctl.h>

#include <linux/blkpg.h>
#include <linux/hdreg.h>

#include <errno.h>
#include <string.h>

#include <stdio.h>


int main (int ac, char** av)
{
        struct hd_driveid       hdi;
        int                     fd;
        char*                   hdi_buf[40];
        
        if (ac != 2)
        {
                printf ("Usage: %s DEVICE\n", av[0]);
                return 1;
        } 
        
        if ((fd = open(av[1], O_RDONLY)) == -1)
        {
                printf ("couldn't open device \"%s\": [%s]\n", av[1],
                                strerror(errno));
                return 2;
        }
                
        if (ioctl (fd, HDIO_GET_IDENTITY, &hdi))
        {
                printf ("couldn't execute ioctl for device: [%s]\n",
                                strerror(errno));
                return 3;
        }
        
        close (fd);

        memcpy (hdi_buf, hdi.model, 40);
        hdi_buf[40] = '\0';
        
        printf ("%s identifies itself as \"%s\"\n", av[1], hdi_buf);

        return 0;
}

