getdelim.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. weak_alias(__getdelim,getdelim);
  18. #define GETDELIM_GROWBY 64
  19. ssize_t __getdelim(char **__restrict lineptr, size_t *__restrict n,
  20. int delimiter, register FILE *__restrict stream)
  21. {
  22. register char *buf;
  23. ssize_t pos = -1;
  24. int c;
  25. __STDIO_AUTO_THREADLOCK_VAR;
  26. if (!lineptr || !n || !stream) { /* Be compatable with glibc... even */
  27. __set_errno(EINVAL); /* though I think we should assert here */
  28. } else {
  29. __STDIO_AUTO_THREADLOCK(stream);
  30. if (!(buf = *lineptr)) { /* If passed NULL for buffer, */
  31. *n = 0; /* ignore value passed and treat size as 0. */
  32. }
  33. /* Within the loop, pos is actually the current buffer index + 2,
  34. * because we want to make sure we have enough space to store
  35. * an additional char plus a nul terminator.
  36. */
  37. pos = 1;
  38. do {
  39. if (pos >= *n) {
  40. if (!(buf = realloc(buf, *n + GETDELIM_GROWBY))) {
  41. pos = -1;
  42. break;
  43. }
  44. *n += GETDELIM_GROWBY;
  45. *lineptr = buf;
  46. }
  47. if ((c = __GETC_UNLOCKED(stream)) != EOF) {
  48. buf[++pos - 2] = c;
  49. if (c != delimiter) {
  50. continue;
  51. }
  52. }
  53. /* We're done, so correct pos back to being the current index. */
  54. if ((pos -= 2) >= 0) {
  55. buf[++pos] = 0;
  56. }
  57. break;
  58. } while (1);
  59. __STDIO_AUTO_THREADUNLOCK(stream);
  60. }
  61. return pos;
  62. }