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. #define GETDELIM_GROWBY 64
  18. ssize_t attribute_hidden __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(__getdelim,getdelim)