aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-10-18 17:04:22 +0000
committerkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-10-18 17:04:22 +0000
commit1d013457cd7513d4422a73cc06fc61c92974c350 (patch)
tree505f6775c94e42f37287c73e8a2ce14e020b201f /libbb
parentb2be82f658346bb4733143198f4fde705a5c2ffa (diff)
downloadbusybox-w32-1d013457cd7513d4422a73cc06fc61c92974c350.tar.gz
busybox-w32-1d013457cd7513d4422a73cc06fc61c92974c350.tar.bz2
busybox-w32-1d013457cd7513d4422a73cc06fc61c92974c350.zip
Return NULL if EOF is encountered before terminating_string.
git-svn-id: svn://busybox.net/trunk/busybox@3545 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-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}