Path: utzoo!attcan!uunet!cs.utexas.edu!wuarchive!udel!haven!decuac!bacchus.pa.dec.com!decwrl!brister From: brister@decwrl.dec.com (James Brister) Newsgroups: comp.unix.programmer Subject: Re: reading chars in raw mode with Ioctl .. How ? Message-ID: Date: 16 Oct 90 13:52:24 GMT References: <24905@uflorida.cis.ufl.EDU> Sender: news@wrl.dec.com (News) Distribution: na Organization: DEC Western Software Lab Lines: 107 In-Reply-To: kcw@beach.cis.ufl.edu's message of 15 Oct 90 20:31:15 GMT X-Checksum-Snefru: ff6547fe 34c6df6b bac4522f 5dea7f83 On 15 Oct 90 20:31:15 GMT, kcw@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 'pick.c' <<'END_OF_FILE' X#include X#include 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@decwrl.dec.com DEC Western Software Lab., Palo Alto, CA {uunet,sun,pyramid}!decwrl!brister