How are shell pipes set up to work with "more" command? - Unix

This is a discussion on How are shell pipes set up to work with "more" command? - Unix ; For example , if I do: ls -l | more then the stdout of ls -l is sent to the stdin of "more". But when "more" prompts for pagination it has to read from the keyboard but its stdin is ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: How are shell pipes set up to work with "more" command?

  1. How are shell pipes set up to work with "more" command?

    For example , if I do:

    ls -l | more

    then the stdout of ls -l is sent to the stdin of "more". But when
    "more" prompts for pagination it has to read from the keyboard but its
    stdin is coming down a pipe. So does it open and read directly from /
    dev/tty or is something else going on?

    I ask because I need to implement something like this myself in C
    soon.

    Thanks for any help.

    B2003

  2. Re: How are shell pipes set up to work with "more" command?

    On Friday 19 September 2008 10:50, Boltar wrote:

    > For example , if I do:
    >
    > ls -l | more
    >
    > then the stdout of ls -l is sent to the stdin of "more". But when
    > "more" prompts for pagination it has to read from the keyboard but its
    > stdin is coming down a pipe. So does it open and read directly from /
    > dev/tty or is something else going on?
    >
    > I ask because I need to implement something like this myself in C
    > soon.


    On my system (linux), strace shows that more reads keyboard commands from fd
    2, that is, stderr, while less reads from /dev/tty.


  3. Re: How are shell pipes set up to work with "more" command?

    On Sep 19, 10:06 am, pk wrote:
    > On my system (linux), strace shows that more reads keyboard commands from fd
    > 2, that is, stderr, while less reads from /dev/tty.


    That does seem to be the case. Doing "ls -l | more 2> /dev/null" stops
    pagination. Reading stderr seems an odd thing to do , perhaps its for
    historical reasons - no /dev/tty device file 30 years ago?

    B2003


  4. Re: How are shell pipes set up to work with "more" command?

    On Sep 19, 5:06*am, pk wrote:
    > On Friday 19 September 2008 10:50, Boltar wrote:
    >
    > > For example , if I do:

    >
    > > ls -l | more

    >
    > > then the stdout of ls -l is sent to the stdin of "more". But when
    > > "more" prompts for pagination it has to read from the keyboard but its
    > > stdin is coming down a pipe. So does it open and read directly from /
    > > dev/tty or is something else going on?

    >
    > > I ask because I need to implement something like this myself in C
    > > soon.

    >
    > On my system (linux), strace shows that more reads keyboard commands fromfd
    > 2, that is, stderr, while less reads from /dev/tty.


    My less tries both:

    #if
    /* Stuff for windows... */
    #else
    /*
    * Try /dev/tty.
    * If that doesn't work, use file descriptor 2,
    * which in Unix is usually attached to the screen,
    * but also usually lets you read from the keyboard.
    */
    #if OS2
    /* The __open() system call translates "/dev/tty" to "con". */
    tty = __open("/dev/tty", OPEN_READ);
    #else
    tty = open("/dev/tty", OPEN_READ);
    #endif
    if (tty < 0)
    tty = 2;
    #endif

    Though I guess in practice, being in Linux, /dev/tty works.

+ Reply to Thread