diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-02 17:58:10 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-02 17:58:10 +0000 |
commit | 8b22b07bc599ca39d1b42cabef98189894b2162f (patch) | |
tree | 64ba681d4e88c20dcb36b2c0c2269e8d4aa5eba5 /libbb/get_line_from_file.c | |
parent | becd8c538cc689460dca83ecc92222969059c5f4 (diff) | |
download | busybox-w32-8b22b07bc599ca39d1b42cabef98189894b2162f.tar.gz busybox-w32-8b22b07bc599ca39d1b42cabef98189894b2162f.tar.bz2 busybox-w32-8b22b07bc599ca39d1b42cabef98189894b2162f.zip |
sed: improve handling of NULs
Diffstat (limited to 'libbb/get_line_from_file.c')
-rw-r--r-- | libbb/get_line_from_file.c | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index b424d59e9..3f2c6096e 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c | |||
@@ -11,8 +11,8 @@ | |||
11 | 11 | ||
12 | #include "libbb.h" | 12 | #include "libbb.h" |
13 | 13 | ||
14 | /* This function reads an entire line from a text file, | 14 | /* This function reads an entire line from a text file, up to a newline |
15 | * up to a newline or NUL byte. It returns a malloc'ed char * which must be | 15 | * or NUL byte, inclusive. It returns a malloc'ed char * which must be |
16 | * stored and free'ed by the caller. If end is null '\n' isn't considered | 16 | * stored and free'ed by the caller. If end is null '\n' isn't considered |
17 | * end of line. If end isn't null, length of the chunk read is stored in it. */ | 17 | * end of line. If end isn't null, length of the chunk read is stored in it. */ |
18 | 18 | ||
@@ -25,7 +25,7 @@ char *bb_get_chunk_from_file(FILE * file, int *end) | |||
25 | 25 | ||
26 | while ((ch = getc(file)) != EOF) { | 26 | while ((ch = getc(file)) != EOF) { |
27 | /* grow the line buffer as necessary */ | 27 | /* grow the line buffer as necessary */ |
28 | if (idx > linebufsz - 2) { | 28 | if (idx >= linebufsz) { |
29 | linebuf = xrealloc(linebuf, linebufsz += 80); | 29 | linebuf = xrealloc(linebuf, linebufsz += 80); |
30 | } | 30 | } |
31 | linebuf[idx++] = (char) ch; | 31 | linebuf[idx++] = (char) ch; |
@@ -35,14 +35,14 @@ char *bb_get_chunk_from_file(FILE * file, int *end) | |||
35 | if (end) | 35 | if (end) |
36 | *end = idx; | 36 | *end = idx; |
37 | if (linebuf) { | 37 | if (linebuf) { |
38 | // huh, is fgets discards prior data on error like this? | 38 | // huh, does fgets discard prior data on error like this? |
39 | // I don't think so.... | 39 | // I don't think so.... |
40 | //if (ferror(file)) { | 40 | //if (ferror(file)) { |
41 | // free(linebuf); | 41 | // free(linebuf); |
42 | // return NULL; | 42 | // return NULL; |
43 | //} | 43 | //} |
44 | linebuf = xrealloc(linebuf, idx+1); | 44 | linebuf = xrealloc(linebuf, idx+1); |
45 | linebuf[idx] = 0; | 45 | linebuf[idx] = '\0'; |
46 | } | 46 | } |
47 | return linebuf; | 47 | return linebuf; |
48 | } | 48 | } |