getdelim.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* getdelim.c --- Implementation of replacement getdelim function.
  2. Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005 Free
  3. Software 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 2, 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, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* Ported from glibc by Simon Josefsson. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include "getdelim.h"
  23. #if !HAVE_FLOCKFILE
  24. # undef flockfile
  25. # define flockfile(x) ((void) 0)
  26. #endif
  27. #if !HAVE_FUNLOCKFILE
  28. # undef funlockfile
  29. # define funlockfile(x) ((void) 0)
  30. #endif
  31. /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
  32. NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
  33. NULL), pointing to *N characters of space. It is realloc'ed as
  34. necessary. Returns the number of characters read (not including
  35. the null terminator), or -1 on error or EOF. */
  36. ssize_t
  37. getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
  38. {
  39. int result = 0;
  40. ssize_t cur_len = 0;
  41. ssize_t len;
  42. if (lineptr == NULL || n == NULL || fp == NULL)
  43. {
  44. errno = EINVAL;
  45. return -1;
  46. }
  47. flockfile (fp);
  48. if (*lineptr == NULL || *n == 0)
  49. {
  50. *n = 120;
  51. *lineptr = (char *) malloc (*n);
  52. if (*lineptr == NULL)
  53. {
  54. result = -1;
  55. goto unlock_return;
  56. }
  57. }
  58. for (;;)
  59. {
  60. char *t;
  61. int i;
  62. i = getc (fp);
  63. if (i == EOF)
  64. {
  65. result = -1;
  66. break;
  67. }
  68. /* Make enough space for len+1 (for final NUL) bytes. */
  69. if (cur_len + 1 >= *n)
  70. {
  71. size_t needed = 2 * (cur_len + 1) + 1; /* Be generous. */
  72. char *new_lineptr;
  73. if (needed < cur_len)
  74. {
  75. result = -1;
  76. goto unlock_return;
  77. }
  78. new_lineptr = (char *) realloc (*lineptr, needed);
  79. if (new_lineptr == NULL)
  80. {
  81. result = -1;
  82. goto unlock_return;
  83. }
  84. *lineptr = new_lineptr;
  85. *n = needed;
  86. }
  87. (*lineptr)[cur_len] = i;
  88. cur_len++;
  89. if (i == delimiter)
  90. break;
  91. }
  92. (*lineptr)[cur_len] = '\0';
  93. result = cur_len ? cur_len : result;
  94. unlock_return:
  95. funlockfile (fp);
  96. return result;
  97. }