summaryrefslogtreecommitdiff
path: root/tools/pkg/3/poll_test/src/main.c
blob: 4d6fdd22aa6a06a62b94f7ebb46f8571b4c3bddb (plain)
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
#include <stdio.h>
#include <poll.h>
#include <unistd.h>

#include <termios.h>

#include <string.h>

int main() {
    struct pollfd fds[1];
    fds[0].fd = 0; 
    fds[0].events = POLLIN; 

    struct termios newt;
    tcgetattr(STDIN_FILENO, &newt);
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt); 

    printf("Waiting for input (5 second timeout)...\n");
    int ret = poll(fds, 1, 5000); 

    if (ret == -1) {
        perror("poll");
        return 1;
    } else if (ret == 0) {
        printf("Timeout !\n");
    } else {
        if (fds[0].revents & POLLIN) {
            printf("Input ! waiting for string\n");
            char buf[100];
            read(STDIN_FILENO,buf,100);
            memset(buf,0,100);
            newt.c_lflag |= ICANON | ECHO;
            tcsetattr(STDIN_FILENO, TCSANOW, &newt); 
            read(STDIN_FILENO,buf,100);
            printf("You enter: %s", buf);
        }
    }

    newt.c_lflag |= ICANON | ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &newt); 

    return 0;
}