Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an empty printf allow me to continue reading data from the stdin?

CODE

while (1)
    {
        keycode = key_hook();
        if (keycode == SPACE || keycode == BKSPACE)
        {
            render_again = 1;
        }
        if (keycode == ESC)
            break;
        if (render_again)
        {
            render_again = 0;
            render(all);
        }
        dprintf(1, "");      //I have no idea why this prevents the program from freezing
    }
    int key_hook()
    {
     char buffer[4];

     read(0, buffer, 4);
     return (*(unsigned int *)buffer);
    }

Alright, so this piece of code handles redrawing of text on screen. Some rows of text are underlined or highlighted using termcaps (tputs(tgetstr("us, NULL")......). Everything prints fine but after the first redraw of the text the while apparently freezes unless a dprintf/printf is present. The key_hook function just reads 4 bytes from the stdin and converts them to an int.

like image 865
Rpreda Avatar asked Nov 09 '22 21:11

Rpreda


1 Answers

When I last did work here, my version of key_hook had a loop of single byte reads. This was broken by an alarm of 1 second and logic to whether the data so far was a key prefix.

The alarm interrupted the read, and stopped freeze

like image 65
mksteve Avatar answered Nov 14 '22 22:11

mksteve