]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - include/all-io.h
dmesg: add --follow-new
[thirdparty/util-linux.git] / include / all-io.h
index 38a760ff6b42052e15e6c76ce764f74cbe02b095..8ffa9cfb110722d48493404b9035df3732c360e4 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * No copyright is claimed.  This code is in the public domain; do with
+ * it what you wish.
+ *
+ * Written by Karel Zak <kzak@redhat.com>
+ *            Petr Uzel <petr.uzel@suse.cz>
+ */
+
 #ifndef UTIL_LINUX_ALL_IO_H
 #define UTIL_LINUX_ALL_IO_H
 
@@ -5,6 +13,8 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "c.h"
+
 static inline int write_all(int fd, const void *buf, size_t count)
 {
        while (count) {
@@ -15,11 +25,11 @@ static inline int write_all(int fd, const void *buf, size_t count)
                if (tmp > 0) {
                        count -= tmp;
                        if (count)
-                               buf = (void *) ((char *) buf + tmp);
+                               buf = (const void *) ((const char *) buf + tmp);
                } else if (errno != EINTR && errno != EAGAIN)
                        return -1;
                if (errno == EAGAIN)    /* Try later, *sigh* */
-                       usleep(10000);
+                       xusleep(250000);
        }
        return 0;
 }
@@ -35,11 +45,11 @@ static inline int fwrite_all(const void *ptr, size_t size,
                if (tmp > 0) {
                        nmemb -= tmp;
                        if (nmemb)
-                               ptr = (void *) ((char *) ptr + (tmp * size));
+                               ptr = (const void *) ((const char *) ptr + (tmp * size));
                } else if (errno != EINTR && errno != EAGAIN)
                        return -1;
                if (errno == EAGAIN)    /* Try later, *sigh* */
-                       usleep(10000);
+                       xusleep(250000);
        }
        return 0;
 }
@@ -54,13 +64,13 @@ static inline ssize_t read_all(int fd, char *buf, size_t count)
        while (count > 0) {
                ret = read(fd, buf, count);
                if (ret <= 0) {
-                       if ((errno == EAGAIN || errno == EINTR || ret == 0) &&
-                           (tries++ < 5))
+                       if (ret < 0 && (errno == EAGAIN || errno == EINTR) && (tries++ < 5)) {
+                               xusleep(250000);
                                continue;
+                       }
                        return c ? c : -1;
                }
-               if (ret > 0)
-                       tries = 0;
+               tries = 0;
                count -= ret;
                buf += ret;
                c += ret;
@@ -68,5 +78,4 @@ static inline ssize_t read_all(int fd, char *buf, size_t count)
        return c;
 }
 
-
 #endif /* UTIL_LINUX_ALL_IO_H */