summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer2006-06-10 11:04:43 +0000
committerBernhard Reutner-Fischer2006-06-10 11:04:43 +0000
commit2d1a6e7c1fc5f5772aa9986893f3d6db28fd99c2 (patch)
tree42d77ce40a0d8615ef307e22b38d3ee918a4e8a1
parent6ba8bbe88a3bcb48df882793faea6713ad3bbc4e (diff)
downloadbusybox-2d1a6e7c1fc5f5772aa9986893f3d6db28fd99c2.zip
busybox-2d1a6e7c1fc5f5772aa9986893f3d6db28fd99c2.tar.gz
- fix bug #887, in bb_get_chomped_line_from_file(), the last char was removed
unconditionally, even if it was not a newline. This was apparently broken by r14254 - whitespace while at it.
-rw-r--r--libbb/get_line_from_file.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 44cf448..68837b2 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -18,7 +18,7 @@
* stored and free'ed by the caller. If end is null '\n' isn't considered
* and of line. If end isn't null, length of the chunk read is stored in it. */
-char *bb_get_chunk_from_file(FILE *file, int *end)
+char *bb_get_chunk_from_file(FILE * file, int *end)
{
int ch;
int idx = 0;
@@ -30,10 +30,12 @@ char *bb_get_chunk_from_file(FILE *file, int *end)
if (idx > linebufsz - 2) {
linebuf = xrealloc(linebuf, linebufsz += 80);
}
- linebuf[idx++] = (char)ch;
- if (!ch || (end && ch == '\n')) break;
+ linebuf[idx++] = (char) ch;
+ if (!ch || (end && ch == '\n'))
+ break;
}
- if (end) *end = idx;
+ if (end)
+ *end = idx;
if (linebuf) {
if (ferror(file)) {
free(linebuf);
@@ -45,18 +47,21 @@ char *bb_get_chunk_from_file(FILE *file, int *end)
}
/* Get line, including trailing /n if any */
-char *bb_get_line_from_file(FILE *file)
+char *bb_get_line_from_file(FILE * file)
{
int i;
+
return bb_get_chunk_from_file(file, &i);
}
/* Get line. Remove trailing /n */
-char *bb_get_chomped_line_from_file(FILE *file)
+char *bb_get_chomped_line_from_file(FILE * file)
{
int i;
- char *c=bb_get_chunk_from_file(file, &i);
- if(i) c[--i]=0;
-
+ char *c = bb_get_chunk_from_file(file, &i);
+
+ if (i && c[--i] == '\n')
+ c[i] = 0;
+
return c;
}