Sample:
/tmp$ exec /tmp/cd /bin
/bin$ exec /tmp/cd /home
/home$ exec /tmp/cd /usr
/usr$
Sample code: #include <err.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *shell;
if (argc != 2)
errx(1, "Usage: exec cd /some/path");
if (chdir(argv[1]) == -1)
err(1, "chdir");
shell = getenv("SHELL");
if (!shell || !*shell)
shell = "/bin/sh";
execl(shell, shell, NULL);
err(1, "exec");
}This seems a little awkward and I'm not sure what is being proven. :P
When they introduced something like the fork/exec model we now have they discovered cd didn't work any more and had to write it as a built in.
Now, for bonus points, work out how goto worked when it wasn't a built in.
Makes sense, since those systems might not have enough memory to run the shell and another program at the same time.
python -c 'import os; os.chdir("/")'
Notice that the working directory of your shell is unaffected. :-)
Or:
bash -c 'export foo=bar'; echo $foo
That being said, aside from a kernel patch there is definitely no official way of doing it. This is the hacky way:
http://stackoverflow.com/questions/2375003/how-do-i-set-the-...
The hoops needed to run it on semi-modern Linux was a real PITA (think chroot jail and some ld shenanigans to patch shared libs). Microsoft's backward compatibility is reallly hard to do.
After learning a valuable lesson in exactly how dynamic library work and the recommended process for live libc upgrade (don't do it if ABI changes) I fixed it by using my IRC client which was already running so unaffected to get a statically linked copy of /bin and /sbin from another machine, via DCC Send...
Recovery then consisted of restoring libc5 from slackware 3.2 install media.
I can't remember how I got root, either su was statically linked (believable since it's setuid) or I had a logged in root session. I did have to used the tcsh "echo *" trick for file listing and the shell built-in cd...