summaryrefslogtreecommitdiff
path: root/sys/src/cmd/webfs/buf.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/webfs/buf.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/webfs/buf.c')
-rwxr-xr-xsys/src/cmd/webfs/buf.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/sys/src/cmd/webfs/buf.c b/sys/src/cmd/webfs/buf.c
new file mode 100755
index 000000000..ffd249407
--- /dev/null
+++ b/sys/src/cmd/webfs/buf.c
@@ -0,0 +1,89 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ip.h>
+#include <plumb.h>
+#include <thread.h>
+#include <fcall.h>
+#include <9p.h>
+#include "dat.h"
+#include "fns.h"
+
+void
+initibuf(Ibuf *b, Ioproc *io, int fd)
+{
+ b->fd = fd;
+ b->io = io;
+ b->rp = b->wp = b->buf;
+}
+
+int
+readibuf(Ibuf *b, char *buf, int len)
+{
+ int n;
+
+ n = b->wp - b->rp;
+ if(n > 0){
+ if(n > len)
+ n = len;
+ memmove(buf, b->rp, n);
+ b->rp += n;
+ return n;
+ }
+ return ioreadn(b->io, b->fd, buf, len);
+}
+
+void
+unreadline(Ibuf *b, char *line)
+{
+ int i, n;
+
+ i = strlen(line);
+ n = b->wp - b->rp;
+ memmove(&b->buf[i+1], b->rp, n);
+ memmove(b->buf, line, i);
+ b->buf[i] = '\n';
+ b->rp = b->buf;
+ b->wp = b->rp+i+1+n;
+}
+
+int
+readline(Ibuf *b, char *buf, int len)
+{
+ int n;
+ char *p;
+
+ len--;
+
+ for(p = buf;;){
+ if(b->rp >= b->wp){
+ n = ioread(b->io, b->fd, b->wp, sizeof(b->buf)/2);
+ if(n < 0)
+ return -1;
+ if(n == 0)
+ break;
+ b->wp += n;
+ }
+ n = *b->rp++;
+ if(len > 0){
+ *p++ = n;
+ len--;
+ }
+ if(n == '\n')
+ break;
+ }
+
+ /* drop trailing white */
+ for(;;){
+ if(p <= buf)
+ break;
+ n = *(p-1);
+ if(n != ' ' && n != '\t' && n != '\r' && n != '\n')
+ break;
+ p--;
+ }
+
+ *p = 0;
+ return p-buf;
+}
+