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 (10 years, 1 month ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by ahoka: rename drvctlio_examples.mdwn to examples/drvctlio.mdwn

    1: This and other code examples really want the [[wiki/todo/highlight_plugin]]. --[[schmonz]]
    2: 
    3: -----
    4: 
    5: #include <sys/ioctl.h>
    6: #include <sys/drvctlio.h>
    7: 
    8: #include <err.h>
    9: #include <string.h>
   10: #include <stdlib.h>
   11: #include <fcntl.h>
   12: 
   13: int
   14: main(int argc, char *argv[])
   15: {
   16:         struct devlistargs l;
   17:         unsigned int i, children;
   18:         int drvfd;
   19: 
   20:         if (argc != 2)
   21:                 err(1, "args");
   22: 
   23:         if ((drvfd = open(DRVCTLDEV, O_RDONLY, 0)) < 0)
   24:                 err(1, "can't open " DRVCTLDEV);
   25: 
   26:         strlcpy(l.l_devname, argv[1], sizeof(l.l_devname));
   27:         l.l_childname = NULL;
   28:         l.l_children = 0;
   29: 
   30:         if (ioctl(drvfd, DRVLISTDEV, &l) == -1)
   31:                 err(1, "DRVLISTDEV");
   32: 
   33:         children = l.l_children;
   34:         l.l_childname = malloc(sizeof(l.l_childname[0]) * children);
   35: 
   36:         if (ioctl(drvfd, DRVLISTDEV, &l) == -1)
   37:                 err(1, "DRVLISTDEV");
   38: 
   39:         if (l.l_children != children)
   40:                 err(1, "number of children changed between ioctls");
   41: 
   42:         if (l.l_childname == NULL) {
   43:                 puts("No children.");
   44:                 return 0;
   45:         }
   46: 
   47:         for (i = 0; i < l.l_children; i++) {
   48:                 puts(l.l_childname[i]);
   49:         }
   50: 
   51:         close(drvfd);
   52: 
   53:         return 0;
   54: }

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