s_read_line.c 988 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. /* Append an input line to a String.
  14. *
  15. * Returns a pointer to the character string (or 0).
  16. * Trailing newline is left on.
  17. */
  18. extern char *
  19. s_read_line(Biobuf *fp, String *to)
  20. {
  21. char *cp;
  22. int llen;
  23. if(to->ref > 1)
  24. sysfatal("can't s_read_line a shared string");
  25. s_terminate(to);
  26. cp = Brdline(fp, '\n');
  27. if(cp == 0)
  28. return 0;
  29. llen = Blinelen(fp);
  30. if(to->end - to->ptr < llen)
  31. s_grow(to, llen);
  32. memmove(to->ptr, cp, llen);
  33. cp = to->ptr;
  34. to->ptr += llen;
  35. s_terminate(to);
  36. return cp;
  37. }