I\'m currently reading up on and experimenting with the different possibilities of running programs from within C code on Linux. My use cases cover all possible scenarios, from
Invoking a shell allows you to do all the things that you can do in a shell. For example,
FILE *fp = popen("ls *", "r");
is possible with popen()
(expands all files in the current directory).
Compare it with:
execvp("/bin/ls", (char *[]){"/bin/ls", "*", NULL});
You can't exec ls
with *
as argument because exec(2)
will interpret *
literally.
Similarly, pipes (|
), redirection (>
, <
, ...), etc., are possible with popen
.
Otherwise, there's no reason to use popen
if you don't need shell - it's unnecessary. You'll end up with an extra shell process and all the things that can go wrong in a shell go can wrong in your program (e.g., the command you pass could be incorrectly interpreted by the shell and a common security issue). popen() is designed that way. fork
+ exec
solution is cleaner without the issues associated with a shell.