#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libdevinfo.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <limits.h>

/*
 * Tinny utility to traverse the device tree and dump
 * all the minor cdrom nodes.
 */

static int
dump_minor(di_node_t node, di_minor_t  minor,  void *arg)
{
    char *nt, *mnp;
    char mpath[PATH_MAX];
    
    nt = di_minor_nodetype(minor);
    if (nt == NULL)
        return (DI_WALK_CONTINUE);
    
    if (strcmp(nt, DDI_NT_UGEN) == 0)
    {
        mnp = di_devfs_minor_path(minor);
        if (mnp != NULL)
        {
            strcpy(mpath, "/devices");
            strlcat(mpath, mnp, PATH_MAX);
            
            if (strstr(mnp, ",raw"))
            {
                di_devfs_path_free(mnp);
                return (DI_WALK_CONTINUE);
            }
            strlcat(mpath, ",raw", PATH_MAX);
            printf("/devices%s %s\n", mnp, mpath);
            di_devfs_path_free(mnp);
        }
    }
    
    return (DI_WALK_CONTINUE);
}

int main(void)
{
    /*char a;
    for (a='a';a<'z';a++)
      printf(" /devices/pci@0,0/pci1019,1b92@1d,7/storage@7/disk@0,0:%c /devices/pci@0,0/pci1019,1b92@1d,7/storage@7/disk@0,0:%c,raw\n",a,a);
    return 0;*/
    
    di_node_t root_node;
    printf("%s\n",DDI_NT_UGEN );
    
    if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL)
    {
        return (1);
    }
    di_walk_minor(root_node, NULL, 0, NULL, dump_minor);
    di_fini(root_node);
    sync();
    
    return (0);
}
