reading chars in raw mode with Ioctl .. How ?

James Brister brister at decwrl.dec.com
Tue Oct 16 23:52:24 AEST 1990


On 15 Oct 90 20:31:15 GMT, kcw at beach.cis.ufl.edu (Ken Whedbee) said:

> I m looking for example code (bsd derivative .. SunOS perferably) that
> reads characters one at a time instead of buffering the chars until a
> newline like the read() system call.

Here's my (very old) version of the "pick" program from Kernighan and
Ritchie's UNIX programming book. It uses CBREAK mode, and it runs on
Ultrix--but I doubt porting would be a problem.

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  pick.c
# Wrapped by brister at westworld on Tue Oct 16 09:51:57 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'pick.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pick.c'\"
else
echo shar: Extracting \"'pick.c'\" \(1018 characters\)
sed "s/^X//" >'pick.c' <<'END_OF_FILE'
X#include <sgtty.h>
X#include <stdio.h>
X
char *progname ;      /* name of program for error message */
int pipeout ;
X
main (argc,argv)
X  int argc ;
X  char *argv [] ;
X{
X  int i ;
X  struct sgttyb oldmode, newmode ;
X
X
X  if (argc == 1)
X    {
X      fprintf (stderr,"%s [-] [files...]\n",progname) ;
X      exit (1) ;
X    }
X
X  pipeout = isatty (0) ;
X  progname = argv [0] ;
X  if ( gtty (0,&oldmode) == -1 )
X    { perror ("gtty") ;} 
X  newmode = oldmode ;
X  newmode.sg_flags |= CBREAK ;
X  if ( stty (0,&newmode) == -1 )
X    { perror ("stty") ; }
X
X  i = 1 ;
X  while ( i < argc && pick (argv [i]) == 1 )
X    i++ ;
X
X  stty (0,&oldmode) ;
X  exit (0) ;
X}
X
pick (s)
X  char *s ;
X{
X  int c ; 
X
X  fprintf (stderr, "%s? ",s) ;
X  c = ttyin () ; 
X  fprintf (stderr,"\n\r") ;
X  if ( c == 'y' )
X    {
X      printf ("%s\n",s) ;
X      return 1 ;
X    }
X  else if ( c == 'q' )
X    return 0 ;
X  else 
X    return 1 ;
X}
X
int ttyin ()
X{ 
X  char c ;
X  if ( ! read (0,&c,sizeof (c)) )
X    { perror ("read") ; return 'q' ; }
X  else
X    return c ; 
X}
X
X 
END_OF_FILE
if test 1018 -ne `wc -c <'pick.c'`; then
    echo shar: \"'pick.c'\" unpacked with wrong size!
fi
# end of 'pick.c'
fi
echo shar: End of shell archive.
exit 0




James
--
James Brister                                           brister at decwrl.dec.com
DEC Western Software Lab., Palo Alto, CA    {uunet,sun,pyramid}!decwrl!brister



More information about the Comp.unix.programmer mailing list