Listing 3 Listing CD-ROM drives
1 /* Compile with gcc -g -o listcds listcds.c -ldevinfo */
2 #include <stdio.h>
3 #include <ftw.h>
4 #include <libdevinfo.h>
5
6 di_minor_t srchminor;
7
8 int showpath(const char *fname, const struct stat *statbuf, int type) {
9 if (statbuf->st_rdev == di_minor_devt(srchminor)) {
10 printf("%s\n", fname);
11 return(1);
12 }
13 return(0);
14 }
15
16 int checknode(di_node_t node, void *dummy) {
17 di_minor_t minor = DI_MINOR_NIL;
18 char *path;
19
20 /* Check if any of the minor nodes are CD-ROMs */
21 while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
22 if (di_minor_nodetype(minor) &&
23 ((!strcmp(di_minor_nodetype(minor), DDI_NT_CD) ||
24 (!strcmp(di_minor_nodetype(minor), DDI_NT_CD_CHAN))))) {
25 srchminor = minor;
26 if (ftw("/dev", &showpath, 10) == 0) {
27 printf("/devices%s:%s\n",
28 path = di_devfs_path(node), di_minor_name(minor));
29 di_devfs_path_free(path);
30 }
31 break;
32 }
33 }
34
35 return(DI_WALK_CONTINUE);
36 }
37
38 int main() {
39 di_node_t tRootNode;
40
41 if ((tRootNode = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
42 perror("Failed to get root node");
43 return(1);
44 }
45
46 /* Examine all nodes */
47 di_walk_node(tRootNode, DI_WALK_CLDFIRST, NULL, &checknode);
48
49 di_fini(tRootNode);
50
51 return(0);
52 }
|