Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Daniel LaBell */
- /* /home/dlabell/src/C/getch.c */
- /* file-created: Tue Feb 3 02:00:37 1998 */
- #include <termios.h>
- #include <unistd.h>
- #include <stdio.h>
- int getch(void)
- {
- struct termios term_settings,term_settings_saved;
- int x;
- if ( tcgetattr(STDIN_FILENO,&term_settings))
- return -1;
- term_settings_saved=term_settings;
- term_settings.c_lflag &= ~ICANON ;
- term_settings.c_lflag &= ~ECHO ;
- term_settings.c_cc[VMIN]=1 ;
- term_settings.c_cc[VTIME]=0;
- if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
- return -1;
- x=getchar();
- tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
- return x;
- }
- int getche(void)
- {
- struct termios term_settings,term_settings_saved;
- int x;
- if ( tcgetattr(STDIN_FILENO,&term_settings))
- return -1;
- term_settings_saved=term_settings;
- term_settings.c_lflag &= ~ICANON ;
- term_settings.c_lflag |= ECHO ;
- term_settings.c_cc[VMIN]=1 ;
- term_settings.c_cc[VTIME]=0;
- if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
- return -1;
- x=getchar();
- tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
- return x;
- }
- /* getch pending or getch predicate if you like */
- /* returns -1 if no char is pending, -2 if error, or value of */
- int getchp(void)
- {
- struct termios term_settings,term_settings_saved;
- char c;
- int x;
- if ( tcgetattr(STDIN_FILENO,&term_settings))
- return -2;
- term_settings_saved=term_settings;
- term_settings.c_lflag &= ~ICANON ;
- term_settings.c_lflag &= ~ECHO ;
- term_settings.c_cc[VMIN]=0;
- term_settings.c_cc[VTIME]=0;
- if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
- return -2;
- switch(read(STDIN_FILENO,&c,1))
- {
- case 0: x=-1; break;
- case -1: x=-2; break;
- default: x=c;
- }
- tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
- return x;
- }
- int getchpe(void)
- {
- struct termios term_settings,term_settings_saved;
- char c;
- int x;
- if ( tcgetattr(STDIN_FILENO,&term_settings))
- return -2;
- term_settings_saved=term_settings;
- term_settings.c_lflag &= ~ICANON ;
- term_settings.c_lflag |= ECHO ;
- term_settings.c_cc[VMIN]=0;
- term_settings.c_cc[VTIME]=0;
- if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
- return -2;
- switch(read(STDIN_FILENO,&c,1))
- {
- case 0: x=-1; break;
- case -1: x=-2; break;
- default: x=c;
- }
- tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
- return x;
- }
- /* test prog */
- #include <stdlib.h>
- int pause_message(const char * message)
- {
- if ( message )
- puts(message);
- return getch();
- }
- int main(int arc, char * argv[])
- {
- int x;
- char c;
- if (!(isatty(STDIN_FILENO)))
- {
- fprintf(stderr, "Not a terminal.\n");
- exit(2);
- }
- system ("ls");
- pause_message( "\npress almost any key to continue");
- puts("enter an integer number ");
- scanf( "%i", &x);
- printf( "5 + your number = %i\n", (5+x));
- pause_message( "\npress almost any key to continue");
- puts("Enter a single char: ");
- c=getch();
- printf("\n you typed %c, which is %hi in ascii\n", c, (short)c);
- pause_message( "\npress almost any key to continue");
- while ( getchp() < 0 )
- printf("After thats comes %hi\n", (short)++c);
- return pause_message( "\npress almost any key to quit");
- }
Add Comment
Please, Sign In to add comment