What does ttyn (3) function return?

The man page is here: http://man.cat-v.org/unix-6th/3/ttyn

In this example:

if (ttyn(0) = 'x'){
...
}

      

The man page states that "x is returned if the specified file does not match Typewriter."

The specified file will be argument 0, so standard notation, right?

What is a typewriter? My keyboard?

What are you checking with this line?

if (ttyn(0) = 'x')

      

+3


source to share


2 answers


At this point, the typewriter (or teletype or tty) was an RS-232 terminal connected to the computer via a serial port. Recording device /dev

corresponding to these ports are called /dev/tty0

, /dev/tty1

, /dev/ttya

etc. Each of these files was a special symbol file, as opposed to a regular file.

When a terminal was discovered by the system, usually by powering on or connecting via a modem, the process init

opened the device in file descriptors 0, 1, and 2 in a new process, and these file descriptors were persisted through the login process, user shell, and any processes laid out from the shell ...

As you said in your question, file descriptor 0 is also called standard input.

The function ttyn

calls fstat

in its argument, which returns some information about the file, such as its naming number, resolution, etc. ttyn

then reads through /dev

, looking through each file that starts with "tty"

to see which one has an inode number as an argument ttyn

. When it finds a match, it returns the 4th character of the name of file that will be '0'

, '1'

, 'a'

etc. If no matches are found, it returns 'x'

.

The PDP-11 usually had a console and several 8-port serial interfaces. therefore it was not ttyx

. And you can name the devices in /dev

whatever you want. Therefore, it was easy to avoid /dev/ttyx

being the actual device.



Type commands goto

can be used ttyn(0) != 'x'

to determine if the user actually typed a command on the terminal.


Here is the default config file /etc/ttys

used init

in V6. The console was tty8

.

In V7 Unix, the ttyn functionality was replaced with ttyname , which can contain longer device names, and isatty , which returned true if the handle to the flash drive was a terminal device. The goto command was missing in V7.

+7


source


I've never seen this library call before; I'm used to being more familiar ttyname

. The webpage doesn't give a return value, but depending on what the text says it would give the last value char

in the string returned ttynam(3)

. So if stdin (fd0) is connected to "/ dev / tty2" then the return value is char 2

. And in C, you can check this with:

if (ttyn(0) == '2') { ... }

      



The documentation provided is not clear. And he uses bad terminology; instead of "typewriter" he should use "teletype" or "terminal", which are the accepted terms. Remember that stdin may differ from stdout; it is quite possible to run run cat </dev/tty1 > /dev/tty2

if you have permissions for it.

0


source







All Articles