Fix new glibc warnings about warn_unused_result.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 14 Nov 2008 06:12:46 +0000 (16:42 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 14 Nov 2008 06:12:46 +0000 (16:42 +1030)
ccan/antithread/antithread.c
ccan/read_write_all/_info.c [new file with mode: 0644]
ccan/read_write_all/read_write_all.c [new file with mode: 0644]
ccan/read_write_all/read_write_all.h [new file with mode: 0644]
ccan/tap/tap.c

index 9297cdac1d7f7b992e92e6284516df35778ff938..7382c59e3c402e04f3c419fe04bc1cdacaa3d8bb 100644 (file)
@@ -11,6 +11,7 @@
 #include "antithread.h"
 #include <ccan/noerr/noerr.h>
 #include <ccan/talloc/talloc.h>
+#include <ccan/read_write_all/read_write_all.h>
 #include <ccan/alloc/alloc.h>
 
 /* FIXME: Valgrind support should be possible for some cases.  Tricky
@@ -290,7 +291,7 @@ struct athread *at_spawn(struct at_pool *pool, void *arg, char *cmdline[])
                execvp(argv[0], argv);
 
                err = errno;
-               write(pool->parent_wfd, &err, sizeof(err));
+               write_all(pool->parent_wfd, &err, sizeof(err));
                exit(1);
        }
 
diff --git a/ccan/read_write_all/_info.c b/ccan/read_write_all/_info.c
new file mode 100644 (file)
index 0000000..32fccc8
--- /dev/null
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+/**
+ * read_write_all - read_all and write_all routines.
+ *
+ * Successful read and write calls may only partly complete if a
+ * signal is received or they are not operating on a normal file.
+ *
+ * read_all() and write_all() do the looping for you.
+ *
+ * Example:
+ *     #include <err.h>
+ *     #include <stdio.h>
+ *     #include <unistd.h>
+ *     #include <ccan/read_write_all/read_write_all.h>
+ *
+ *     #define BUFFER_SIZE 10
+ *     int main(int argc, char *argv[])
+ *     {
+ *             char buffer[BUFFER_SIZE+1];
+ *
+ *             if (!read_all(STDIN_FILENO, buffer, BUFFER_SIZE))
+ *                     err(1, "Could not read %u characters", BUFFER_SIZE);
+ *             buffer[BUFFER_SIZE] = '\0';
+ *             printf("I read '%.*s'\n", BUFFER_SIZE, buffer);
+ *             return 0;
+ *     }
+ *
+ * Licence: LGPL (2 or any later version)
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               return 0;
+       }
+
+       return 1;
+}
diff --git a/ccan/read_write_all/read_write_all.c b/ccan/read_write_all/read_write_all.c
new file mode 100644 (file)
index 0000000..406bd45
--- /dev/null
@@ -0,0 +1,38 @@
+#include "read_write_all.h"
+#include <unistd.h>
+#include <errno.h>
+
+bool write_all(int fd, const void *data, size_t size)
+{
+       while (size) {
+               ssize_t done;
+
+               done = write(fd, data, size);
+               if (done < 0 && errno == EINTR)
+                       continue;
+               if (done <= 0)
+                       return false;
+               data += done;
+               size -= done;
+       }
+
+       return true;
+}
+
+bool read_all(int fd, void *data, size_t size)
+{
+       while (size) {
+               ssize_t done;
+
+               done = read(fd, data, size);
+               if (done < 0 && errno == EINTR)
+                       continue;
+               if (done <= 0)
+                       return false;
+               data += done;
+               size -= done;
+       }
+
+       return true;
+}
+
diff --git a/ccan/read_write_all/read_write_all.h b/ccan/read_write_all/read_write_all.h
new file mode 100644 (file)
index 0000000..05f7985
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef _CCAN_READ_WRITE_H
+#define _CCAN_READ_WRITE_H
+#include <stddef.h>
+#include <stdbool.h>
+
+bool write_all(int fd, const void *data, size_t size);
+bool read_all(int fd, void *data, size_t size);
+
+#endif /* _CCAN_READ_WRITE_H */
index 6e268324446b7d1e33a7953005306b971ba1cf4b..6c454a5b976be628d55bb71abfbd169bc56e3a97 100644 (file)
@@ -105,7 +105,8 @@ _gen_result(int ok, const char *func, char *file, unsigned int line,
           expansions on it */
        if(test_name != NULL) {
                va_start(ap, test_name);
-               vasprintf(&local_test_name, test_name, ap);
+               if (vasprintf(&local_test_name, test_name, ap) < 0)
+                       local_test_name = NULL;
                va_end(ap);
 
                /* Make sure the test name contains more than digits
@@ -363,7 +364,8 @@ skip(unsigned int n, char *fmt, ...)
        LOCK;
 
        va_start(ap, fmt);
-       vasprintf(&skip_msg, fmt, ap);
+       if (vasprintf(&skip_msg, fmt, ap) < 0)
+               skip_msg = NULL;
        va_end(ap);
 
        while(n-- > 0) {
@@ -386,7 +388,8 @@ todo_start(char *fmt, ...)
        LOCK;
 
        va_start(ap, fmt);
-       vasprintf(&todo_msg, fmt, ap);
+       if (vasprintf(&todo_msg, fmt, ap) < 0)
+               todo_msg = NULL;
        va_end(ap);
 
        todo = 1;