File:  [NetBSD Developer Wiki] / wikisrc / examples / drvctlio.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Sat Apr 17 17:04:07 2010 UTC (4 years ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by ahoka: rename drvctlio_examples.mdwn to examples/drvctlio.mdwn

This and other code examples really want the [[wiki/todo/highlight_plugin]]. --[[schmonz]]

-----

#include <sys/ioctl.h>
#include <sys/drvctlio.h>

#include <err.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

int
main(int argc, char *argv[])
{
        struct devlistargs l;
        unsigned int i, children;
        int drvfd;

        if (argc != 2)
                err(1, "args");

        if ((drvfd = open(DRVCTLDEV, O_RDONLY, 0)) < 0)
                err(1, "can't open " DRVCTLDEV);

        strlcpy(l.l_devname, argv[1], sizeof(l.l_devname));
        l.l_childname = NULL;
        l.l_children = 0;

        if (ioctl(drvfd, DRVLISTDEV, &l) == -1)
                err(1, "DRVLISTDEV");

        children = l.l_children;
        l.l_childname = malloc(sizeof(l.l_childname[0]) * children);

        if (ioctl(drvfd, DRVLISTDEV, &l) == -1)
                err(1, "DRVLISTDEV");

        if (l.l_children != children)
                err(1, "number of children changed between ioctls");

        if (l.l_childname == NULL) {
                puts("No children.");
                return 0;
        }

        for (i = 0; i < l.l_children; i++) {
                puts(l.l_childname[i]);
        }

        close(drvfd);

        return 0;
}

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb