Configuring a custom working directory for a process launched with exec

I call execv in my C code to run the executable, but I want to set its working directory to something normal.

For example, in one case, I run ls , but it lists the files in my program source directory. But I want to set the working directory to something ordinary. How will I reach it, so I will put it on /usr/bin , and ls list the files in this directory. And do not give me a specific solution for ls , it was just an example.

+4
source share
1 answer

Use chdir(2) after successful fork(2) before executing:

 switch (fork()) { case 0: chdir(newpath); execvp(...); break; } 
+14
source

Source: https://habr.com/ru/post/1399554/


All Articles