Solved

Unbuffered read in Solaris

Posted on 1998-07-23
6
542 Views
Last Modified: 2013-12-21
I cant seem to get the code below to read the getchar statement without buffering and waiting for a CR on Solaris 2.6. This has to be a common problem, for example, how would one read a function key pressed if one had to wait for a cr. I'm sure there is a simple solution.

BTW, curses can easily perform this, but is not thread safe as the normal terminal I/O calls are.

#include <stdio.h>

main()      
{
   int c;
   printf("Hit any character to continue\n");            

   /* This next call should return without a first typing a CR */
   c = getchar();            
   /* but it doesn't */

   printf("Thank you for typing %c.\n", c);
   exit(0);
}
0
Comment
Question by:AustinKalb
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:seedy
ID: 2009138
Simple solution use curses as shown below.  Or use termios - read man pages.

#include <curses.h>

main()
{
        initscr();
        cbreak();
        noecho();
        mvprintw(10, 10, "Hit any character to continue");
        refresh();
        mvprintw(11, 10, "Thank you for typing %c", getch());
        refresh();
        endwin();
}  

0
 

Author Comment

by:AustinKalb
ID: 2009139
I realize curses will work, but it is not thread safe, which I need. I am looging for the simple solution using termio. The termio man pages read like stereo instructions. How bout an example that will compile under solaris 2.6 using termio. Thanks.
0
 
LVL 2

Accepted Solution

by:
seedy earned 50 total points
ID: 2009140
Code Compiled under SunOS 5.x.  Should work under Solaris 2.6 too.
--- Code segment start ---
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

main()
{
        struct termios  tTermios, tTermioSav;
        int     ii;

        if ( (tcgetattr(STDOUT_FILENO, &tTermios) < 0) ||
             (tcgetattr(STDOUT_FILENO, &tTermioSav) < 0)  ) {
                perror("tcgetattr");
                exit(1);
        }

        tTermios.c_lflag  &= (!ICANON);/* Turn off canonical processing */
        tTermios.c_cc[VMIN] = 1;        /* Get at least a char  */
        tTermios.c_cc[VTIME] = 0;       /* block till a char is read */
        if (tcsetattr(STDOUT_FILENO, TCSANOW, &tTermios) < 0 ) {
                perror("tcsetattr");
        }
        printf("Hit any character to continue ");

        if ( (ii = fgetc(stdin)) == EOF ) {
                perror("fgetc");
        }

        printf("\nThank you for typing %c\n", ii);
        if (tcsetattr(STDOUT_FILENO, TCSANOW, &tTermioSav) < 0 ) {
                perror("tcsetattr");
        }
}
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:AustinKalb
ID: 2009141
Much better. Compiled fine. Worked great. Easy to follow. Thanks!
0
 

Author Comment

by:AustinKalb
ID: 2009142
On Solaris 2.6, I am using the code based on the above. I have wwritten a character based application for speeed. However, it seems that only some of the keys really get to the application. Others, like PageUp, make the terminal scroll. How do I get keys like PageUp and PageDown to the application.

Also, using the above code, after presumably getting the keyboard to function the way I want, I would like to capture keys like pageup and pagedown, while simultaniously still allowing text input on a command line. That is, I want to use the function keys to scroll, but have other input read, kinda like a word processor, but character based.

Thanks.
0
 
LVL 2

Expert Comment

by:seedy
ID: 2009143
> How do I get keys like PageUp and PageDown to the application
This largely depends on the terminal that you use and NOT on the application.  Make sure that these keys are readable by any other applications (shell or 'od -c', etc).
> simultaniously still allowing text input on a command line
What do you mean here?  Who will be "reading" the "command" line?

Also, most of these keys generate a multi character sequence.  For example on my Win 95 terminal application, the F1 key generates three characters: <ESC>OP

Though I would like to continue our conversation here, that will not be very appropriate.  Why don't you post a new question; this will also be seen by many experts, increasing your chance of getting a correct answer fast.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now