The Terminal Has a Name

In Linux and Unix, the terminal is assigned a name. Specifically, it’s the name of a file located in the /dev directory. This configuration is necessary because the operating system treats all devices as files. Like a file, you can read and write from the terminal; it’s an I/O device. To get started, you must know the current terminal’s filename.

The ttyname() function obtains the name of a terminal. Here’s the man page format:

char *ttyname(int fd);

Argument fd is a file descriptor – not a FILE handle, but the number assigned to an open file. Read my blog post here for details on file descriptors.

The value returned from ttyname() is a string representing terminal’s pathname.

This function requires you to include the unistd.h header file. While this header file is available to Windows C compilers, the ttyname() function is available only in Unix and Linux.

Here’s sample code:

2020_10_03-Lesson-a.c

#include <stdio.h>
#include <unistd.h>

int main()
{
    char *term;

    term = ttyname(0);
    printf("This is terminal %s\n",term);

    return(0);
}

The ttyname() function is called with argument 0. This value represents the file descriptor for the standard input device, stdin, which is the current terminal. Here’s sample output:

This is terminal /dev/tty1

The terminal name you see may be different, depending on the OS and other weird things.

Once you know the terminal’s filename, you can open it for I/O just like any other file. You can use the formatted file functions, as demonstrated in this code:

2020_10_03-Lesson-b.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    char *term;
    FILE *tout;

    /* obtain terminal name and open */
    term = ttyname(0);
    tout = fopen(term,"w");
    if( tout==NULL )
    {
        fprintf(stderr,"Unable to open %s\n",term);
        exit(1);
    }

    /* send text to the terminal */
    fprintf(tout,"Hello, terminal %s\n",term);

    /* close */
    fclose(tout);

    return(0);
}

The terminal filename is obtained at Line 11: term = ttyname(0); The next statement attempts to open this file for writing (output) by using the fopen() function: tout = fopen(term,"w"); The tout file handle is then tested to ensure that the file was opened.

At Line 20, text is output to the terminal. The file is closed at Line 23.

Here’s a sample run:

Hello, terminal /dev/tty1

Of course, you could always just write to standard output; the output goes to the current terminal by default. Yet in this sample code, the terminal’s device/filename was obtained, opened for output, then the output is sent via the terminal’s file/device instead. It’s a roundabout way of doing things, but it drives home the point that all devices in Unix are files and can be treated as such.

2 thoughts on “The Terminal Has a Name

  1. Do all peripherals (in the widest sense, including built-in drives, USB ports etc) have filenames. For example could you open a keyboard file instead of using stdin?

  2. I believe the BeOS carried the “everything is a device” concept to the degree that you could open the keyboard, but not Unix/Linux. You can use an ioctl on the device to access the hardware. I’ve never dived that deep.

Leave a Reply