Being quite new to writing shell. I am trying to get "cd" to go to home directory when I don't give any extra argument.
But when I call "cd" and the code tries to chdir(getenv("HOME"))
, it shows an error message "No such file or directory"
.
/*
Builtin function implementations.
*/
int cd(char **args){
if (args[1] == NULL){
printf("%s\n", getenv("HOME"));
if (chdir(getenv("HOME")) != 0) {
perror("dsh");
}
} else if (chdir(args[1]) != 0){
perror("dsh");
}
return 1;
}
getenv("HOME")
does give correct directory, i.e. "/Users/oasisweng"
I guess I have done something incorrectly. Where should I fix? If possible, please tell me why.
I have read the chdir man here but if I manually enter cd /Users/oasisweng
, then it will work.
Thank you!!