summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2011-05-25 13:05:37 +0100
committerPádraig Brady <P@draigBrady.com>2011-05-25 23:04:58 +0100
commitd92849fe5a9c8a24203f70b02806737666cb980b (patch)
tree3a90141f81ee5ff12a87854bb8256b00e4d5e010 /src
parente89c998a9ecbe0a1b29d7e6c97531d5069877087 (diff)
downloadcoreutils-d92849fe5a9c8a24203f70b02806737666cb980b.tar.xz
split: fix an edge case where -n l/... creates an extra file
* src/split.c (lines_bytes_chunk): Handle the edge case where the file is truncated as we read. * tests/misc/split-lchunk: Cleanup; no functional change.
Diffstat (limited to 'src')
-rw-r--r--src/split.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/split.c b/src/split.c
index e09ebcec8..6c75080a7 100644
--- a/src/split.c
+++ b/src/split.c
@@ -599,6 +599,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
off_t chunk_end = chunk_size - 1;
off_t n_written = 0;
bool new_file_flag = true;
+ bool chunk_truncated = false;
if (k > 1)
{
@@ -620,6 +621,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
error (EXIT_FAILURE, errno, "%s", infile);
else if (n_read == 0)
break; /* eof. */
+ chunk_truncated = false;
eob = buf + n_read;
while (bp != eob)
@@ -659,11 +661,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
if (!next && bp == eob)
{
/* replenish buf, before going to next chunk. */
-
- /* If we're going to stop reading,
- then count the current chunk. */
- if (n_written >= file_size)
- chunk_no++;
+ chunk_truncated = true;
break;
}
chunk_no++;
@@ -684,6 +682,9 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
}
}
+ if (chunk_truncated)
+ chunk_no++;
+
/* Ensure NUMBER files are created, which truncates
any existing files or notifies any consumers on fifos.
FIXME: Should we do this before EXIT_FAILURE? */