s_read.c 1015 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include "String.h"
  13. enum
  14. {
  15. Minread= 256,
  16. };
  17. /* Append up to 'len' input bytes to the string 'to'.
  18. *
  19. * Returns the number of characters read.
  20. */
  21. extern int
  22. s_read(Biobuf *fp, String *to, int len)
  23. {
  24. int rv;
  25. int n;
  26. if(to->ref > 1)
  27. sysfatal("can't s_read a shared string");
  28. for(rv = 0; rv < len; rv += n){
  29. n = to->end - to->ptr;
  30. if(n < Minread){
  31. s_grow(to, Minread);
  32. n = to->end - to->ptr;
  33. }
  34. if(n > len - rv)
  35. n = len - rv;
  36. n = Bread(fp, to->ptr, n);
  37. if(n <= 0)
  38. break;
  39. to->ptr += n;
  40. }
  41. s_terminate(to);
  42. return rv;
  43. }