I did something similar with TCL, the basis was using an extension I wrote to handle the UNIX stuff [0]. It operated an On-Premises cloud environment appliance, and `init` was just a TCL script (at one point it was a statically linked binary with the init script embedded, but that turned out to be overkill)
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?
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.
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.
Very cool. Good use of quickjs, although it would have been cool if it somehow didn’t need a libc and just used the syscall interface. Makes me want to give that a try.
I remember some core Unix utilities reimplemented in Perl, mainly done for Win32 systems back in the day. OFC the performance coudn't compete with the ones written in C, but it was good enough.
[0] https://chiselapp.com/user/rkeene/repository/tuapi/doc/trunk...
https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=1...
and
https://bellard.org/jslinux/
By the famous Fabrice Bellard who is the creator of QuickJS, QEMU, FFMPEG and many other brilliant and fascinating tools!
https://bellard.org/
But yes, in the BSDs, Linux and Windows, the kernel has a concept of a current working directory.
I am working something similar in Go, and writing an educative blog post series about it: https://serversfor.dev/linux-inside-out/
Making a micro Linux distro [for RISC-V]
https://popovicu.com/posts/making-a-micro-linux-distro/