Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 18.3 Special Input Characters 679


Example


Beforedescribing all the special characters in detail, let’s look at a small program that
changes them. The program in Figure18.10 disables the interrupt character and sets the
end-of-file character to Control-B.

#include "apue.h"
#include <termios.h>

int
main(void)
{
struct termios term;
long vdisable;

if (isatty(STDIN_FILENO) == 0)
err_quit("standard input is not a terminal device");

if ((vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE)) < 0)
err_quit("fpathconf error or _POSIX_VDISABLE not in effect");

if (tcgetattr(STDIN_FILENO, &term) < 0) /* fetch tty state */
err_sys("tcgetattr error");

term.c_cc[VINTR] = vdisable; /* disable INTR character */
term.c_cc[VEOF] = 2; /* EOF is Control-B */

if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) < 0)
err_sys("tcsetattr error");

exit(0);
}

Figure 18.10 Disable interrupt character and change end-of-file character

Note the following points regarding this program.

•Wemodify the terminal characters only if standardinput is a terminal device.
We callisatty(Section 18.9) to check this.
•Wefetch the_POSIX_VDISABLEvalue usingfpathconf.
•The functiontcgetattr(Section 18.4) fetches atermiosstructurefromthe
kernel. After we’ve modified this structure, we call tcsetattrto set the
attributes. The only attributes that change arethe ones we specifically modified.
•Disabling the interrupt key is different from ignoring the interrupt signal. The
program in Figure18.10 simply disables the special character that causes the
terminal driver to generateSIGINT.Wecan still use thekillfunction to send
the signal to the process.
Free download pdf