Need Help: 1-char input in C
rf at wu1.UUCP
rf at wu1.UUCP
Wed Dec 28 09:43:14 AEST 1983
The following will work only if you controlling terminal is not
initially in cbreak mode:
system("stty cbreak");
c=getchar();
system("stty -cbreak");
Raw mode should be avoided, since it will sometimes return
characters with whose values are either less than zero or
greater than 127. A switch to raw mode may also cause the
terminal to lose some output, since raw mode disables the
XON/XOFF protocol.
You probably want to enable cbreak mode when you begin your
program and end it when you're done. The following less
portable code is a hacked version of something which does work
and is much faseter than system("stty . . .");. It probably
works, but has not been tested.
struct sgttyb otsgb;
/* iniline -- initialize line
This routine stores the terminal characteristics of stdin in
otsgb and turns on cbreak mode. If stdin's file is not a
terminal (perhaps for testing,) iniline does nothing.
*/
iniline() {
struct sgttyb ntsgb;
if (isatty(fileno(stdin))) {
ioctl(fileno(stdin), TIOCGETP, &otsgb);
ntsgb = otsgb;
ntsgb.sg_flags |= CBREAK;
ioctl(fileno(stdin), TIOCSETP, &ntsgb);
}
}
/* resetline -- reset line characteristics
This routine restores the terminal characteristics of stdin,
which should be preserved in otsgb. If the line's file is not a
terminal (perhaps for testing,) resetline does nothing.
*/
resetline() {
if (isatty(fileno(stdin))) {
ioctl(fileno(stdin), TIOCSETP, &otsgb);
}
}
Randolph Fritz
Western Union Telegraph
More information about the Comp.lang.c
mailing list