Advertisement
Guest User

Untitled

a guest
Jan 17th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. /* getline.c -- Replacement for GNU C library function getline
  2.  
  3. Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  18.  
  19. /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
  20.  
  21. #include <sys/types.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include <stdlib.h>
  25.  
  26. /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
  27. + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
  28. malloc (or NULL), pointing to *N characters of space. It is realloc'd
  29. as necessary. Return the number of characters read (not including the
  30. null terminator), or -1 on error or EOF. */
  31.  
  32. int getstr (char ** lineptr, size_t *n, FILE * stream, char terminator, int offset)
  33. {
  34. int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
  35. char *read_pos; /* Where we're reading into *LINEPTR. */
  36. int ret;
  37.  
  38. if (!lineptr || !n || !stream)
  39. return -1;
  40.  
  41. if (!*lineptr)
  42. {
  43. *n = 64;
  44. *lineptr = (char *) malloc (*n);
  45. if (!*lineptr)
  46. return -1;
  47. }
  48.  
  49. nchars_avail = *n - offset;
  50. read_pos = *lineptr + offset;
  51.  
  52. for (;;)
  53. {
  54. register int c = getc (stream);
  55.  
  56. /* We always want at least one char left in the buffer, since we
  57. always (unless we get an error while reading the first char)
  58. NUL-terminate the line buffer. */
  59.  
  60. assert(*n - nchars_avail == read_pos - *lineptr);
  61. if (nchars_avail < 1)
  62. {
  63. if (*n > 64)
  64. *n *= 2;
  65. else
  66. *n += 64;
  67.  
  68. nchars_avail = *n + *lineptr - read_pos;
  69. *lineptr = (char *) realloc (*lineptr, *n);
  70. if (!*lineptr)
  71. return -1;
  72. read_pos = *n - nchars_avail + *lineptr;
  73. assert(*n - nchars_avail == read_pos - *lineptr);
  74. }
  75.  
  76. if (c == EOF || ferror (stream))
  77. {
  78. /* Return partial line, if any. */
  79. if (read_pos == *lineptr)
  80. return -1;
  81. else
  82. break;
  83. }
  84.  
  85. *read_pos++ = c;
  86. nchars_avail--;
  87.  
  88. if (c == terminator)
  89. /* Return the line. */
  90. break;
  91. }
  92.  
  93. /* Done - NUL terminate and return the number of chars read. */
  94. *read_pos = '\0';
  95.  
  96. ret = read_pos - (*lineptr + offset);
  97. return ret;
  98. }
  99.  
  100. ssize_t getline(char **lineptr, size_t *n, FILE *stream)
  101. {
  102. return getstr (lineptr, n, stream, '\n', 0);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement