<分区>
我的代码:
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;
}
在分配后立即使用 llvm 调试器检查 returnVal
显示其值为 0(尽管其初始化为 1),但是执行 p (char*)getenv("PWD")
在 chdir()
系统调用之后立即在调试器中显示目录没有改变。
我使用的是 Xcode,但后来我尝试使用 bash shell,以及使用 gcc 编译器的 tcsh,但没有用。
更有趣的是,同一段代码在另一个人实现的 shell 上成功运行,这可能表明 shell 实现不正确,但是我正在这段代码片段中进行调试,应该没有办法 chdir()
稍后会丢失。