File: termch.c

package info (click to toggle)
icmake 7.11-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,804 kB
  • ctags: 1,541
  • sloc: ansic: 7,755; makefile: 6,063; sh: 359; cpp: 83
file content (45 lines) | stat: -rw-r--r-- 870 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
    Enter a single char without \n on systems supporting termios.
    If things fail, fall back to `enterch()'
*/

#include <termios.h>
#include "builtin.ih"

int termch(void)
{
    struct termios saved;
    struct termios tattr;
    int key;

    if (tcgetattr(STDIN_FILENO, &saved))
    {
        fprintf(stderr, "Single key input failed. Press `Enter' as well\n");
        return enterch();
    }
    
    tcgetattr(STDIN_FILENO, &tattr);    /* can't assign saved to tattr */

    tattr.c_lflag &= ~(ICANON | ECHO);
    tattr.c_cc[VMIN] = 1;
    tattr.c_cc[VTIME] = 0;
    
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr))
    {
        fprintf(stderr, "Single key input failed. Press `Enter' as well\n");
        return enterch();
    }

    key = getchar();
    printf("%c\n", key);    

    tcsetattr(STDIN_FILENO, TCSANOW, &saved);

    return key;
}