File:  [NetBSD Developer Wiki] / wikisrc / examples / drvctlio.mdwn
Revision 1.2: download - view: text, annotated - select for diffs
Sun Apr 25 21:21:05 2010 UTC (3 years, 11 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by ahoka: indent by four spaces

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