~aritra1911/ansi_anim

34548c0035f43d8aa425ab1f15018b5005af3583 — Aritra Sarkar 2 years ago 2a4b390
Set timeout for read()
1 files changed, 23 insertions(+), 2 deletions(-)

M main.c
M main.c => main.c +23 -2
@@ 1,3 1,4 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>


@@ 38,6 39,10 @@ static inline void enable_raw_mode(void)
     * This isn't always necessary */
    raw.c_cflag |= (CS8);

    /* Set timeout for read() */
    raw.c_cc[VMIN] = 0;     /* read() returns as soon as there's input */
    raw.c_cc[VTIME] = 1;    /* read() times out after 100 milliseconds */

    tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}



@@ 52,8 57,24 @@ int main(void)

    enable_raw_mode();

    /* keep reading until EOF or 'q' */
    while ( read(STDIN_FILENO, &c, 1) && c != 'q' );
    while ( 1 ) {

        /* Read a byte from the standard input */
        while ( read(STDIN_FILENO, &c, 1) < 1 );

        if ( iscntrl(c) ) {
            /* Control characters are non-printable,
             * hence don't even try to print them */
            printf("%d\r\n", c);

        } else {
            /* print character ascii values and
             * show the characters as well */
            printf("%d ('%c')\r\n", c, c);

            if ( c == 'q' ) break;
        }
    }

    return EXIT_SUCCESS;
}