On Saturday 04 February 2012 19:23, in comp.os.linux.misc,
NoE...@home.org
wrote:
Not entirely true. Nothing is restored, because nothing (in /that/ process)
was changed.
> Is there any way to actually change the parent
> directory for subsequent system calls?
Yes, of course. By using the chdir(2) syscall.
> So that, a first ls command will list the
> contents of the original directory, but
> after cd /tmp subsequent ls calls will list
> the contents of /tmp?
Certainly not the way you are doing it.
Processes inherit things from their parent processes, including current
working directory. A child process cannot change a parent process' cwd; it
can only change it's own cwd, which can be inherited by /it's/ children.
You've used a complicated way to try to change the cwd. Your system() call
forks off a child process which executes the shell (/bin/sh) to run a
command. That's a parent (which won't be affected), a child running /bin/sh
(which /may/ be affected, if the system() command was a shell builtin
like "cd"), and (possibly) a grandchild process (if the system()
command /wasn't/ a shell builtin).
It'll be the child or grandchild (depending) who changes directory, then
exits.
To change directory /in the parent/ process, you actually have to call the
chdir(2) system call.
HTH
--
Lew Pitcher