🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #1040

Re: reading chars in raw mode with Ioctl .. How ?

#1040
From: Robert Felps
Date: Tue, 16 Oct 1990 02:39
79 lines
2641 bytes
In <24905@uflorida.cis.ufl.EDU> kcw@beach.cis.ufl.edu (Ken Whedbee) writes:

>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.

>I understand that ioctl() can do this by setting the terminal into
>raw mode such that no pre-processing of the input takes place.  There's
>example code of how to do this on pg. 340 in Bach's book.  But alas,
>this is system V code.

>Does anyone know of or can give me an example in bsd ?

This should do BSD or SV single character input.

---------------------------------------------------------------------------
/*
This program resets the tty driver to allow single character input.
It provides for single character input in shell scripts.

Usage:
	ANSWER=`getsks`

To compile and install:

	cc -D[BSD|SV] getsinglekey.c -o getsks
	chmod 555 getsks
	cp getsks /usr/local/bin
 */

#include <sys/types.h>
#include <signal.h>

#ifdef BSD
#include <sgtty.h>
	/* include the BSD terminal handling header file. */
struct sgttyb new, old; /* BSD term-info structure */
#else
	/* include the System V terminal handling header file. */
#include <termio.h>
struct termio new, old; /* SYSV term-info structure */
#define gtty(fd,arg)   (ioctl(fd, TCGETA, arg))
#define stty(fd,arg)   (ioctl(fd, TCSETA, arg))
#endif

main()
{
  int c;

  gtty(0,&old);
  gtty(0,&new);

#ifdef BSD /* set "raw" mode for BSD */
  new.sg_flags &= ~(ECHO|CRMOD);
  new.sg_flags |= RAW;
#else
  new.c_iflag &= ~(INLCR|ICRNL|IUCLC|ISTRIP|IXON|BRKINT);
  new.c_oflag &= ~OPOST;
  new.c_lflag &= ~(ICANON|ISIG|ECHO);
  new.c_cc[VMIN]  = 1;
  new.c_cc[VTIME] = 1;
#endif

  stty(0,&new);

  if (c=getchar()) {
    stty(0,&old);
    putchar(c);
    exit(0);
  }
}
---------------------------------------------------------------------------

Hope it helps,

Robert Felps                                felps@convex.com
Convex Computer Corp                        OS System Specialist
3000 Waterview Parkway                      Tech. Assistant Ctr
Richardson, Tx.  75083                      1(800) 952-0379
Technical Details
Message-ID: <felps.656044796@convex.convex.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!convex!convex.convex.com!felps
References: <24905@uflorida.cis.ufl.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!convex!convex.convex.com!felps
From: felps@convex.com (Robert Felps)
Newsgroups: comp.unix.programmer
Subject: Re: reading chars in raw mode with Ioctl .. How ?
Message-ID: <felps.656044796@convex.convex.com>
Date: 16 Oct 90 02:39:56 GMT
References: <24905@uflorida.cis.ufl.EDU>
Sender: news@convex.com
Distribution: na
Lines: 79