1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) many different people.
6 * If you wrote this, please acknowledge your work.
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off)
16 const int term_length = strlen(terminating_string);
17 int end_string_offset;
26 return linebuf; /* NULL */
30 if (idx >= linebufsz) {
32 linebuf = xrealloc(linebuf, linebufsz);
38 /* Check for terminating string */
39 end_string_offset = idx - term_length;
40 if (end_string_offset >= 0
41 && memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0
48 /* Grow/shrink *first*, then store NUL */
49 linebuf = xrealloc(linebuf, idx + 1);
54 /* Read up to TERMINATING_STRING from FILE and return it,
55 * including terminating string.
56 * Non-terminated string can be returned if EOF is reached.
57 * Return NULL if EOF is reached immediately. */
58 char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string)
60 return xmalloc_fgets_internal(file, terminating_string, 0);
63 char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string)
65 return xmalloc_fgets_internal(file, terminating_string, 1);