getline.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* getline.c -- Replacement for GNU C library function getline
  2. Copyright (C) 1993, 1996, 2001, 2002 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
  15. #include <assert.h>
  16. #include <stdio.h>
  17. /* Always add at least this many bytes when extending the buffer. */
  18. #define MIN_CHUNK 64
  19. /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
  20. + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
  21. malloc (or NULL), pointing to *N characters of space. It is realloc'd
  22. as necessary. Return the number of characters read (not including the
  23. null terminator), or -1 on error or EOF.
  24. NOTE: There is another getstr() function declared in <curses.h>. */
  25. static int getstr(char **lineptr, size_t *n, FILE *stream,
  26. char terminator, size_t offset)
  27. {
  28. int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
  29. char *read_pos; /* Where we're reading into *LINEPTR. */
  30. int ret;
  31. if (!lineptr || !n || !stream)
  32. return -1;
  33. if (!*lineptr) {
  34. *n = MIN_CHUNK;
  35. *lineptr = malloc(*n);
  36. if (!*lineptr)
  37. return -1;
  38. }
  39. nchars_avail = *n - offset;
  40. read_pos = *lineptr + offset;
  41. for (;;) {
  42. register int c = getc(stream);
  43. /* We always want at least one char left in the buffer, since we
  44. always (unless we get an error while reading the first char)
  45. NUL-terminate the line buffer. */
  46. assert(*n - nchars_avail == read_pos - *lineptr);
  47. if (nchars_avail < 2) {
  48. if (*n > MIN_CHUNK)
  49. *n *= 2;
  50. else
  51. *n += MIN_CHUNK;
  52. nchars_avail = *n + *lineptr - read_pos;
  53. *lineptr = realloc(*lineptr, *n);
  54. if (!*lineptr)
  55. return -1;
  56. read_pos = *n - nchars_avail + *lineptr;
  57. assert(*n - nchars_avail == read_pos - *lineptr);
  58. }
  59. if (c == EOF || ferror (stream)) {
  60. /* Return partial line, if any. */
  61. if (read_pos == *lineptr)
  62. return -1;
  63. else
  64. break;
  65. }
  66. *read_pos++ = c;
  67. nchars_avail--;
  68. if (c == terminator)
  69. /* Return the line. */
  70. break;
  71. }
  72. /* Done - NUL terminate and return the number of chars read. */
  73. *read_pos = '\0';
  74. ret = read_pos - (*lineptr + offset);
  75. return ret;
  76. }
  77. int getline (char **lineptr, size_t *n, FILE *stream)
  78. {
  79. return getstr(lineptr, n, stream, '\n', 0);
  80. }