Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unbuffered read in Solaris

Posted on 1998-07-23
6
Medium Priority
?
567 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

610 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