From: osycs@giac1.oscs.montana.edu (Craig Spannring)
Subject: Re: How can I do pure I/O? no buffer, echo, translations, etc...
Keywords: n
Organization: Geographic Information & Analysis Center Montana State University
Date: Sun, 27 Sep 1992 04:45:01 GMT

In article <1992Sep26.163036.9297@news.cs.indiana.edu> "Stefan Berg" <sgberg@peach.ucs.indiana.edu> writes:

[ 
  Stuff about wanting a un-echoed, unbuffered, non-blocking read routine
  wanted.
]

Here is a program that should help get you started-
It will work on BSD based systems.  I don't have a
System V based machine here so I'm not sure what
modifications will have to be made to get it to run
on those boxes.


>--- cut here ---<
#include <unistd.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sgtty.h>
#include <sys/ioctl.h>
#include <sys/types.h>


FILE *input;
struct sgttyb   old_control;

  static
  void
Raw()
{
    /****************************************************/
	/*													*/
	/* This puts the terminal driver in raw mode and    */
	/* turns the echo off.								*/
	/*													*/
    /****************************************************/

	struct sgttyb   control;


    if (isatty(fileno(input)))
    {
        gtty(fileno(input), &control);
		bcopy(&control, &old_control, sizeof( struct sgttyb ));
        control.sg_flags = (control.sg_flags | RAW) & (ECHO ^ 0xffffffff);
        stty(fileno(input), &control);
    }
	else
	{
		fprintf(stderr, "Error: Not a tty\n");
	}
} /* Raw() */

  static
  void
Cooked()
{
    /*****************************************************/
    /*                                                   */
    /* This resotres the terminal driver to the standard */
    /* cooked mode.                                      */
    /*                                                   */
    /*****************************************************/

	stty(fileno(input), &old_control);
} /* Cooked() */


main()
{
	int					ch;
	fd_set  			readfds;
	struct timeval		zero_wait = { 0, 0 };


	if (NULL == (input = fopen("/dev/tty", "r")))
	{
		fprintf(stderr, "Error: Couldn't open /dev/tty\n");
		exit(-1);
	}
	Raw();

	while(1)
	{
		/* We must reset readfds each time through the loop because */
		/* the select call clobbers it each time select gets called */
		FD_ZERO(&readfds);
		FD_SET(fileno(input), &readfds);

		/* The select function will tell us if there */
		/* is a file ready to be read                */
		if ( 0 != select(fileno(input)+1, &readfds, NULL, NULL, &zero_wait))
		{
			read(fileno(input), &ch, 1);
			if (3 == ch)
			{
				break;
			}
			printf("You pressed %c\015\n", ch);
		}
		else
		{
			/* There was no character ready to be */
			/* read, so process other things.     */
			printf("No character was selected\015\n");
			sleep(1);
		}
	}

	/* we must remeber to restore the terminal to cooked mode. */
	/* If we don't, most shells won't restore the terminal     */
	/* driver properly.                                        */
	Cooked();
	printf("\n");
} /* main() */

>---cut here---<

-- 
=========================================================================
 Six of one, 110 (base 2) of | Craig Spannring --- (406) 994-6128
 another.                    | osycs.giac1.oscs.montana.edu
 ----------------------------+ Geographic Information & Analysis Center