-1
      char *home = getenv("HOME");
      if (!strcmp(t->argv[0], "cd")) {
         if (!t->argv[1]) {
            chdir(home);
         }
         if (chdir(t->argv[1])) {
            perror(t->argv[1]);
         }
      }

what this is supposed to do is run the cd command with the provided argument, and if there is no argument provided then return to the home directory. if i call cd without an argument, chdir prints "Bad address". i dont know why, considering i printed the value that getenv("HOME") returns and entered that path manually in my shell and it worked fine. why does it work when i provide the value but not when getenv provides it?

I also cant manually code a path for home either because it has to run on other machines. i need it to work with getenv("HOME").

1 Answers1

2

i was being an idiot, as @dimich pointed out. the first chdir worked fine, but the second if statement should be an else if.

new code:

      char *home = getenv("HOME");
      if (!strcmp(t->argv[0], "cd")) {
         if (!t->argv[1]) {
            chdir(home);
         } else if (chdir(t->argv[1])) {
            perror(t->argv[1]);
         }
      }