Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!rochester!pt!ius2.cs.cmu.edu!edw
From: edw@ius2.cs.cmu.edu (Eddie Wyatt)
Newsgroups: comp.lang.c,comp.unix.questions
Subject: Re: Keyboard Input (Revised Question) ...
Message-ID: <1231@ius2.cs.cmu.edu>
Date: Sun, 12-Jul-87 23:01:32 EDT
Article-I.D.: ius2.1231
Posted: Sun Jul 12 23:01:32 1987
Date-Received: Mon, 13-Jul-87 05:36:25 EDT
References: <1043@bucsb.bu.edu.UUCP>
Organization: Carnegie-Mellon University, CS/RI
Lines: 109
Keywords: Keyboard
Xref: mnetor comp.lang.c:3014 comp.unix.questions:3170

In article <1043@bucsb.bu.edu.UUCP>, eap@bucsb.bu.edu.UUCP (Eric Pearce) writes:
> 
> 
> 
> I would like to write a routine that performs a repeated sequence of statements
> (i.e. a loop) that would check for input from the keyboard and do something
> according to what was typed in.  Also, it would be able to continue doing the
> loop regardless of whether or not anything was typed in from the keyboard.
> 


   This seems to be a common question in Unix. To follow, one
procedure that reads one character without waiting for a return
and one procedure that determines if a port is ready to read from.
Modify them to fit your needs if you want.  Do a man stty and ioctl
to find out what the proper include files are.




/**************************************************************************
 *                                                                        *
 *                              w_read_char                               *
 *                                                                        *
 **************************************************************************

   Purpose :  This function reads a single character from input without
	    waiting for a return to be typed.

   Programmer : Eddie Wyatt
 
   Date : July 1987

   Input : None

   Output : returns the current character from standard input

   Locals : 
	ch - the character read
	omode - the old tty mode (used in restoring mode after the character
	        is read)
	mode - use to change the tty mode so a single character can
	       be read without waiting for a return

   Globals : 
	stdin - not modified

 ************************************************************************/

int w_read_char()
    {
    struct sgttyb omode, mode;
    int ch;

    gtty(fileno(stdin),&mode);
    bcopy((char *) &mode, (char *) &omode,sizeof(struct sgttyb));
    mode.sg_flags |= CBREAK;

    stty(fileno(stdin),&mode);
    ch = getchar();
    stty(fileno(stdin),&omode);

    return(ch);
    }




/**************************************************************************
 *                                                                        *
 *                             ready_to_read                              *
 *                                                                        *
 **************************************************************************

   Purpose :  This function returns TRUE if the port (fd) is ready to
	    read from.

   Programmer : Eddie Wyatt
 
   Date : January 1987

   Input : 
   fd - a port

   Output :  returns TRUE if the port is ready to read

   Locals : 
    num - the number of bytes ready to read

   Globals : None

 ************************************************************************/

int ready_to_read(fd)
    int fd;
    {
    int num;

    ioctl(fd,FIONREAD,(char *)&num);
    return(num > 0);
    }


-- 
					Eddie Wyatt

e-mail: edw@ius2.cs.cmu.edu

terrorist, cryptography, DES, drugs, cipher, secret, decode, NSA, CIA, NRO.