shiftdot515

getch.c

Apr 12th, 2020
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. /* Daniel LaBell */
  2. /* /home/dlabell/src/C/getch.c */
  3. /*  file-created: Tue Feb  3 02:00:37 1998 */
  4. #include <termios.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7.  
  8.  
  9. int getch(void)
  10. {
  11.   struct termios term_settings,term_settings_saved;
  12.   int x;
  13.   if ( tcgetattr(STDIN_FILENO,&term_settings))
  14.     return -1;
  15.   term_settings_saved=term_settings;
  16.   term_settings.c_lflag &= ~ICANON ;
  17.   term_settings.c_lflag &= ~ECHO ;
  18.   term_settings.c_cc[VMIN]=1 ;
  19.   term_settings.c_cc[VTIME]=0;
  20.   if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
  21.     return -1;
  22.   x=getchar();
  23.   tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  24.   return x;
  25. }
  26.  
  27. int getche(void)
  28. {
  29.   struct termios term_settings,term_settings_saved;
  30.   int x;
  31.   if ( tcgetattr(STDIN_FILENO,&term_settings))
  32.     return -1;
  33.   term_settings_saved=term_settings;
  34.   term_settings.c_lflag &= ~ICANON ;
  35.   term_settings.c_lflag |= ECHO ;
  36.   term_settings.c_cc[VMIN]=1 ;
  37.   term_settings.c_cc[VTIME]=0;
  38.   if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
  39.     return -1;
  40.   x=getchar();
  41.   tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  42.   return x;
  43. }
  44.  
  45. /* getch pending or getch predicate if you like */
  46. /* returns -1 if no char is pending, -2 if error, or value of  */
  47.  
  48. int getchp(void)
  49. {
  50.   struct termios term_settings,term_settings_saved;
  51.   char c;
  52.   int x;
  53.   if ( tcgetattr(STDIN_FILENO,&term_settings))
  54.     return -2;
  55.   term_settings_saved=term_settings;
  56.   term_settings.c_lflag &= ~ICANON ;
  57.   term_settings.c_lflag &= ~ECHO ;
  58.   term_settings.c_cc[VMIN]=0;
  59.   term_settings.c_cc[VTIME]=0;
  60.   if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
  61.     return -2;
  62.   switch(read(STDIN_FILENO,&c,1))
  63.     {
  64.     case 0: x=-1; break;
  65.     case -1: x=-2; break;
  66.     default:     x=c;
  67.     }
  68.   tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  69.   return x;
  70. }
  71.  
  72.  
  73.  
  74. int getchpe(void)
  75. {
  76.   struct termios term_settings,term_settings_saved;
  77.   char c;
  78.   int x;
  79.   if ( tcgetattr(STDIN_FILENO,&term_settings))
  80.     return -2;
  81.   term_settings_saved=term_settings;
  82.   term_settings.c_lflag &= ~ICANON ;
  83.   term_settings.c_lflag |= ECHO ;
  84.   term_settings.c_cc[VMIN]=0;
  85.   term_settings.c_cc[VTIME]=0;
  86.   if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
  87.     return -2;
  88.   switch(read(STDIN_FILENO,&c,1))
  89.     {
  90.     case 0: x=-1; break;
  91.     case -1: x=-2; break;
  92.     default:     x=c;
  93.     }
  94.   tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  95.   return x;
  96. }
  97.  
  98.  
  99. /* test prog */
  100. #include <stdlib.h>
  101.  
  102. int pause_message(const char * message)
  103. {
  104.   if ( message )
  105.     puts(message);
  106.   return getch();
  107. }
  108.  
  109. int main(int arc, char * argv[])
  110. {    
  111.   int x;
  112.   char c;
  113.   if (!(isatty(STDIN_FILENO)))
  114.     {
  115.       fprintf(stderr, "Not a terminal.\n");
  116.       exit(2);
  117.     }
  118.   system ("ls");
  119.   pause_message( "\npress almost any key to continue");
  120.   puts("enter an integer number ");
  121.   scanf( "%i", &x);
  122.   printf( "5 + your number = %i\n", (5+x));
  123.   pause_message( "\npress almost any key to continue");
  124.   puts("Enter a single char: ");
  125.   c=getch();
  126.   printf("\n you typed %c, which is %hi in ascii\n", c, (short)c);
  127.   pause_message( "\npress almost any key to continue");
  128.   while ( getchp() < 0 )
  129.     printf("After thats comes %hi\n", (short)++c);
  130.   return  pause_message( "\npress almost any key to quit");
  131. }
Add Comment
Please, Sign In to add comment