From a3f5f6414b4bcb75d719ad4cd40a85bef43c3fc2 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Mon, 4 Jul 2022 12:00:24 +0200 Subject: [PATCH] Cleanup --- src/tty.c | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/tty.c b/src/tty.c index 6886c7b..f1efa69 100644 --- a/src/tty.c +++ b/src/tty.c @@ -79,8 +79,8 @@ static bool map_o_del_bs = false; static char hex_chars[2]; static unsigned char hex_char_index = 0; static char tty_buffer[BUFSIZ*2]; -static size_t tty_count = 0; -static char *tty_write_ptr = tty_buffer; +static size_t tty_buffer_count = 0; +static char *tty_buffer_write_ptr = tty_buffer; static void optional_local_echo(char c) { @@ -122,11 +122,24 @@ inline static unsigned char char_to_nibble(char c) void tty_flush(int fd) { - write(fd, tty_buffer, tty_count); + ssize_t count; + + do + { + count = write(fd, tty_buffer, tty_buffer_count); + if (count < 0) + { + // Error + debug_printf("Write error while flushing tty buffer (%s)", strerror(errno)); + break; + } + tty_buffer_count -= count; + } + while (tty_buffer_count > 0); // Reset - tty_write_ptr = tty_buffer; - tty_count = 0; + tty_buffer_write_ptr = tty_buffer; + tty_buffer_count = 0; } ssize_t tty_write(int fd, const void *buffer, size_t count) @@ -138,27 +151,30 @@ ssize_t tty_write(int fd, const void *buffer, size_t count) // Write byte by byte with output delay for (size_t i=0; i BUFSIZ) + if ((tty_buffer_count + count) > BUFSIZ) { tty_flush(fd); } - // Copy bytes to tty buffer - memcpy(tty_write_ptr, buffer, count); - tty_write_ptr += count; - tty_count += count; + // Copy bytes to tty write buffer + memcpy(tty_buffer_write_ptr, buffer, count); + tty_buffer_write_ptr += count; + tty_buffer_count += count; bytes_written = count; }