Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 379005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:50:59+00:00 2026-05-12T14:50:59+00:00

I’m taking a look at the code to the ‘less’ utility, specifically how it

  • 0

I’m taking a look at the code to the ‘less’ utility, specifically how it gets keyboard input. Interestingly, on line 80 of ttyin.c, it sets the file descriptor to read from:

     /*
      * 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;

Isn’t file descriptor 2 stderr? If so, WTH?! I thought keyboard input was sent through stdin.

Interestingly, even if you do ls -l * | less, after the file finishes loading, you can still use the keyboard to scroll up and down, but if you do ls -l * | vi, then vi will yell at you because it doesn’t read from stdin. What’s the big idea? How did I end up in this strange new land where stderr is both a way to report errors to the screen and read from the keyboard? I don’t think I’m in Kansas anymore…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:51:00+00:00Added an answer on May 12, 2026 at 2:51 pm
    $ ls -l /dev/fd/
    lrwx------ 1 me me 64 2009-09-17 16:52 0 -> /dev/pts/4
    lrwx------ 1 me me 64 2009-09-17 16:52 1 -> /dev/pts/4
    lrwx------ 1 me me 64 2009-09-17 16:52 2 -> /dev/pts/4
    

    When logged in at an interative terminal, all three standard file descriptors point to the same thing: your TTY (or pseudo-TTY).

    $ ls -fl /dev/std{in,out,err}
    lrwxrwxrwx 1 root root 4 2009-09-13 01:57 /dev/stdin -> fd/0
    lrwxrwxrwx 1 root root 4 2009-09-13 01:57 /dev/stdout -> fd/1
    lrwxrwxrwx 1 root root 4 2009-09-13 01:57 /dev/stderr -> fd/2
    

    By convention, we read from 0 and write to 1 and 2. However, nothing prevents us from doing otherwise.

    When your shell runs ls -l * | less, it creates a pipe from ls‘s file descriptor 1 to less‘s file descriptor 0. Obviously, less can no longer read the user’s keyboard input from file descriptor 0 – it tries to get the TTY back however it can.

    If less has not been detached from the terminal, open("/dev/tty") will give it the TTY.

    However, in case that fails… what can you do? less makes one last attempt at getting the TTY, assuming that file descriptor 2 is attached to the same thing that file descriptor 0 would be attached to, if it weren’t redirected.

    This is not failproof:

    $ ls -l * | setsid less 2>/dev/null
    

    Here, less is given its own session (so it is no longer a part of the terminal’s active process group, causing open("/dev/tty") to fail), and its file descriptor 2 has been changed – now less exits immediately, because it is outputting to a TTY yet it fails to get any user input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 201k
  • Answers 201k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no direct way to do case insensitive lookup… May 12, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer i know this is an old post, but i ran… May 12, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer <a href="http://linkhere" onclick="return dostuff()">Stuff</a> Make sure that the dosuff function… May 12, 2026 at 8:15 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.