list - C: traversing tree_nodes in a directory tree -


i building traversable directory tree.

this diagram drew understand structure easily:

enter image description here

how print out list of subdirectories (subdir) of current working directory (cwd)?

so example, use shell commands in order:

mk_directoryname, cd_directoryname, ls 

below code attempt method 'cd', seems stay in root directory , print directories , not subdirectories of cwd:

// *checks whether cwd has subdirectory named arg // *if yes, function returns corresponding tree node (and become new working directory) // *if no, prints error message // *handle cd , cd .. struct tree_node *do_cd(struct tree_node *cwd, struct tree_node *root, char *arg) {      // checks if cwd has subdirectory named arg     struct list_node *subdir = cwd -> first_child;      struct tree_node *pardir = cwd -> parent;     printf("making current working directory parent directory.\n");      while (subdir != null) {         if (strcmp(subdir -> tree -> string_buffer, arg) == 0) {             printf("subdirectory exists: entering!\n");             cwd = subdir-> tree;             printf("making subdirectory current working directory: name = %s\n", arg);             printf("returning current working directory: %s.\n", arg);             return cwd;         }         else if (strcmp(arg, "..") == 0) {             cwd = pardir;             printf("returning parent directory.\n");             return cwd;         }         else if (strcmp(arg, "") == 0) {             printf("returning root directory.\n");             return root;         }         subdir = subdir-> next;     }     printf("directory not exist!\n");     return cwd; } 

ls print method code attempt:

// prints children of directory cwd (not recursively) void do_ls(struct tree_node *cwd) {     printf("listing directories...\n");     struct list_node *subdir = cwd -> first_child;     while (subdir != null) {         printf("%s\n", subdir ->tree -> string_buffer);         subdir = subdir->next;     } } 


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -