Interesting how the author was looking at "cd" obviously sourced from FreeBSD project (perhaps found on a Mac computer running XNU kernel) and then, to investigate further, she consulted the source code for the Linux kernel to learn about "the" chdir syscall. Would the title "How does 'cd' work in MacOS ?" be better for readers wishing to learn, or worse? I wonder. The blog posts from her friend at jvns.ca have been…
How does `cd` work?
81–90 of 93 posts
Re: How does `cd` work?
#82Interesting how the author was looking at "cd" obviously sourced from FreeBSD project (perhaps found on a Mac computer running XNU kernel) and then, to investigate further, she consulted the source code for the Linux kernel to learn about "the" chdir syscall. Would the title "How does 'cd' work in MacOS ?" be better for readers wishing to learn, or worse? I wonder. The blog posts from her friend at jvns.ca have been…
I find it sad that people that are so curious about how things work end up buying Macs, and therefore unwillingly contributing to less openness and "studyability".
Re: How does `cd` work?
#83Random piece of info about cd: cd.. (no space between "cd" and "..") works on Windows
Re: How does `cd` work?
#84Not one character in the article is actually devoted to how `cd` works...it's just the story of how the author found out where to find the code for it. An article much more worthy of the title "How does `cd` work?" would maybe, you know, actually go through the code that makes `cd` work.
I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.
I am a little confused. What is the parent process and why does that matter here?
Re: How does `cd` work?
#85Earlier quoted context omitted.
I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.
>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process) I am a little confused. What is the parent process and why does that matter here?
What you want cd to do is change the caller's (or parent's) working directory, or in this case the shell's working directory.
There are hacks to do it that you can find elsewhere in this thread (process injection and debug APIs to name two) but frankly the shell changing its own working directory via a builtin is a much simpler and more reliable solution that is also cross-platform.
Re: How does `cd` work?
#86Earlier quoted context omitted.
>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process) I am a little confused. What is the parent process and why does that matter here?
The parent is the shell that called the theoretical /user/bin/cd. It means that a cd program would get launched by the shell, change its own working directory and then terminate, which is completely useless. What you want cd to do is change the caller's (or parent's) working directory, or in this case the shell's working directory. There are hacks to do it that you can find elsewhere in this thread (process injection…
Re: How does `cd` work?
#87That's not bad, but I'd also suggest checking man pages for syscalls. `man 2 chdir` will give the some documentation on both Mac and Linux OSes and call out the specs that are relevant to the call. (Why `man 2`? That searches section 2 of the man pages which is dedicated to syscalls. How on earth would someone know that? `man man` of course. :-) )
On linux, an interesting thing is that you can inspect a process's current working directory (IE the last thing they chdir()ed to) by looking in /proc//cwd/. It presents itself as a symlink to the actual current directory as stored in the kernel.
Re: How does `cd` work?
#88Earlier quoted context omitted.
I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.
>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process) I am a little confused. What is the parent process and why does that matter here?
If we have a shell and we use it to start up a separate chdir program, we get two processes: shell (parent) and chdir (child). The kernel isolates them from each other, so they both have completely distinct working directories. The chdir executes the relevant system call, changes its own working directory and exits. The shell is not affected by the system calls performed by its children, so the chdir process effectively does nothing.
By implementing chdir as a built-in function, the shell is able to change the state of its own process.
Re: How does `cd` work?
#89Earlier quoted context omitted.
It's been like that since the DOS days, IIRC. You can simulate that behavior on bash with an alias such as this one: alias cd..="cd .."
I believe that you also don't need the space when you specify a path: cd\foo\x The cmd parser in Windows is the most bizarre piece of software in common use. I don't believe there is a single person that actually understands how it works completely.
Re: How does `cd` work?
#90Okay, but now I want to know how it works on something other than (foo)Nix, because that's not an OS space I care about. What about how it works in CP/M or foo-DOS? I've never used a *Nix computer, but I know that I used 'cd' going back to the time of using a TRS-80 CoCo. I know it's a low-level system call in most on-disk operating systems to change directories. But, for instance, how does it translate physical addr…
* https://superuser.com/a/380231/38062
* https://unix.stackexchange.com/a/251215/5132
... and so on.