getline.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * getline.c --- Based on...
  3. *
  4. * getdelim.c --- Implementation of replacement getdelim function.
  5. * Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005 Free
  6. * Software Foundation, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. /* Ported from glibc by Simon Josefsson. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #if !HAVE_GETLINE
  28. //#include "getdelim.h"
  29. #include <stdio.h>
  30. #include <limits.h>
  31. #include <stdlib.h>
  32. #include <errno.h>
  33. #ifndef SIZE_MAX
  34. #define SIZE_MAX ((size_t) -1)
  35. #endif
  36. #ifndef SSIZE_MAX
  37. #define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
  38. #endif
  39. #if !HAVE_FLOCKFILE
  40. #undef flockfile
  41. #define flockfile(x) ((void)0)
  42. #endif
  43. #if !HAVE_FUNLOCKFILE
  44. #undef funlockfile
  45. #define funlockfile(x) ((void)0)
  46. #endif
  47. /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
  48. NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
  49. NULL), pointing to *N characters of space. It is realloc'ed as
  50. necessary. Returns the number of characters read (not including
  51. the null terminator), or -1 on error or EOF. */
  52. ssize_t
  53. getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) {
  54. ssize_t result;
  55. size_t cur_len = 0;
  56. if (lineptr == NULL || n == NULL || fp == NULL) {
  57. errno = EINVAL;
  58. return -1;
  59. }
  60. flockfile (fp);
  61. if (*lineptr == NULL || *n == 0) {
  62. *n = 120;
  63. *lineptr = (char *) malloc(*n);
  64. if (*lineptr == NULL) {
  65. result = -1;
  66. goto unlock_return;
  67. }
  68. }
  69. for (;;) {
  70. int i;
  71. i = getc(fp);
  72. if (i == EOF) {
  73. result = -1;
  74. break;
  75. }
  76. /* Make enough space for len+1 (for final NUL) bytes. */
  77. if (cur_len + 1 >= *n) {
  78. size_t needed_max =
  79. SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
  80. size_t needed = 2 * *n + 1; /* Be generous. */
  81. char *new_lineptr;
  82. if (needed_max < needed)
  83. needed = needed_max;
  84. if (cur_len + 1 >= needed) {
  85. result = -1;
  86. goto unlock_return;
  87. }
  88. new_lineptr = (char *) realloc (*lineptr, needed);
  89. if (new_lineptr == NULL) {
  90. result = -1;
  91. goto unlock_return;
  92. }
  93. *lineptr = new_lineptr;
  94. *n = needed;
  95. }
  96. (*lineptr)[cur_len] = i;
  97. cur_len++;
  98. if (i == delimiter) break;
  99. }
  100. (*lineptr)[cur_len] = '\0';
  101. result = cur_len ? (int) cur_len : (int) result;
  102. unlock_return:
  103. funlockfile(fp);
  104. return result;
  105. }
  106. #endif