ref: e0db57f18648142b44f6a3b17b077c01ca39ff14
parent: cf00494038eb41f37ff22c13fbef80ef65fbf281
author: Jacob Moody <moody@posixcafe.org>
date: Sat Apr 5 19:59:38 EDT 2025
bio: add Bstropen
--- a/sys/include/bio.h
+++ b/sys/include/bio.h
@@ -1,6 +1,7 @@
#pragma src "/sys/src/libbio"
#pragma lib "libbio.a"
+typedef struct Bstr Bstr;
typedef struct Biobuf Biobuf;
typedef struct Biobufhdr Biobufhdr;
@@ -43,6 +44,12 @@
uchar b[Bungetsize+Bsize];
};
+struct Bstr
+{
+ Biobufhdr;
+ uchar b[];
+};
+
/* Dregs, redefined as functions for backwards compatibility */
#define BGETC(bp) Bgetc(bp)
#define BPUTC(bp,c) Bputc(bp,c)
@@ -62,6 +69,7 @@
vlong Boffset(Biobufhdr*);
Biobuf* Bopen(char*, int);
Biobuf* Bfdopen(int, int);
+Bstr* Bstropen(void*, int);
int Bprint(Biobufhdr*, char*, ...);
int Bvprint(Biobufhdr*, char*, va_list);
int Bputc(Biobufhdr*, int);
--- a/sys/man/2/bio
+++ b/sys/man/2/bio
@@ -1,6 +1,6 @@
.TH BIO 2
.SH NAME
-Bopen, Bfdopen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetrune, Bgetd, Bungetc, Bungetrune, Bread, Bseek, Boffset, Bfildes, Blinelen, Bputc, Bputrune, Bprint, Bvprint, Bwrite, Bflush, Bterm, Bbuffered, Blethal, Biofn \- buffered input/output
+Bopen, Bfdopen, Bstropen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetrune, Bgetd, Bungetc, Bungetrune, Bread, Bseek, Boffset, Bfildes, Blinelen, Bputc, Bputrune, Bprint, Bvprint, Bwrite, Bflush, Bterm, Bbuffered, Blethal, Biofn \- buffered input/output
.SH SYNOPSIS
.ta \w'Biobuf* 'u
.B #include <u.h>
@@ -26,6 +26,9 @@
Biobuf* Bfdopen(int fd, int mode)
.PP
.B
+Bstr* Bstropen(void *s, int len);
+.PP
+.B
int Binit(Biobuf *bp, int fd, int mode)
.PP
.B
@@ -123,6 +126,14 @@
.IR malloc (2)
to allocate a buffer.
.PP
+.I Bstropen
+creates a buffer from an existing static string.
+It calls
+.IR malloc (2)
+to allocate the buffer and prefills it with the contents
+specified within
+.IR s .
+.PP
.I Binit
initializes a standard size buffer, type
.IR Biobuf ,
@@ -168,9 +179,10 @@
.IR Bflush 's
return value.
If the buffer was allocated by
-.I Bopen
+.IR Bopen ,
+.I Bfdopen
or
-.IR Bfdopen ,
+.IR Bstropen ,
the buffer is
.I freed
and the file is closed.
--- a/sys/src/libbio/binit.c
+++ b/sys/src/libbio/binit.c
@@ -51,6 +51,12 @@
}
static int
+biodummy(Biobufhdr*, void*, long)
+{
+ return 0;
+}
+
+static int
bioread(Biobufhdr *bp, void *v, long n)
{
return read(bp->fid, v, n);
@@ -150,6 +156,23 @@
}
setmalloctag(bp, getcallerpc(&name));
return bp;
+}
+
+Bstr*
+Bstropen(void *p, int size)
+{
+ Bstr *b;
+
+ b = malloc(sizeof(Bstr) + Bungetsize + size);
+ if(b == nil)
+ return nil;
+ memmove(b->b + Bungetsize, p, size);
+ Binits(b, -1, OREAD, b->b, size + Bungetsize);
+ b->iof = biodummy;
+ b->icount = -size;
+ b->flag = Bmagic;
+ setmalloctag(b, getcallerpc(&p));
+ return b;
}
int
--
⑨