Help, single char input

coolbean nieters at eagle.crd.ge.com
Fri Oct 19 05:10:09 AEST 1990



hi there
	A readchar() program to suit your needs follows.  I wasn't
sure what version OS you were working on so I wrote a version to work
on Sun OS 3.5 and one to work on Sun OS 4.0.  A sample main() is
included too.  

hope this helps.

--ed

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

main()
{
  int x;

  printf("enter a character: ");
  x = readchar();
  printf("\nthe letter %c was entered.\n", x);
}


>>>>> Sun OS 3.5 Version: <<<<<

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

int
readchar()
{
  struct sgttyb old, new;
  int x;

  ioctl(0, TIOCGETP, &old);	/* get the current tty settings */
  new = old;
  new.sg_flags |= CBREAK;

  ioctl(0, TIOCSETP, &new);	/* set the new tty options */

  x=getchar();

  ioctl(0, TIOCSETP, &old);	/* return the tty to its original state */
  return(x);
}


>>>>> Sun OS 4.0 Version: <<<<<


#include <stdio.h>
#include <sys/termios.h>
#include <fcntl.h>

int
readchar()
{
  struct termios old, new;
  int x;

  ioctl(0, TCGETS, &old);	/* get the current tty settings */
  new=old;
  new.c_lflag &= ~ICANON;
  new.c_cc[VMIN]=1;
  new.c_cc[VTIME]=0;
  
  ioctl(0, TCSETS, &new);	/* set the new tty options */

  x=getchar();

  ioctl(0, TCSETS, &old);	/* return the tty to its original state */
  return(x);
  
}
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Ed Nieters		     INTERnet: nieters at crd.ge.com
GE Corporate R&D	     UUCPnet:  uunet!crd.ge.com!nieters	       
Schenectady, NY 12301	     BELLnet:  (518) 387-5187



More information about the Comp.unix.questions mailing list