getdelim.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* getdelim.c --- Implementation of replacement getdelim function.
  2. Copyright (C) 1994, 1996-1998, 2001, 2003, 2005-2021 Free Software
  3. Foundation, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 3, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  14. /* Ported from glibc by Simon Josefsson. */
  15. /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
  16. optimizes away the lineptr == NULL || n == NULL || fp == NULL tests below. */
  17. #define _GL_ARG_NONNULL(params)
  18. #include <config.h>
  19. #include <stdio.h>
  20. #include <limits.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #ifndef SSIZE_MAX
  25. # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
  26. #endif
  27. #if USE_UNLOCKED_IO
  28. # include "unlocked-io.h"
  29. # define getc_maybe_unlocked(fp) getc(fp)
  30. #elif !HAVE_FLOCKFILE || !HAVE_FUNLOCKFILE || !HAVE_DECL_GETC_UNLOCKED
  31. # undef flockfile
  32. # undef funlockfile
  33. # define flockfile(x) ((void) 0)
  34. # define funlockfile(x) ((void) 0)
  35. # define getc_maybe_unlocked(fp) getc(fp)
  36. #else
  37. # define getc_maybe_unlocked(fp) getc_unlocked(fp)
  38. #endif
  39. static void
  40. alloc_failed (void)
  41. {
  42. #if defined _WIN32 && ! defined __CYGWIN__
  43. /* Avoid errno problem without using the realloc module; see:
  44. https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html */
  45. errno = ENOMEM;
  46. #endif
  47. }
  48. /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
  49. NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
  50. NULL), pointing to *N characters of space. It is realloc'ed as
  51. necessary. Returns the number of characters read (not including
  52. the null terminator), or -1 on error or EOF. */
  53. ssize_t
  54. getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
  55. {
  56. ssize_t result;
  57. size_t cur_len = 0;
  58. if (lineptr == NULL || n == NULL || fp == NULL)
  59. {
  60. errno = EINVAL;
  61. return -1;
  62. }
  63. flockfile (fp);
  64. if (*lineptr == NULL || *n == 0)
  65. {
  66. char *new_lineptr;
  67. *n = 120;
  68. new_lineptr = (char *) realloc (*lineptr, *n);
  69. if (new_lineptr == NULL)
  70. {
  71. alloc_failed ();
  72. result = -1;
  73. goto unlock_return;
  74. }
  75. *lineptr = new_lineptr;
  76. }
  77. for (;;)
  78. {
  79. int i;
  80. i = getc_maybe_unlocked (fp);
  81. if (i == EOF)
  82. {
  83. result = -1;
  84. break;
  85. }
  86. /* Make enough space for len+1 (for final NUL) bytes. */
  87. if (cur_len + 1 >= *n)
  88. {
  89. size_t needed_max =
  90. SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
  91. size_t needed = 2 * *n + 1; /* Be generous. */
  92. char *new_lineptr;
  93. if (needed_max < needed)
  94. needed = needed_max;
  95. if (cur_len + 1 >= needed)
  96. {
  97. result = -1;
  98. errno = EOVERFLOW;
  99. goto unlock_return;
  100. }
  101. new_lineptr = (char *) realloc (*lineptr, needed);
  102. if (new_lineptr == NULL)
  103. {
  104. alloc_failed ();
  105. result = -1;
  106. goto unlock_return;
  107. }
  108. *lineptr = new_lineptr;
  109. *n = needed;
  110. }
  111. (*lineptr)[cur_len] = i;
  112. cur_len++;
  113. if (i == delimiter)
  114. break;
  115. }
  116. (*lineptr)[cur_len] = '\0';
  117. result = cur_len ? cur_len : result;
  118. unlock_return:
  119. funlockfile (fp); /* doesn't set errno */
  120. return result;
  121. }