模仿c中的"cat >"命令(LINUX/UNIX)

Imitate "cat >" command in c (LINUX/UNIX)

我需要模仿命令:"cat > " using c with exec family

我找不到解决方法,因为它无法识别符号“>”,我已经尝试使用 execlp,但可能我的语法有误。

感谢您的帮助!赞赏。

命令是cat> 由 shell 解释,将命令的标准输出重定向到特定文件。

因此,您需要将进程 运行 cat 的标准输出(文件描述符 1)连接到文件:

/* emulating `cat >file` */
int fd = open("file", …);
/* use our own fd 1 */
dup2(fd, 1);
close(fd);
/* with new stdout, exec `cat` */
exec("cat");