2001-10-24 05:00:29 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-06-13 07:34:03 +00:00
|
|
|
/*
|
2001-10-24 05:00:29 +00:00
|
|
|
* Utility routines.
|
2001-06-13 07:34:03 +00:00
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) many different people.
|
2003-07-14 21:21:08 +00:00
|
|
|
* If you wrote this, please acknowledge your work.
|
2001-06-13 07:34:03 +00:00
|
|
|
*
|
2006-07-10 11:41:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-06-13 07:34:03 +00:00
|
|
|
*/
|
|
|
|
|
2001-10-18 17:04:22 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2009-03-18 17:32:44 +00:00
|
|
|
static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off, size_t *maxsz_p)
|
2001-06-13 07:34:03 +00:00
|
|
|
{
|
|
|
|
char *linebuf = NULL;
|
|
|
|
const int term_length = strlen(terminating_string);
|
|
|
|
int end_string_offset;
|
|
|
|
int linebufsz = 0;
|
|
|
|
int idx = 0;
|
|
|
|
int ch;
|
2009-03-18 17:32:44 +00:00
|
|
|
size_t maxsz = *maxsz_p;
|
2001-06-13 07:34:03 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ch = fgetc(file);
|
|
|
|
if (ch == EOF) {
|
2007-12-26 20:44:45 +00:00
|
|
|
if (idx == 0)
|
|
|
|
return linebuf; /* NULL */
|
|
|
|
break;
|
2001-06-13 07:34:03 +00:00
|
|
|
}
|
|
|
|
|
2007-12-26 20:44:45 +00:00
|
|
|
if (idx >= linebufsz) {
|
2006-10-26 23:25:17 +00:00
|
|
|
linebufsz += 200;
|
|
|
|
linebuf = xrealloc(linebuf, linebufsz);
|
2009-03-18 17:32:44 +00:00
|
|
|
if (idx >= maxsz) {
|
|
|
|
linebuf[idx] = ch;
|
|
|
|
idx++;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-13 07:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
linebuf[idx] = ch;
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
/* Check for terminating string */
|
|
|
|
end_string_offset = idx - term_length;
|
2007-12-26 20:44:45 +00:00
|
|
|
if (end_string_offset >= 0
|
2006-10-12 19:29:44 +00:00
|
|
|
&& memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0
|
|
|
|
) {
|
2007-12-26 20:44:45 +00:00
|
|
|
if (chop_off)
|
|
|
|
idx -= term_length;
|
2001-06-13 07:34:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-12-26 20:44:45 +00:00
|
|
|
/* Grow/shrink *first*, then store NUL */
|
2006-10-12 19:29:44 +00:00
|
|
|
linebuf = xrealloc(linebuf, idx + 1);
|
2001-06-13 07:34:03 +00:00
|
|
|
linebuf[idx] = '\0';
|
2009-03-18 17:32:44 +00:00
|
|
|
*maxsz_p = idx;
|
2006-10-12 19:29:44 +00:00
|
|
|
return linebuf;
|
2001-06-13 07:34:03 +00:00
|
|
|
}
|
2007-12-26 20:44:45 +00:00
|
|
|
|
|
|
|
/* Read up to TERMINATING_STRING from FILE and return it,
|
|
|
|
* including terminating string.
|
|
|
|
* Non-terminated string can be returned if EOF is reached.
|
|
|
|
* Return NULL if EOF is reached immediately. */
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string)
|
2007-12-26 20:44:45 +00:00
|
|
|
{
|
2009-03-18 17:32:44 +00:00
|
|
|
size_t maxsz = INT_MAX - 4095;
|
|
|
|
return xmalloc_fgets_internal(file, terminating_string, 0, &maxsz);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p)
|
|
|
|
{
|
|
|
|
size_t maxsz;
|
|
|
|
|
|
|
|
if (!maxsz_p) {
|
|
|
|
maxsz = INT_MAX - 4095;
|
|
|
|
maxsz_p = &maxsz;
|
|
|
|
}
|
|
|
|
return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p);
|
2007-12-26 20:44:45 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string)
|
2007-12-26 20:44:45 +00:00
|
|
|
{
|
2009-03-18 17:32:44 +00:00
|
|
|
size_t maxsz = INT_MAX - 4095;
|
|
|
|
return xmalloc_fgets_internal(file, terminating_string, 1, &maxsz);
|
2007-12-26 20:44:45 +00:00
|
|
|
}
|