bread.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. Bread(Biobufhdr *bp, void *ap, int32_t count)
  14. {
  15. int32_t c;
  16. uint8_t *p;
  17. int i, n, ic;
  18. p = ap;
  19. c = count;
  20. ic = bp->icount;
  21. while(c > 0) {
  22. n = -ic;
  23. if(n > c)
  24. n = c;
  25. if(n == 0) {
  26. if(bp->state != Bractive)
  27. break;
  28. i = read(bp->fid, bp->bbuf, bp->bsize);
  29. if(i <= 0) {
  30. bp->state = Bracteof;
  31. if(i < 0)
  32. bp->state = Binactive;
  33. break;
  34. }
  35. bp->gbuf = bp->bbuf;
  36. bp->offset += i;
  37. if(i < bp->bsize) {
  38. memmove(bp->ebuf-i, bp->bbuf, i);
  39. bp->gbuf = bp->ebuf-i;
  40. }
  41. ic = -i;
  42. continue;
  43. }
  44. memmove(p, bp->ebuf+ic, n);
  45. c -= n;
  46. ic += n;
  47. p += n;
  48. }
  49. bp->icount = ic;
  50. if(count == c && bp->state == Binactive)
  51. return -1;
  52. return count-c;
  53. }