
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <stdio.h>

int get_one_char(f)
   int f;
{
     struct sgttyb tty, ntty;
     int ttyset, stat, ch;

     ttyset = 0;

     stat = ioctl(f, TIOCGETP, &tty);
     if (stat == -1) {
          perror("ioctl");
          return(-1);
     }

     if (! (tty.sg_flags & CBREAK)) {
          ntty = tty;
          ttyset = (! ttyset);
          ntty.sg_flags |= CBREAK;
          ntty.sg_flags &= (~ ECHO);
          stat = ioctl(f, TIOCSETN, &ntty);
          if (stat == -1) {
               perror("ioctl");
               return(-1);
          }
     }

     ch = getchar();

     if (ttyset) {
          stat = ioctl(f, TIOCSETN, &tty);
          if (stat == -1) {
               perror("ioctl");
               return(-1);
          }
     }

     return(ch);
}

