As a historical curiosity, in the very first versions of Unix chdir was a normal command rather than a shell builtin. You see, at that point Unix had no fork system call. There were multiple processes, but they were created statically at startup rather than on-demand. Running a command in the shell would cause the command to replace the shell in the address space of the process, and the process quitting would put the…
This is a really common way for software to become better. The pattern of development: - Make A - Make B better - A now broken because it had dependencies on B - (maybe) Realize a fundamental pattern that unifies A and B. - (maybe) Unify principles so that A and B become lemmas. This is a very scientific way of working. The truth emerges as the byproduct of keeping all the plates spinning in your model. As long as yo…
How does `cd` work?
71–80 of 93 posts
Re: How does `cd` work?
#72Earlier quoted context omitted.
Everybody who comes to know about systems calls will have learned the concept for the first time. Some people come to that knowledge by becoming a web developer, gaining some rudimentary understanding of the command line, typing `which cd`, and eventually reading the bash source. The fact that this is even possible is a testament to the author's curiosity and the value of free software. Others come to that knowledge…
I think gp is not ragging on the blog's author for being a novice at Unix systems internals but lamenting the " I looked into this but then it all seemed so complicated so I stopped" attitude that is present in this as well as the sibling posts on sudo and ls. I would much prefer to read three posts delving deeper into the inner workings of any one of these commands than the existing three muddled surface level treat…
I will say I did learn about some shell along the way and the bit about processes was interesting and starting to get at the core of 'cd'. It just seems like there are many other pieces of the puzzle, and I encourage the author to keep researching and keep writing about them.
Re: How does `cd` work?
#73Random piece of info about cd: cd.. (no space between "cd" and "..") works on Windows
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 .."
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?
#74Earlier quoted context omitted.
There's no reason why one shouldn't be able to invoke the chdir system call and implement the same behaviour.
Allowing any process to arbitrarily modify the state of another process would be a disaster for stability and security.
Re: How does `cd` work?
#75Re: How does `cd` work?
#76I used the first edition, but I think all you need is this: https://www.amazon.com/Advanced-Programming-UNIX-Environment...
https://www.amazon.com/Design-Implementation-MTX-Operating-S...
Re: How does `cd` work?
#77Earlier quoted context omitted.
> If /usr/bin is on a case-insensitive filesystem, and you execvp("CD", ...) That seems like fairly rare edge case, considering that UNIX typically had case-sensitive file-systems, or rather handled filenames as opaque blobs. I wonder what actual system caused the need for case folding? Maybe HFS?
The `tr` bit isn't actually in the FreeBSD version of the script[1], so I'm assuming it's a macOS HFS thing. https://github.com/freebsd/freebsd/blob/master/usr.bin/alias...
Re: How does `cd` work?
#78Not 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.
Re: How does `cd` work?
#79> The cd builtin is invoked as part of the Bash shell. > The Bash shell invokes the chdir function. Nope! Not quite. Bash: ~$ ls -ld foo lrwxrwxrwx 1 kaz kaz 4 Mar 1 6:50 foo -> /etc ~$ cd foo ~/foo$ pwd /home/kaz/foo ~/foo$ cd .. ~$ pwd /home/kaz Raw chdir and getcwd syscalls: $ pwd /home/kaz $ txr This is the TXR Lisp interactive listener of TXR 190. Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.…
So the article does point out that CD isn't a direct alias for chdir
Re: How does `cd` work?
#80Earlier quoted context omitted.
Allowing any process to arbitrarily modify the state of another process would be a disaster for stability and security.
Isn’t that what a debugger does?