getdelim.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. /* Note: There is a defect in this function. (size_t vs ssize_t). */
  9. /* glibc function --
  10. * Return -1 if error or EOF prior to any chars read.
  11. * Return number of chars read (including possible delimiter but not
  12. * the terminating nul) otherwise.
  13. *
  14. * NOTE: If we need to allocate a buffer, we do so prior to attempting
  15. * a reading. So space may be allocated even if initially at EOF.
  16. */
  17. #define GETDELIM_GROWBY 64
  18. ssize_t attribute_hidden __libc_getdelim(char **__restrict lineptr, size_t *__restrict n,
  19. int delimiter, register FILE *__restrict stream)
  20. {
  21. register char *buf;
  22. ssize_t pos = -1;
  23. int c;
  24. __STDIO_AUTO_THREADLOCK_VAR;
  25. if (!lineptr || !n || !stream) { /* Be compatable with glibc... even */
  26. __set_errno(EINVAL); /* though I think we should assert here */
  27. } else {
  28. __STDIO_AUTO_THREADLOCK(stream);
  29. if (!(buf = *lineptr)) { /* If passed NULL for buffer, */
  30. *n = 0; /* ignore value passed and treat size as 0. */
  31. }
  32. /* Within the loop, pos is actually the current buffer index + 2,
  33. * because we want to make sure we have enough space to store
  34. * an additional char plus a nul terminator.
  35. */
  36. pos = 1;
  37. do {
  38. if (pos >= *n) {
  39. if (!(buf = realloc(buf, *n + GETDELIM_GROWBY))) {
  40. pos = -1;
  41. break;
  42. }
  43. *n += GETDELIM_GROWBY;
  44. *lineptr = buf;
  45. }
  46. if ((c = __GETC_UNLOCKED(stream)) != EOF) {
  47. buf[++pos - 2] = c;
  48. if (c != delimiter) {
  49. continue;
  50. }
  51. }
  52. /* We're done, so correct pos back to being the current index. */
  53. if ((pos -= 2) >= 0) {
  54. buf[++pos] = 0;
  55. }
  56. break;
  57. } while (1);
  58. __STDIO_AUTO_THREADUNLOCK(stream);
  59. }
  60. return pos;
  61. }
  62. strong_alias(__libc_getdelim,__getdelim)
  63. weak_alias(__getdelim,getdelim)