]> git.refcnt.org Git - colorize.git/commitdiff
fifo: use nonblocking mode nonblocking
authorSteven Schubiger <stsc@refcnt.org>
Mon, 4 Nov 2024 21:01:58 +0000 (22:01 +0100)
committerSteven Schubiger <stsc@refcnt.org>
Mon, 4 Nov 2024 21:01:58 +0000 (22:01 +0100)
colorize.c

index da03804a57bb57b87fe15f831f394b9870ef301f..83d4c34b9cbe9ec7a5d545c405febe96e7cd7b69 100644 (file)
@@ -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));