Add Book to My BookshelfPurchase This Book Online

Chapter 12 - Terminals

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Terminal-Related Functions
Functions and methods for examining and changing terminal attributes are often used in conjunction with the three procedures ctermid, ttyname, and isatty.
The ctermid function is defined by the POSIX standard to return the name of the calling process' controlling terminal:
    #include <stdio.h>
    char *ctermid(char *s);
The single parameter s should point to a character array of at least L_ctermid bytes; this constant is defined in the include file. The name of the terminal will be stored in this array, and the address of the array returned. If s is null, ctermid stores the terminal name in an internal static array that is overwritten on each call, and returns a pointer to that array. If the process has no controlling terminal, ctermid returns a null pointer.
In the previous chapter, we said that a program can always refer to the file /dev/tty when it wants to reference the controlling terminal; this makes ctermid seem somewhat superfluous. However, this is only true for UNIX systems. Other POSIX-compliant systems, such as Digital's VMS, may use a different name. The ctermid function allows the name to be determined in a portable manner.
Use the ttyname function to obtain the name of the terminal attached to a specific file descriptor:
    #include <stdlib.h>
    char *ttyname(int fd);
The fd parameter should be an open file descriptor referencing a terminal device. A pointer to a static array containing the name of the terminal device associated with that file descriptor is returned. The null pointer is returned if the file descriptor does not refer to a terminal device. Note that ttyname will always return the real name of the terminal referenced by fd; it will never return /dev/tty.
The isatty function determines if a file descriptor does refer to a terminal device:
    #include <stdlib.h>
    int isatty(int fd);
The fd parameter should be a file descriptor referencing an open file. If the file is a terminal device, isatty returns 1; it returns 0 otherwise.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback