/* * SXML: sample program to graft XML tree * $Id: sxmlgraft.c,v 1.2 2007/08/31 09:57:49 kouichi Exp $ */ #include #include #include #include #include #include #include #include "sxml.h" static void dirwalk(sxml_node_t *, const char *); static void dirwalk(node, dirname) sxml_node_t * node; const char * dirname; { DIR * dirp; dirp = opendir(dirname); if (dirp != NULL) { sxml_node_t * ep; ep = sxml_new_element(node, "directory"); if (ep != NULL) { register struct dirent * dp; if (dirname[0] == '/') { chdir(dirname); } sxml_set_attribute(ep, "name", dirname); while ((dp = readdir(dirp)) != NULL) { struct stat sbuf; if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) { continue; } if (stat(dp->d_name, &sbuf) == 0) { if (S_ISDIR(sbuf.st_mode)) { char * path; asprintf(&path, "%s/%s", dirname, dp->d_name); if (path != NULL) { dirwalk(ep, path); free(path); chdir(dirname); } } else { sxml_node_t * np; np = sxml_set_node(ep, "file", dp->d_name); if (np != NULL) { if (S_ISREG(sbuf.st_mode)) { sxml_set_attribute(np, "type", "regular"); } else if (S_ISLNK(sbuf.st_mode)) { sxml_set_attribute(np, "type", "symbolick"); } else if (S_ISBLK(sbuf.st_mode)) { sxml_set_attribute(np, "type", "block"); } else if (S_ISCHR(sbuf.st_mode)) { sxml_set_attribute(np, "type", "character"); } else if (S_ISSOCK(sbuf.st_mode)) { sxml_set_attribute(np, "type", "socket"); } else if (S_ISFIFO(sbuf.st_mode)) { sxml_set_attribute(np, "type", "pipe"); } else { sxml_set_attribute(np, "type", "unknown"); } } } } } } closedir(dirp); } } int main(argc, argv) int argc; char * argv[]; { sxml_node_t * root; if (argc == 1) { fprintf(stderr, "usage: %s \n", argv[0]); exit(64); } root = sxml_new_vertex(); if (root != NULL) { sxml_node_t * np; np = sxml_new_prolog(root, "xml"); if (np != NULL) { sxml_set_attribute(np, "encoding", "us-ascii"); sxml_set_attribute(np, "version", "1.0"); } dirwalk(root, argv[1]); sxml_print_tree(root, stdout); sxml_delete_node(root); } return 0; }