bflush.c 891 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. int
  13. Bflush(Biobufhdr *bp)
  14. {
  15. int n, c;
  16. switch(bp->state) {
  17. case Bwactive:
  18. n = bp->bsize+bp->ocount;
  19. if(n == 0)
  20. return 0;
  21. c = write(bp->fid, bp->bbuf, n);
  22. if(n == c) {
  23. bp->offset += n;
  24. bp->ocount = -bp->bsize;
  25. return 0;
  26. }
  27. bp->state = Binactive;
  28. bp->ocount = 0;
  29. break;
  30. case Bracteof:
  31. bp->state = Bractive;
  32. case Bractive:
  33. bp->icount = 0;
  34. bp->gbuf = bp->ebuf;
  35. return 0;
  36. }
  37. return Beof;
  38. }