【发布时间】:2016-04-07 11:29:58
【问题描述】:
您好,我一直在用 c 编写 shell,但在尝试重定向时卡住了。在我的程序中重定向标准输出时,标准输入不起作用。
void redirect(node_t* node){
// mode 0: >$%d mode 1: < mode 2: > mode 3: >>
int input = 0;
if(node->redirect.mode == 2){
input = 1; // >
} else{
input = 0; // <
}
int pid = 0;
int *status = 0;
char * filename = node->redirect.target; // filename
int fd;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
if((pid = fork()) == 0){
dup2(fd, input); // STDIN OR STDOUT
close(fd);
node_t* node2 = node->redirect.child;
execvp(node2->command.program, node2->command.argv); // execute program
printf("failed to execvp\n");
exit(1);
} else {
wait(status);
}
}
我是 fork() 的新手,但我的问题是我在这里做错了什么,重定向标准输出有效,但标准输入没有向给定文件写入任何内容。
【问题讨论】:
-
由于文件描述符打开
O_WRONLY | O_CREAT | O_TRUNC,子进程无法从中读取任何数据。在标准输入重定向的情况下,您需要打开它O_RDONLY。 -
@Ctx 我已经用
O_RDONLY替换了O_WRONLY,但它没有任何效果。但是既然我需要将标准输入写入其中,为什么还要准备好任何数据呢? -
重定向进程的标准输入通常意味着给它一个文件描述符以读取(这只是一个约定,从技术上讲当然也可以写入标准输入)。
-
你最后的评论让我很困惑。你试着写
stdin?您能否制作一个 MCVE,以便更好地理解您要做什么?