Why does empty printf allow me to read data from 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); } 

Ok, so this piece of code handles redrawing text on the screen. Some lines of text are underlined or highlighted using termcaps (tputs(tgetstr("us, NULL")......) . Everything prints fine, but after the first redraw of the text, it seems to freeze if dprintf/printf present The key_hook function simply reads 4 bytes from stdin and converts them to int .

+5
source share
1 answer

When I last worked here, my version of key_hook had a single-byte read loop. This was violated by a 1 second alarm and the logic of whether the data was still a key prefix.

An alarm interrupted reading and stopped the freeze

+1
source

Source: https://habr.com/ru/post/1240593/


All Articles