iogetdelim.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (C) 1994-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include <stdlib.h>
  23. #include "libioP.h"
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <limits.h>
  27. /* Read up to (and including) a TERMINATOR from FP into *LINEPTR
  28. (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
  29. NULL), pointing to *N characters of space. It is realloc'ed as
  30. necessary. Returns the number of characters read (not including the
  31. null terminator), or -1 on error or EOF. */
  32. ssize_t
  33. _IO_getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
  34. {
  35. ssize_t result;
  36. ssize_t cur_len = 0;
  37. ssize_t len;
  38. if (lineptr == NULL || n == NULL)
  39. {
  40. __set_errno (EINVAL);
  41. return -1;
  42. }
  43. CHECK_FILE (fp, -1);
  44. _IO_acquire_lock (fp);
  45. if (_IO_ferror_unlocked (fp))
  46. {
  47. result = -1;
  48. goto unlock_return;
  49. }
  50. if (*lineptr == NULL || *n == 0)
  51. {
  52. *n = 120;
  53. *lineptr = (char *) malloc (*n);
  54. if (*lineptr == NULL)
  55. {
  56. result = -1;
  57. goto unlock_return;
  58. }
  59. }
  60. len = fp->_IO_read_end - fp->_IO_read_ptr;
  61. if (len <= 0)
  62. {
  63. if (__underflow (fp) == EOF)
  64. {
  65. result = -1;
  66. goto unlock_return;
  67. }
  68. len = fp->_IO_read_end - fp->_IO_read_ptr;
  69. }
  70. for (;;)
  71. {
  72. size_t needed;
  73. char *t;
  74. t = (char *) memchr ((void *) fp->_IO_read_ptr, delimiter, len);
  75. if (t != NULL)
  76. len = (t - fp->_IO_read_ptr) + 1;
  77. if (__glibc_unlikely (len >= SSIZE_MAX - cur_len))
  78. {
  79. __set_errno (EOVERFLOW);
  80. result = -1;
  81. goto unlock_return;
  82. }
  83. /* Make enough space for len+1 (for final NUL) bytes. */
  84. needed = cur_len + len + 1;
  85. if (needed > *n)
  86. {
  87. char *new_lineptr;
  88. if (needed < 2 * *n)
  89. needed = 2 * *n; /* Be generous. */
  90. new_lineptr = (char *) realloc (*lineptr, needed);
  91. if (new_lineptr == NULL)
  92. {
  93. result = -1;
  94. goto unlock_return;
  95. }
  96. *lineptr = new_lineptr;
  97. *n = needed;
  98. }
  99. memcpy (*lineptr + cur_len, (void *) fp->_IO_read_ptr, len);
  100. fp->_IO_read_ptr += len;
  101. cur_len += len;
  102. if (t != NULL || __underflow (fp) == EOF)
  103. break;
  104. len = fp->_IO_read_end - fp->_IO_read_ptr;
  105. }
  106. (*lineptr)[cur_len] = '\0';
  107. result = cur_len;
  108. unlock_return:
  109. _IO_release_lock (fp);
  110. return result;
  111. }
  112. weak_alias (_IO_getdelim, __getdelim)
  113. weak_alias (_IO_getdelim, getdelim)