summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2011-04-12 16:38:20 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2011-12-15 10:20:09 +0100
commit4c41e245cfaf9f85af31c26394cf6549392f89f5 (patch)
tree1d3f389b5b9ac37669a90ada6ab1d2706412101e /lib
parent90d036d62cc8e2fb274d02c8d6b331d61bd5ba55 (diff)
downloadbarebox-4c41e245cfaf9f85af31c26394cf6549392f89f5.tar.gz
barebox-4c41e245cfaf9f85af31c26394cf6549392f89f5.tar.xz
libbb: add read_full/write_full functions
These functions read/write all data or return with an error. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/libbb.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/libbb.c b/lib/libbb.c
index 3d02202634..9a0a60bdb8 100644
--- a/lib/libbb.c
+++ b/lib/libbb.c
@@ -127,3 +127,53 @@ char *simple_itoa(unsigned int i)
return p + 1;
}
EXPORT_SYMBOL(simple_itoa);
+
+/*
+ * write_full - write to filedescriptor
+ *
+ * Like write, but guarantees to write the full buffer out, else
+ * it returns with an error.
+ */
+int write_full(int fd, void *buf, size_t size)
+{
+ size_t insize = size;
+ int now;
+
+ while (size) {
+ now = write(fd, buf, size);
+ if (now <= 0)
+ return now;
+ size -= now;
+ buf += now;
+ }
+
+ return insize;
+}
+EXPORT_SYMBOL(write_full);
+
+/*
+ * read_full - read from filedescriptor
+ *
+ * Like read, but this function only returns less bytes than
+ * requested when the end of file is reached.
+ */
+int read_full(int fd, void *buf, size_t size)
+{
+ size_t insize = size;
+ int now;
+ int total = 0;
+
+ while (size) {
+ now = read(fd, buf, size);
+ if (now == 0)
+ return total;
+ if (now < 0)
+ return now;
+ total += now;
+ size -= now;
+ buf += now;
+ }
+
+ return insize;
+}
+EXPORT_SYMBOL(read_full);