如何恢复被dup2覆盖的stdin?
发布时间:2020-12-15 21:26:53 所属栏目:安全 来源:网络整理
导读:我试图用另一个管道替换stdin,然后将原始stdin放回到fd#0. 例如 dup2(p,0); // p is a pre-existing fd of a pipeexec(/* some commands */);//what will be here in order to have the original stdin back?scanf(...) //continue processing with original
我试图用另一个管道替换stdin,然后将原始stdin放回到fd#0.
例如 dup2(p,0); // p is a pre-existing fd of a pipe exec(/* some commands */); //what will be here in order to have the original stdin back? scanf(...) //continue processing with original stdin. 解决方法
一旦被覆盖(关闭),您就无法恢复原始状态.您可以做的是在覆盖它之前保存它的副本(当然需要提前计划):
int old_stdin = dup(STDIN_FILENO); dup2(p,STDIN_FILENO); close(p); // Usually correct when you dup to a standard I/O file descriptor. …code using stdin… dup2(old_stdin,STDIN_FILENO); close(old_stdin); // Probably correct scanf(…); 但是,你的代码提到了exec(……一些命令……); – 如果这是POSIX (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |