s_read.c 603 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "String.h"
  5. enum
  6. {
  7. Minread= 256,
  8. };
  9. /* Append up to 'len' input bytes to the string 'to'.
  10. *
  11. * Returns the number of characters read.
  12. */
  13. extern int
  14. s_read(Biobuf *fp, String *to, int len)
  15. {
  16. int rv;
  17. int n;
  18. if(to->ref > 1)
  19. sysfatal("can't s_read a shared string");
  20. for(rv = 0; rv < len; rv += n){
  21. n = to->end - to->ptr;
  22. if(n < Minread){
  23. s_grow(to, Minread);
  24. n = to->end - to->ptr;
  25. }
  26. if(n > len - rv)
  27. n = len - rv;
  28. n = Bread(fp, to->ptr, n);
  29. if(n <= 0)
  30. break;
  31. to->ptr += n;
  32. }
  33. s_terminate(to);
  34. return rv;
  35. }