Testing if keystrokes are waiting in the buffer (solution)

Warren Tucker wht at n4hgf.uucp
Fri May 4 06:15:34 AEST 1990


In article <24107 at mimsy.umd.edu> chris at mimsy.umd.edu (Chris Torek) writes:
>(First, an obligatory note to comp.lang.c readers: the notion of a `key'
>is found nowhere in the C language, therefore the notion of testing for
>...

well said
>Readers not interested in Unix details can skip the rest of
>this followup.

ditto

BSD:  ioctl(0,FIONREAD,&number_of_chars_waiting_to_be_read);

XENIX V, SCO UNIX, UNIX Sys V Rel 4:
      if(rdchk(0))  /* if ICANON is on, rdchk will report nothing until
                     * newline or whatever is typed */
      {
         data is waiting ...
      }
      else
      {
         no data is waiting ...
      }
Sys V Rel 2, standard Rel 3: (tricky; this is just a clue)
    ioctl(0,TCGETA,&termio_struct_at_beginning);

    ioctl(0,TCGETA,&termio_struct);
    termio_struct.c_lflags ~= ~(ICANON);
    termio_struct.c_cc[VMIN] = 0;
    termio_struct.c_cc[VTIME] = 0;
    ioctl(0,TCSETA,&termio_struct);

    if((i = read(0,&input_char,1) == 0)
    {
        no input
    }
    else if(i < 0)
    {
        read error
    }
    else
    {
        'input_character' has a charcter
    }

    /* before program terminates */
    ioctl(0,TCSETA,&termio_struct_at_beginning);

V7: horrible /dev/kmem munging you dont want to hear about

------------------------------------------------------------------
Warren Tucker, TuckerWare gatech!n4hgf!wht or wht%n4hgf at gatech.edu
McCarthyism did to cinema what ANSI did to C,  cast a great number
of characters into the void.



More information about the Comp.lang.c mailing list