I am currently writing a shell in C (school project). My cd and pwd is working. But when I do: cd /bin and after pwd, pwd shows /usr/bin. For me it looks like execve with pwd can't handle /bin but why?
name% ./myshell
name: cd
Current working dir: /nfs/homes/name
name: cd /bin
Current working dir: /usr/bin
name: exit
Here I was working with getcwd(). But also when I pass pwd to Execve it is the same resulut. I fixed the problem.
But I want to know why this happens and are there other folders that are not working?
int ft_cd(char *path)
{
if (!path)
chdir(getenv("HOME"));
else if (chdir(path))
ft_printf("bash: cd: %s: No such file or directory\n", path);
if (!ft_strcmp(path, "/bin") || !ft_strcmp(path, "/bin/"))
return (1);
return (0);
}
Now I am looking for cd /bin or cd /bin/. In this case, I print /bin when pwd is called. ft_strcmp is almost thesame as strcmp.
Run ls -l /bin
and you will see it is a symlink to /usr/bin
.
Since your pwd
code (presumably using getcwd()
) doesn't know how you arrived in /usr/bin
, it reports that name (the physical name for the directory) rather than /bin
(the logical name).
Look at the POSIX specification for the cd
command, and you'll see the -L
(logical) and -P
(physical) options. Your command is effectively implementing the -P
option (only). Also look at the POSIX specification for the pwd
command; similar comments apply.