我正在看一下'less'实用程序的代码,特别是它如何获得键盘输入.有趣的是,在ttyin.c的第80行,它将文件描述符设置为:
/*
* 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;
Run Code Online (Sandbox Code Playgroud)
是不是文件描述符2 stderr?如果是这样,WTH ?! 我以为键盘输入是通过stdin发送的.
有趣的是,即使你这样做ls -l * | less,在文件加载完成后,你仍然可以使用键盘上下滚动,但如果你这样做ls -l * | vi …