【问题标题】:why more command is not take stdin for reading input?为什么更多命令不使用标准输入来读取输入?
【发布时间】:2015-01-19 12:58:31
【问题描述】:

在 more 命令中,它不会从标准输入读取输入。但是使用 dup2() 函数将标准输入更改为一个文件描述符,届时它将起作用。为什么会这样?

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
main()
   {
   int fd=open("coprocess.c",O_RDWR);

    if ( fork() ==  0 ) 
    { 
    dup2(fd,0);
    close(fd);
    execl("/bin/more","more",(char *)0);
    printf("error\n");
    }
    else 
    wait(0);

}

【问题讨论】:

  • 显然更多是从标准输入读取,否则您的 dup2 示例将无法正常工作!?

标签: c linux exec dup2


【解决方案1】:

'more' 可以从标准输入或命令行中给出的文件中读取。看起来在命令行上给出文件路径的情况下,“更多”打开该文件并重新执行自身,将打开的文件描述符设置为标准输入。

在此过程中,'more' 实现的核心始终是从标准输入读取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2014-12-16
    • 1970-01-01
    • 2014-11-17
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多