Search code examples
unixprocesspipestdinless-unix

How does a program like less handle both piped stdin and keyboard input?


If I run, for example, ifconfig | less, the less program manages to at the same time:

  1. Capture output of ifconfig through the standard input where the output of ifconfig is piped
  2. React to user's keyboard input and be interactive

I never gave this much thought, but as I tried to write a program with functionality similar to less, I got confused: how does it manage to do both at the same time, and what does it do to implement 2, as it's clearly not the standard input?


Solution

  • It reads keyboard input from /dev/tty instead of from stdin.