0

MyCode:

    int ushExCd(Cmd cmd){ // BuiltIn shell command implementation
        char * home = "HOME";
        int returnVal = 1;
        if(cmd->nargs > 1){ // target directory is specified in args[1]
            returnVal = chdir(cmd->args[1]);
        }else{ // target directory is not specified, use "HOME" instead
            returnVal = chdir(getenv(home));
        }
        if(-1 == returnVal){
            perror("cd");
        }
        return 1;
    }

Using a llvm debugger to check returnVal right after its assignment shows that its value is 0 (though its initialized to 1), but doing p (char*)getenv("PWD") in the debugger immediately after the chdir() system call shows that the directory has not changed.

I was using Xcode, but then I tried using bash shell, and a tcsh using gcc compilers with no use.

Whats even more interesting is, the same piece of code runs successfully on a shell that another person implemented, which might indicate an incorrect shell implementation, but then I am debugging right inside this code snippet and there should be no way the chdir() is getting lost at a later point of time.

Mathieu
  • 8,840
  • 7
  • 32
  • 45

1 Answers1

1

I think that the getenv("PWD") will give you the current directory when your program was launched.

To know the current directory, use getcwd().

Mathieu
  • 8,840
  • 7
  • 32
  • 45