s_read_line.c 576 B

12345678910111213141516171819202122232425262728293031
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "String.h"
  5. /* Append an input line to a String.
  6. *
  7. * Returns a pointer to the character string (or 0).
  8. * Trailing newline is left on.
  9. */
  10. extern char *
  11. s_read_line(Biobuf *fp, String *to)
  12. {
  13. char *cp;
  14. int llen;
  15. if(to->ref > 1)
  16. sysfatal("can't s_read_line a shared string");
  17. s_terminate(to);
  18. cp = Brdline(fp, '\n');
  19. if(cp == 0)
  20. return 0;
  21. llen = Blinelen(fp);
  22. if(to->end - to->ptr < llen)
  23. s_grow(to, llen);
  24. memmove(to->ptr, cp, llen);
  25. cp = to->ptr;
  26. to->ptr += llen;
  27. s_terminate(to);
  28. return cp;
  29. }