Cleanup and simplify get_reason() and related functions

Rename randreason() to fread_random() to better reflect its function,
and change it to a one-pass algorithm.  Have it write into a caller-provided
buffer instead of a static buffer.  Fixes a file leak when an empty
file is given to this function.

Change freadln() so that it never returns an empty line - it only returns
0 at end-of-file now.  This simplifies its callers.

Factor out some common code from the several get_reason()-type functions,
which greatly simplifies them.
This commit is contained in:
Kevin Easton
2015-06-27 00:38:40 +10:00
parent 5f4429bf7b
commit 27e32a0319
3 changed files with 55 additions and 66 deletions

View File

@@ -4012,10 +4012,10 @@ FILE *f;
{
char *hst = NULL;
buffer = alloca(sb.st_size+1);
freadln(f, buffer);
freadln(f, buffer, sb.st_size + 1);
while(!feof(f))
{
if ((freadln(f, buffer)))
if ((freadln(f, buffer, sb.st_size + 1)))
{
if (*buffer == '#')
continue;