Solved

Unbuffered read in Solaris

Posted on 1998-07-23
6
560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

696 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