0

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");
}
  • you said if(...) then return cwd, what about other conditions? the compiler gives error because not all paths return – Pooya Feb 05 '16 at 18:51
  • What do you mean with "how do I return cwd without an error"? Do you mean build errors? What build errors? Run-time errors? What and where? And why not return `NULL` if you fail to change directory? You ***must*** returns something in all paths of the function, otherwise you will heave *undefined behavior* when using the returned pointer. Also, you do know that `return` makes the function return *immediately*, no statements after it will be executed. – Some programmer dude Feb 05 '16 at 18:52
  • Where should I return NULL? After printf("Directory does not exist!\n");? – MsSingularity Feb 05 '16 at 18:53
  • after `printf("Directory does not exist!\n");` – John Hascall Feb 05 '16 at 18:54
  • Ahhh I get it, thanks guys! You're so nice! – MsSingularity Feb 05 '16 at 18:54
  • Sorry, this is my 2nd day programming in C – MsSingularity Feb 05 '16 at 18:55
  • also this line `printf("Directory with name %sentered.\n", arg);` will never get executed due to the return before it – John Hascall Feb 05 '16 at 18:55
  • 2
    Your second day? Then I say you're way out of your depth! Start with something simpler, just a ["hello world" program](https://en.wikipedia.org/wiki/%22Hello,_World!%22_program) and work your way up from there, small baby steps at a time. I recommend you check out [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and find a good beginner book or tutorial. – Some programmer dude Feb 05 '16 at 18:57
  • This is a common programming assignment, so she may not have that luxury... – John Hascall Feb 05 '16 at 18:58
  • I'm guessing that instead of `subDir->tree = cwd;` you want `cwd = subDir->tree;` maybe... – John Hascall Feb 05 '16 at 19:02
  • @JohnHascall Common assignment yes. Common **second day** assignment? Not very likely. – Some programmer dude Feb 05 '16 at 19:02
  • Could easily be a first assignment in a "implement linux" course, where they assume a pre-req of programming course. – John Hascall Feb 05 '16 at 19:04
  • Thank you guys! This was all very helpful! – MsSingularity Feb 05 '16 at 23:04

1 Answers1

0

You need to return NULL or any pointer to a valid tree. You can check this code:

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;
        printf("Directory with name %sentered.\n", arg);
        return cwd;
    }
    subDir = subDir->next;
}
printf("Directory does not exist!\n");
return NULL;
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72
Pooya
  • 6,083
  • 3
  • 23
  • 43