【发布时间】:2012-02-28 18:35:28
【问题描述】:
我正在制作模拟 Unix shell 的简单 ANSI C 程序。所以我正在使用 fork() 创建子进程,在子进程内部我调用 exec() 来运行给定的(由用户)程序。
我需要做的是将文件内容重定向到标准输入,这样它就可以发送给用户调用程序。
Example: cat < file \\user wants run cat and redirect content of that file to it by typing this to my program prompt
我正在尝试这样做:
...child process...
int fd = open(path_to_file, O_RDONLY);
int read_size = 0;
while ((read_size = read(fd, buffer, BUF_SIZE)) != 0) {
write(STDIN_FILENO, buffer, read_size);
}
close(fd);
execlp("cat", ...);
一切正常,文件内容写入标准输入,但读取整个文件后,cat 仍在等待输入(我需要告诉 cat,输入结束),但我不知道如何:-(?
有什么想法吗?非常感谢!!!
【问题讨论】: