Return NULL if EOF is encountered before terminating_string.

This commit is contained in:
Matt Kraai 2001-10-18 17:04:22 +00:00
parent a7512d74fa
commit bcca3317b5

View File

@ -19,11 +19,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/* #include "libbb.h"
* Continue reading from file until the terminating string is encountered.
* Return data as string. /* Read up to (and including) TERMINATING_STRING from FILE and return it.
* e.g. fgets_str(file, "\n"); will read till end of file * Return NULL on EOF. */
*/
char *fgets_str(FILE *file, const char *terminating_string) char *fgets_str(FILE *file, const char *terminating_string)
{ {
@ -37,12 +36,13 @@ char *fgets_str(FILE *file, const char *terminating_string)
while (1) { while (1) {
ch = fgetc(file); ch = fgetc(file);
if (ch == EOF) { if (ch == EOF) {
break; free(linebuf);
return NULL;
} }
/* grow the line buffer as necessary */ /* grow the line buffer as necessary */
while (idx > linebufsz - 2) { while (idx > linebufsz - 2) {
linebuf = realloc(linebuf, linebufsz += 1000); /* GROWBY */ linebuf = xrealloc(linebuf, linebufsz += 1000);
} }
linebuf[idx] = ch; linebuf[idx] = ch;
@ -55,9 +55,6 @@ char *fgets_str(FILE *file, const char *terminating_string)
break; break;
} }
} }
if (idx == 0) {
return NULL;
}
linebuf[idx] = '\0'; linebuf[idx] = '\0';
return(linebuf); return(linebuf);
} }