aboutsummaryrefslogtreecommitdiffstats
path: root/colorize.c
diff options
context:
space:
mode:
authorGravatar Steven Schubiger <stsc@refcnt.org> 2024-11-04 22:01:58 +0100
committerGravatar Steven Schubiger <stsc@refcnt.org> 2024-11-04 22:01:58 +0100
commit8e8113fe21a241caf498ba2571656f3efc149fba (patch)
tree5f1cd5c9febd4733c8ad733a0cf9b728b30297f1 /colorize.c
parente38e8f99ce735798c2674451664a700cc04e2577 (diff)
downloadcolorize-nonblocking.tar.gz
colorize-nonblocking.tar.bz2
fifo: use nonblocking modenonblocking
Diffstat (limited to 'colorize.c')
-rw-r--r--colorize.c20
1 files 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));