2
Copyright (C) 2009- The University of Notre Dame
3
Originally written by Kevin Partington (27 January 2009)
4
This software is distributed under the GNU General Public License.
5
See the file COPYING for details.
14
char * get_line( FILE *fp )
16
static char *other = NULL;
17
static char buffer[LARGE_LINE_MAX];
19
/* Free the other buffer, if we have used it. */
26
if (!fgets(buffer, LARGE_LINE_MAX, fp))
31
/* If the main buffer is completely filled and there is more
32
to read... (second condition is for slackers who don't put newlines
33
at the end of their text files) */
34
if (!strrchr(buffer, '\n') && strlen(buffer) == LARGE_LINE_MAX - 1)
36
int s = LARGE_LINE_MAX;
38
/* ...use the heap buffer ("other"), doubling its size
39
repeatedly until it is filled. */
41
char *tmp = realloc(other, 2*s * sizeof(char));
52
strncpy(tmp, buffer, strlen(buffer));
57
/* Reusing tmp as a return value check */
58
tmp = fgets(other + s - 1, s + 1, fp);
62
/* fgets failed because there is no more to
63
read (i.e., EOF), after a read has already
64
occurred. This shouldn't happen if the file
65
is properly delimited with newlines
66
(including one on the end of the file
67
itself). If you don't write newlines at the
68
end of your text files, you should be
69
ashamed of yourself! -KP
76
} while (!strrchr(other, '\n'));