Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

694 Te rminal I/O Chapter 18


Thetcsendbreak function transmits a continuous stream of zerobits for a
specified duration. If thedurationargument is 0, the transmission lasts between 0.25
second and 0.5 second. POSIX.1 specifies that ifdurationis nonzero, the transmission
time is implementation dependent.

18.9 Ter minal Identification


Historically,the name of the controlling terminal in most versions of the UNIX System
has been/dev/tty.POSIX.1 provides a runtime function that we can call to determine
the name of the controlling terminal.

#include <stdio.h>

char *ctermid(char *ptr);

Returns: pointer to name of controlling terminal
on success, pointer to empty string on error

Ifptris non-null, it is assumed to point to an array of at leastL_ctermidbytes, and the
name of the controlling terminal of the process is stored in the array.The constant
L_ctermidis defined in<stdio.h>.Ifptris a null pointer,the function allocates
room for the array (usually as a static variable). Again, the name of the controlling
terminal of the process is stored in the array.
In both cases, the starting address of the array is returned as the value of the
function. Since most UNIX systems use/dev/ttyas the name of the controlling
terminal, this function is intended to aid portability to other operating systems.

All four platforms described in this text return the string/dev/ttywhen we callctermid.

Example —ctermidFunction


Figure18.12 shows an implementation of the POSIX.1ctermidfunction.
#include <stdio.h>
#include <string.h>

static char ctermid_name[L_ctermid];

char *
ctermid(char *str)
{
if (str == NULL)
str = ctermid_name;
return(strcpy(str, "/dev/tty")); /* strcpy() returns str */
}

Figure 18.12Implementation of POSIX.1ctermidfunction
Free download pdf