git: 9front

Download patch

ref: 383ce0d408cdb9e9a601d0265957a0e8394e9571
parent: 3ced091502a4099065d7679f889da754e57dc4b6
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Wed Oct 5 20:39:05 EDT 2011

libbio: add Breadn

--- a/sys/include/bio.h
+++ b/sys/include/bio.h
@@ -66,6 +66,7 @@
 void*	Brdline(Biobufhdr*, int);
 char*	Brdstr(Biobufhdr*, int, int);
 long	Bread(Biobufhdr*, void*, long);
+long	Breadn(Biobufhdr*, void*, long);
 vlong	Bseek(Biobufhdr*, vlong, int);
 int	Bterm(Biobufhdr*);
 int	Bungetc(Biobufhdr*);
--- a/sys/man/2/bio
+++ b/sys/man/2/bio
@@ -1,6 +1,6 @@
 .TH BIO 2
 .SH NAME
-Bopen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetrune, Bgetd, Bungetc, Bungetrune, Bread, Bseek, Boffset, Bfildes, Blinelen, Bputc, Bputrune, Bprint, Bvprint, Bwrite, Bflush, Bterm, Bbuffered, Blethal \- buffered input/output
+Bopen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetrune, Bgetd, Bungetc, Bungetrune, Bread, Breadn, Bseek, Boffset, Bfildes, Blinelen, Bputc, Bputrune, Bprint, Bvprint, Bwrite, Bflush, Bterm, Bbuffered, Blethal \- buffered input/output
 .SH SYNOPSIS
 .ta \w'Biobuf* 'u
 .B #include <u.h>
@@ -70,6 +70,9 @@
 long	Bread(Biobufhdr *bp, void *addr, long nbytes)
 .PP
 .B
+long	Breadn(Biobufhdr *bp, void *addr, long nbytes)
+.PP
+.B
 long	Bwrite(Biobufhdr *bp, void *addr, long nbytes)
 .PP
 .B
@@ -235,6 +238,13 @@
 .IR addr .
 The number of bytes read is returned on success
 and a negative value is returned if a read error occurred.
+.PP
+.I Breadn
+is like
+.I Bread
+but continues reading until
+.I nbytes
+have been read into the buffer.
 .PP
 .I Bseek
 applies
--- /dev/null
+++ b/sys/src/libbio/breadn.c
@@ -1,0 +1,26 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+
+long
+Breadn(Biobufhdr *bp, void *data, long len)
+{
+	char *e, *p;
+	int n;
+
+	p = data;
+	e = p + len;
+	if(e < p){
+		Berror(bp, "invalid read length");
+		return -1;
+	}
+	while(p < e){
+		if((n = Bread(bp, p, e - p)) <= 0){
+			if(n < 0 && p == data)
+				return -1;
+			break;
+		}
+		p += n;
+	}
+	return p - (char*)data;
+}
--- a/sys/src/libbio/mkfile
+++ b/sys/src/libbio/mkfile
@@ -17,6 +17,7 @@
 	brdline.$O\
 	brdstr.$O\
 	bread.$O\
+	breadn.$O\
 	bseek.$O\
 	bwrite.$O\
 	bvprint.$O\
--