I am calling execv
in my C code to launch an executable, but I want to set its working directory to something custom.
For example, in one case, I am launching ls
, but it lists the files in my original program's directory. But I want to set the working directory to something custom. How will I achieve it, such that, I'll set it to /usr/bin
and ls
will list the files in that dir. And don't give me a specific solution for ls
, it was just an example.
Use chdir(2)
after a successful fork(2)
, before execing:
switch (fork()) {
case 0:
chdir(newpath);
execvp(...);
break;
}
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