Determining Your tty Name

If your application must request input from the terminal, you can always open the special pathname "/dev/tty". This special pathname causes the UNIX kernel to open the real pathname necessary to gain access to the controlling terminal. This allows your application to request a password from the user, for example.

There are other times when you need to know if a particular file descriptor is a tty device or not. This frequently occurs when dealing with standard input, which is provided by the shell. How does the application tell when the standard input is redirected to take data from a file, or when the data is coming from a terminal? Perhaps the user prompt is to be suppressed if the input is coming from a file. The ttyname(3) and isatty(3) functions solve these thorny problems.

#include <unistd.h>

char * ttyname(int fd);

int isatty(int fd);

The ttyname(3) function accepts an open file descriptor as its only input argument. It returns a string pointer for the tty device if isatty(3) returns true. Otherwise, ttyname(3) will return a null pointer. The errno value is not affected.

Function isatty(3) accepts an open file descriptor as its only input argument. It returns true if the file descriptor represents a terminal and false when it is not a tty.

Listing 4.6 shows a simple program putting these functions to work on standard input, output, and error.

Code Listing 4.6. tty.c—A Test Program for ttyname(3) and isatty(3)
1:   /* tty.c */
2:
3:   #include <stdio.h>
4:   #include <unistd.h>
5:
6:   void
7:   tty_info(int fd) {
8:       int b = isatty(fd);                 /* Test if a tty */
9:
10:      printf("fd=%d %s a tty
",fd,b?"is":"isn't");
11:      if ( b )
12:          printf("tty name is '%s'
",ttyname(fd));
13:  }
14:
15:  int
16:  main(int argc,char **argv) {
17:
18:      tty_info(0);                        /* Query standard input */
19:      tty_info(1);                        /* Query standard output */
20:      tty_info(2);                        /* Query standard error */
21:      return 0;
22:  }
					

The program in Listing 4.6 tests the status of each of the shell-provided file descriptors, standard input, output, and error. The following shows a compile-and-execute session:

$ make tty
cc -c -D_POSIX_C_SOURCE=199309L -Wall tty.c
cc tty.o -o tty
$ ./tty
fd=0 is a tty
tty name is '/dev/ttyp2'
fd=1 is a tty
tty name is '/dev/ttyp2'
fd=2 is a tty
tty name is '/dev/ttyp2'
$ ./tty 2>/dev/null </dev/null
fd=0 isn't a tty
fd=1 is a tty
tty name is '/dev/ttyp2'
fd=2 isn't a tty
$

The first time ./tty is invoked, all three file descriptors are identified as a tty device. The second time the program is invoked, the standard input and standard error are redirected to /dev/null. The program correctly identifies that file descriptors 0 (standard input) and 2 (standard error) are not terminal devices.

When running this program with standard output redirected, just keep in mind that standard output is where the program output is going.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.34.178