aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-10-18 17:04:22 +0000
committerMatt Kraai <kraai@debian.org>2001-10-18 17:04:22 +0000
commitbcca3317b59a0180f0b42428e9c25405bc423520 (patch)
tree505f6775c94e42f37287c73e8a2ce14e020b201f
parenta7512d74fa64649cdd93602a00046b4ba028cea9 (diff)
downloadbusybox-w32-bcca3317b59a0180f0b42428e9c25405bc423520.tar.gz
busybox-w32-bcca3317b59a0180f0b42428e9c25405bc423520.tar.bz2
busybox-w32-bcca3317b59a0180f0b42428e9c25405bc423520.zip
Return NULL if EOF is encountered before terminating_string.
-rw-r--r--libbb/fgets_str.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/libbb/fgets_str.c b/libbb/fgets_str.c
index 33d8d00cc..4943464d5 100644
--- a/libbb/fgets_str.c
+++ b/libbb/fgets_str.c
@@ -19,11 +19,10 @@
19#include <stdlib.h> 19#include <stdlib.h>
20#include <string.h> 20#include <string.h>
21 21
22/* 22#include "libbb.h"
23 * Continue reading from file until the terminating string is encountered. 23
24 * Return data as string. 24/* Read up to (and including) TERMINATING_STRING from FILE and return it.
25 * e.g. fgets_str(file, "\n"); will read till end of file 25 * Return NULL on EOF. */
26 */
27 26
28char *fgets_str(FILE *file, const char *terminating_string) 27char *fgets_str(FILE *file, const char *terminating_string)
29{ 28{
@@ -37,12 +36,13 @@ char *fgets_str(FILE *file, const char *terminating_string)
37 while (1) { 36 while (1) {
38 ch = fgetc(file); 37 ch = fgetc(file);
39 if (ch == EOF) { 38 if (ch == EOF) {
40 break; 39 free(linebuf);
40 return NULL;
41 } 41 }
42 42
43 /* grow the line buffer as necessary */ 43 /* grow the line buffer as necessary */
44 while (idx > linebufsz - 2) { 44 while (idx > linebufsz - 2) {
45 linebuf = realloc(linebuf, linebufsz += 1000); /* GROWBY */ 45 linebuf = xrealloc(linebuf, linebufsz += 1000);
46 } 46 }
47 47
48 linebuf[idx] = ch; 48 linebuf[idx] = ch;
@@ -55,9 +55,6 @@ char *fgets_str(FILE *file, const char *terminating_string)
55 break; 55 break;
56 } 56 }
57 } 57 }
58 if (idx == 0) {
59 return NULL;
60 }
61 linebuf[idx] = '\0'; 58 linebuf[idx] = '\0';
62 return(linebuf); 59 return(linebuf);
63} 60}