Solved

Unbuffered read in Solaris

Posted on 1998-07-23
6
545 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

895 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

13 Experts available now in Live!

Get 1:1 Help Now