我正在用 C 创建一个Quinine,我需要在其中创建一个新的 c 文件,然后编译并执行它。
我做了一个简单的片段来理解为什么它不起作用。
我的猜测是execv
在 fprintf 完成写入之前启动命令,但我进入睡眠状态并且它也没有工作。
(我为这个最丑陋的代码道歉,但这不是目标)
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char *cmd[100]= {"sh", "-c", "\"gcc tmp.c && ./a.out\""};
fprintf(fopen("tmp.c", "w"), "#include <stdio.h>\nint main(){puts(\"Hello World\");}");
execv("/bin/sh", cmd);
return (0);
}
输出
sh: gcc tmp.c && ./a.out: No such file or directory
任何的想法 ?