bwrite.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. int32_t
  13. Bwrite(Biobufhdr *bp, void *ap, int32_t count)
  14. {
  15. int32_t c;
  16. uint8_t *p;
  17. int i, n, oc;
  18. char errbuf[ERRMAX];
  19. p = ap;
  20. c = count;
  21. oc = bp->ocount;
  22. while(c > 0) {
  23. n = -oc;
  24. if(n > c)
  25. n = c;
  26. if(n == 0) {
  27. if(bp->state != Bwactive)
  28. return Beof;
  29. i = write(bp->fid, bp->bbuf, bp->bsize);
  30. if(i != bp->bsize) {
  31. errstr(errbuf, sizeof errbuf);
  32. if(strstr(errbuf, "interrupt") == nil)
  33. bp->state = Binactive;
  34. errstr(errbuf, sizeof errbuf);
  35. return Beof;
  36. }
  37. bp->offset += i;
  38. oc = -bp->bsize;
  39. continue;
  40. }
  41. memmove(bp->ebuf+oc, p, n);
  42. oc += n;
  43. c -= n;
  44. p += n;
  45. }
  46. bp->ocount = oc;
  47. return count-c;
  48. }