bwrite.c 699 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. long
  5. Bwrite(Biobufhdr *bp, void *ap, long count)
  6. {
  7. long c;
  8. uchar *p;
  9. int i, n, oc;
  10. char errbuf[ERRMAX];
  11. p = ap;
  12. c = count;
  13. oc = bp->ocount;
  14. while(c > 0) {
  15. n = -oc;
  16. if(n > c)
  17. n = c;
  18. if(n == 0) {
  19. if(bp->state != Bwactive)
  20. return Beof;
  21. i = write(bp->fid, bp->bbuf, bp->bsize);
  22. if(i != bp->bsize) {
  23. errstr(errbuf, sizeof errbuf);
  24. if(strstr(errbuf, "interrupt") == nil)
  25. bp->state = Binactive;
  26. errstr(errbuf, sizeof errbuf);
  27. return Beof;
  28. }
  29. bp->offset += i;
  30. oc = -bp->bsize;
  31. continue;
  32. }
  33. memmove(bp->ebuf+oc, p, n);
  34. oc += n;
  35. c -= n;
  36. p += n;
  37. }
  38. bp->ocount = oc;
  39. return count-c;
  40. }