I am building a directory tree.
I am trying to replicate the shell 'cd' command to enter a directory.
How do I return tree_node cwd after making cwd it's subdirectory?
(cwd = current working directory, subDir = subdirectory) for example:
cwd = subDir->tree;
return cwd; (how do I return cwd without an error?)
// *checks whether cwd has a subdirectory named arg
// *if yes, the function returns the corresponding tree node (and become new working directory)
// *if no, prints an error message
// *handle cd and cd ..
struct tree_node *do_cd(struct tree_node *cwd, struct tree_node *root, char *arg) {
// checks if directory exists
struct list_node *subDir = cwd -> first_child;
while (subDir != NULL) {
if (strcmp(subDir->tree->string_buffer, arg) == 0) {
printf("Entering directory with name %s \n", arg);
subDir->tree = cwd;
return cwd;
printf("Directory with name %sentered.\n", arg);
}
subDir = subDir->next;
}
printf("Directory does not exist!\n");
}