top | item 46389563

(no title)

supermdguy | 2 months ago

Reading the code, I was surprised to see that cd was implemented by calling out to the os library. I assumed that was something the shell or at least userspace handled. At what level does the concept of a “current directory” exist?

discuss

order

creatonez|2 months ago

It's at the kernel level. Each process has its own current working directory. On Linux, these CWD values are exposed at `/proc/[...]/cwd`. This value affects the resolution of relative paths in filesystem operations at a syscall level.

hnlmorg|2 months ago

It’s also generally a shell builtin. Though you do find an executable called cd too for compatibility reasons.

semiquaver|2 months ago

Unix defines a Working Directory that every process has, changed with chdir(2): https://man7.org/linux/man-pages/man2/chdir.2.html

mort96|2 months ago

This doesn't technically answer the question: POSIX doesn't concern itself with the kernel interface, only with the libc. Most POSIX systems have a kernel with a syscall interface that mirrors the libc API so that these libc functions are just syscall wrappers, but nothing technically prevents the current working directory to be a purely userspace concept maintained by the libc where all relative paths passed to filesystem functions are translated into absolute paths by the libc function before being passed to the kernel via syscall.

But yes, in the BSDs, Linux and Windows, the kernel has a concept of a current working directory.