home *** CD-ROM | disk | FTP | other *** search
- From: rjl@hpuerca.atl.hp.com (Rich Lombardi)
- Date: Sun, 16 Aug 1992 17:22:06 GMT
- Subject: Re: How to do nobuffered IO on stdin in C on HP unix?
- Message-ID: <151270001@hpuerca.atl.hp.com>
- Organization: the HP Response Center, Atlanta
- Path: sparky!uunet!wupost!sdd.hp.com!hpscdc!hplextra!hpcc05!hpuerca!rjl
- Newsgroups: comp.sys.hp
- References: <1992Aug14.121655.18711@walter.cray.com>
- Lines: 96
-
- / hpuerca:comp.sys.hp / albrcp@cray.com (Albrecht Craig P) / 1:16 pm Aug 14, 1992 /
- >Does anybody know a way of doing nobuffered IO using C on a HP9000/7xx
- >workstation?
- >
- >I would like to do nobuffered IO on the "stdin" file. I want to be
- >able to process each character as the user types it in at the time
- >of input, NOT when they press the RETURN key.
- >
- >I heard that one can use "setvbuf", but I have no such luck in getting
- >to work for "stdin"
-
- Here's an old example that I've used for some time. There should be no problem
- compiling and running this on any HP-UX system.
-
- Note that this doesn't use C's FILE types but file descriptors instead. It
- operates on the STDIN file descriptor 0.
-
- I haven't played with setvbuf(3S) or tcattribute(3C) yet which explains why
- this still uses termio(7) ioctl's.
-
- Rich Lombardi
- rjl@hpuerca.atl.hp.com
-
- ---------------------------------cut here----------------------------------
-
- /* Program: raw_rs232.c
- *
- * Purpose: To read a single character at a time from stdin.
- *
- * rjl 2/20/87
- */
-
- /*
- ###############################################################
- Hewlett-Packard makes no warranty of any kind with regard to the
- accuracy or completeness of any information in this file, including,
- but not limited to, the implied warranties of merchantability and
- fitness for a particular purpose. Hewlett-Packard shall not be held
- liable for errors contained herein or direct, indirect, special,
- incidental or consequential damages in connection with the furnishing,
- performance, or use of this material.
- ###############################################################
- */
-
- #include <sys/ioctl.h>
- #include <sys/termio.h>
- #include <stdio.h>
- main()
- {
- struct termio orig, new;
- char buf[25];
-
- /* Get structure for stdin. First one is to restore back to original
- state, second one is to modify the driver. */
- if(ioctl(0,TCGETA,&orig)<0) {
- perror("ioctl TCGETA");
- exit(1);
- }
- if(ioctl(0,TCGETA,&new)<0) {
- perror("ioctl TCGETA");
- exit(1);
- }
-
- /* change the termio structure */
- new.c_lflag &= ~ICANON; /* turn off canonical processing */
- new.c_lflag &= ~ECHO; /* turn off echo */
- new.c_iflag &= ~IXON; /* turn off start/stop output cntrl */
- new.c_iflag &= ~IXOFF; /* turn off start/stop input cntrl */
- new.c_iflag &= ~IXANY; /* disable any character to restart */
- new.c_cc[VMIN] = 1; /* so we can read 1 char at a time */
-
- /* activate the new structure */
- if(ioctl(0,TCSETA,&new)<0) {
- perror("ioctl TCSETA");
- exit(1);
- }
-
- /* Read a character at a time until the letter 'q' is typed */
- while (1) {
- fprintf(stderr,"\nEnter a character ");
- read(0,buf,1);
- if(*buf == 'q') break;
-
- /* check to see if we received a DC1 or DC3 */
- /* Can be generated at the keyboard with <CNTRL>S and
- <CNTRL>q */
- else if(*buf == '\021') printf("DC1\n");
- else if(*buf == '\023') printf("DC3\n");
- else printf("\n character is %c\n",*buf);
- }
- /* restore the termio driver back to the original state we found it */
- if(ioctl(0,TCSETA,&orig)<0) {
- perror("ioctl TCSETA");
- exit(1);
- }
- }
-