From 8e8113fe21a241caf498ba2571656f3efc149fba Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Mon, 4 Nov 2024 22:01:58 +0100 Subject: fifo: use nonblocking mode --- colorize.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/colorize.c b/colorize.c index da03804..83d4c34 100644 --- a/colorize.c +++ b/colorize.c @@ -1357,12 +1357,13 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi const char *line; bool sleep = false; errno = 0; + clearerr (stream); bytes_read = fread (buf, 1, BUF_SIZE, stream); if (bytes_read != BUF_SIZE) { if (errno == EAGAIN || errno == EWOULDBLOCK) sleep = true; - else if (fileno (stream) != STDIN_FILENO && ferror (stream)) + else if (ferror (stream)) vfprintf_fail (formats[FMT_ERROR], BUF_SIZE, "read"); } buf[bytes_read] = '\0'; @@ -2042,9 +2043,24 @@ static FILE * open_file (const char *file, const char *mode) { FILE *stream; + int fd, flags = O_NONBLOCK; + mode_t perms = 0; + + if (streq (mode, "r")) + flags |= O_RDONLY; + else if (streq (mode, "w")) + { + flags |= O_WRONLY | O_CREAT | O_TRUNC; + perms = 0644; + } + + errno = 0; + fd = open (file, flags, perms); + if (fd == -1) + vfprintf_fail (formats[FMT_FILE], file, strerror (errno)); errno = 0; - stream = fopen (file, mode); + stream = fdopen (fd, mode); if (!stream) vfprintf_fail (formats[FMT_FILE], file, strerror (errno)); -- cgit v1.2.3