]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/libbio/binit.c
ether82563: support for i211 with iNVM. (thanks mfny and brennan for testing)
[plan9front.git] / sys / src / libbio / binit.c
index ef705e667f15f9cb85cc2961652148d7836dadeb..13fd0f4ceb84916ec0f8ecd3ff32c45bd3a316c0 100644 (file)
@@ -50,10 +50,21 @@ install(Biobufhdr *bp)
        }
 }
 
+static int
+bioread(Biobufhdr *bp, void *v, long n)
+{
+       return read(bp->fid, v, n);
+}
+
+static int
+biowrite(Biobufhdr *bp, void *v, long n)
+{
+       return write(bp->fid, v, n);
+}
+
 int
 Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size)
 {
-
        p += Bungetsize;        /* make room for Bungets */
        size -= Bungetsize;
 
@@ -65,12 +76,14 @@ Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size)
        case OREAD:
                bp->state = Bractive;
                bp->ocount = 0;
+               bp->iof = bioread;
                break;
 
        case OWRITE:
                install(bp);
                bp->state = Bwactive;
                bp->ocount = -size;
+               bp->iof = biowrite;
                break;
        }
        bp->bbuf = p;
@@ -94,28 +107,48 @@ Binit(Biobuf *bp, int f, int mode)
        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*
 Bopen(char *name, int mode)
 {
        Biobuf *bp;
-       int f;
+       int fd;
 
        switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
        default:
                fprint(2, "Bopen: unknown mode %#x\n", mode);
-               return 0;
+               return nil;
        case OREAD:
-               f = open(name, mode);
+               fd = open(name, mode);
                break;
        case OWRITE:
-               f = create(name, mode, 0666);
+               fd = create(name, mode, 0666);
                break;
        }
-       if(f < 0)
-               return 0;
-       bp = malloc(sizeof(Biobuf));
-       Binits(bp, f, mode, bp->b, sizeof(bp->b));
-       bp->flag = Bmagic;                      /* mark bp open & malloced */
+       if(fd < 0)
+               return nil;
+       bp = Bfdopen(fd, mode);
+       if(bp == nil){
+               close(fd);
+               return nil;
+       }
+       setmalloctag(bp, getcallerpc(&name));
        return bp;
 }
 
@@ -135,3 +168,15 @@ Bterm(Biobufhdr *bp)
        /* otherwise opened with Binit(s) */
        return r;
 }
+
+void
+Biofn(Biobufhdr *bp, int (*f)(Biobufhdr *, void *, long))
+{
+       if(f == nil)
+               if(bp->state == Bwactive)
+                       bp->iof = biowrite;
+               else
+                       bp->iof = bioread;
+       else
+               bp->iof = f;
+}