I want to set the current working directory for a child program. I am setting it with setenv("PWD", "/media", 1); in the parent and starting the child with system(...);. But when the child calls getcwd(0, 0) it is returning /root.
I thought child processes were supposed to inherit the environment from their parent?
POSIX compliant shells set the PWD environment variable to the current working directory. However, the actual current working directory is a property of the process itself (inherited by children spawned using system or fork) and needs to be changed with chdir.
Changes to the PWD environment variable variable don't change anything, and the variable does not necessarily actually reflect the current working directory (if it was changed by something other than the shell).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With