reading chars in raw mode with Ioctl .. How ?
Robert Felps
felps at convex.com
Tue Oct 16 12:39:56 AEST 1990
In <24905 at uflorida.cis.ufl.EDU> kcw at beach.cis.ufl.edu (Ken Whedbee) writes:
>I m looking for example code (bsd derivative .. SunOS perferably) that
>reads characters one at a time instead of buffering the chars until a
>newline like the read() system call.
>I understand that ioctl() can do this by setting the terminal into
>raw mode such that no pre-processing of the input takes place. There's
>example code of how to do this on pg. 340 in Bach's book. But alas,
>this is system V code.
>Does anyone know of or can give me an example in bsd ?
This should do BSD or SV single character input.
---------------------------------------------------------------------------
/*
This program resets the tty driver to allow single character input.
It provides for single character input in shell scripts.
Usage:
ANSWER=`getsks`
To compile and install:
cc -D[BSD|SV] getsinglekey.c -o getsks
chmod 555 getsks
cp getsks /usr/local/bin
*/
#include <sys/types.h>
#include <signal.h>
#ifdef BSD
#include <sgtty.h>
/* include the BSD terminal handling header file. */
struct sgttyb new, old; /* BSD term-info structure */
#else
/* include the System V terminal handling header file. */
#include <termio.h>
struct termio new, old; /* SYSV term-info structure */
#define gtty(fd,arg) (ioctl(fd, TCGETA, arg))
#define stty(fd,arg) (ioctl(fd, TCSETA, arg))
#endif
main()
{
int c;
gtty(0,&old);
gtty(0,&new);
#ifdef BSD /* set "raw" mode for BSD */
new.sg_flags &= ~(ECHO|CRMOD);
new.sg_flags |= RAW;
#else
new.c_iflag &= ~(INLCR|ICRNL|IUCLC|ISTRIP|IXON|BRKINT);
new.c_oflag &= ~OPOST;
new.c_lflag &= ~(ICANON|ISIG|ECHO);
new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 1;
#endif
stty(0,&new);
if (c=getchar()) {
stty(0,&old);
putchar(c);
exit(0);
}
}
---------------------------------------------------------------------------
Hope it helps,
Robert Felps felps at convex.com
Convex Computer Corp OS System Specialist
3000 Waterview Parkway Tech. Assistant Ctr
Richardson, Tx. 75083 1(800) 952-0379
More information about the Comp.unix.programmer
mailing list