Search code examples
ttyc

What does the function ttyn(3) return?


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

This example:

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

The man page says "x is returned if the indicated file does not correspond to a typewriter."

The indicated file would be argument 0, so the standardinput, right?

And what is a typewriter? My keyboard?

What are you checking with this line?

if (ttyn(0) = 'x')

Solution

  • At that point in time, a typewriter (or teletype, or tty) was an RS-232 terminal connected to the computer via a serial port. The device entries in /dev corresponding to these ports were named /dev/tty0, /dev/tty1, /dev/ttya, etc. Each of those files was a character special file, as opposed to an ordinary file.

    When a terminal was detected by the system, typically by being turned on or connected through a modem, the init process opened the device on file descriptors 0, 1, and 2 in a new process, and those file descriptors persisted through the login process, a user's shell, and any processes forked from the shell.

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

    The ttyn function calls fstat on its argument, which returns some info about the file such as its inode number, permissions, etc. ttyn then reads through /dev, looking at each file that starts with "tty", to see which one has the same inode number as ttyn's argument. When it finds a match, it returns the 4th character of the filename, which would be '0', '1', 'a', etc. If no matches are found, it returns 'x'.

    There were generally a console and a few 8-port serial interfaces on a PDP-11. so there was no ttyx. And you could name devices in /dev anything you wanted. So it was easy to avoid /dev/ttyx being an actual device.

    Commands like goto could use ttyn(0) != 'x' to determine whether the user was actually typing the command on a terminal.


    Here is the default config file, /etc/ttys, used by init in V6. The console was tty8.

    In V7 Unix, the functionality of ttyn was replaced by ttyname, which could accommodate longer device names, and isatty, which returned true if the fle descriptor was a terminal device. The goto command was not present in V7.