Why does popen() invoke a shell to execute a process?

前端 未结 3 1864
温柔的废话
温柔的废话 2021-02-20 12:13

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

3条回答
  •  囚心锁ツ
    2021-02-20 12:43

    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.

提交回复
热议问题