【发布时间】:2025-02-11 15:55:02
【问题描述】:
我正在 C 中创建一个Quine,我需要在其中创建一个新的 c 文件,然后编译并执行它。
我做了一个简单的 sn-p 来了解它为什么不起作用。
我的猜测是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
有什么想法吗?
【问题讨论】:
-
quine程序的对象不是在运行时重新创建自己的源代码作为输出吗?请参阅Program that prints its own source code as its output 和Program that prints itself even if the source file is deleted 作为示例; RHS 上的“相关问题”列表中还有更多内容。