bio: add Bfdopen() from plan9port

This commit is contained in:
cinap_lenrek 2014-12-14 20:21:42 +01:00
parent 4afb56f570
commit 035aacf6f5
3 changed files with 44 additions and 11 deletions

View file

@ -59,6 +59,7 @@ int Binits(Biobufhdr*, int, int, uchar*, int);
int Blinelen(Biobufhdr*); int Blinelen(Biobufhdr*);
vlong Boffset(Biobufhdr*); vlong Boffset(Biobufhdr*);
Biobuf* Bopen(char*, int); Biobuf* Bopen(char*, int);
Biobuf* Bfdopen(int, int);
int Bprint(Biobufhdr*, char*, ...); int Bprint(Biobufhdr*, char*, ...);
int Bvprint(Biobufhdr*, char*, va_list); int Bvprint(Biobufhdr*, char*, va_list);
int Bputc(Biobufhdr*, int); int Bputc(Biobufhdr*, int);

View file

@ -1,6 +1,6 @@
.TH BIO 2 .TH BIO 2
.SH NAME .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, Bfdopen, 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
.SH SYNOPSIS .SH SYNOPSIS
.ta \w'Biobuf* 'u .ta \w'Biobuf* 'u
.B #include <u.h> .B #include <u.h>
@ -13,6 +13,9 @@ Bopen, Binit, Binits, Brdline, Brdstr, Bgetc, Bgetrune, Bgetd, Bungetc, Bungetru
Biobuf* Bopen(char *file, int mode) Biobuf* Bopen(char *file, int mode)
.PP .PP
.B .B
Biobuf* Bfdopen(int fd, int mode)
.PP
.B
int Binit(Biobuf *bp, int fd, int mode) int Binit(Biobuf *bp, int fd, int mode)
.PP .PP
.B .B
@ -96,6 +99,17 @@ It calls
.IR malloc (2) .IR malloc (2)
to allocate a buffer. to allocate a buffer.
.PP .PP
.I Bfdopen
allocates a buffer for the already-open file descriptor
.I fd
for mode
.B OREAD
or
.BR OWRITE .
It calls
.IR malloc (2)
to allocate a buffer.
.PP
.I Binit .I Binit
initializes a standard size buffer, type initializes a standard size buffer, type
.IR Biobuf , .IR Biobuf ,

View file

@ -53,7 +53,6 @@ install(Biobufhdr *bp)
int int
Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size) Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size)
{ {
p += Bungetsize; /* make room for Bungets */ p += Bungetsize; /* make room for Bungets */
size -= Bungetsize; size -= Bungetsize;
@ -94,29 +93,48 @@ Binit(Biobuf *bp, int f, int mode)
return Binits(bp, f, mode, bp->b, sizeof(bp->b)); return Binits(bp, f, mode, bp->b, sizeof(bp->b));
} }
Biobuf*
Bfdopen(int fd, int mode)
{
Biobuf *bp;
bp = malloc(sizeof(Biobuf));
if(bp == nil)
return nil;
if(Binits(bp, fd, mode, bp->b, sizeof(bp->b)) != 0){
free(bp);
return nil;
}
bp->flag = Bmagic; /* mark bp open & malloced */
setmalloctag(bp, getcallerpc(&fd));
return bp;
}
Biobuf* Biobuf*
Bopen(char *name, int mode) Bopen(char *name, int mode)
{ {
Biobuf *bp; Biobuf *bp;
int f; int fd;
switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) { switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
default: default:
fprint(2, "Bopen: unknown mode %#x\n", mode); fprint(2, "Bopen: unknown mode %#x\n", mode);
return 0; return nil;
case OREAD: case OREAD:
f = open(name, mode); fd = open(name, mode);
break; break;
case OWRITE: case OWRITE:
f = create(name, mode, 0666); fd = create(name, mode, 0666);
break; break;
} }
if(f < 0) if(fd < 0)
return 0; return nil;
bp = malloc(sizeof(Biobuf)); bp = Bfdopen(fd, mode);
if(bp == nil){
close(fd);
return nil;
}
setmalloctag(bp, getcallerpc(&name)); setmalloctag(bp, getcallerpc(&name));
Binits(bp, f, mode, bp->b, sizeof(bp->b));
bp->flag = Bmagic; /* mark bp open & malloced */
return bp; return bp;
} }