How can I escape variables sent to the 'system' command in C++?

前端 未结 5 1599
迷失自我
迷失自我 2021-01-18 18:19

This question talks about using the system command and passing variables. Here is an example it gives:

string cmd(\"curl -b cookie.txt -d test=\         


        
5条回答
  •  一个人的身影
    2021-01-18 18:47

    See the man page listing all syscalls (system calls are not the system(3) function which forks a shell).

    Read more about system calls and linux kernel, and the overall organization of operating systems

    Back to your question, you could quote for the shell (just follow the shell escape rules to transform your strings; for example prefix any non-letter character with a backslash).

    See also the man pages of fork, execve, etc.

    Read a good book on advanced linux programming and the famous Advanced Unix Programming

    Use the freedom of most of the software on a Linux distribution. For example, you could study the source code of a simple shell like sash.

    And perhaps you just want to use curl. Then the best way is to code for the libcurl library (without any call to system(3))

    Have fun learning all this!

提交回复
热议问题