summaryrefslogtreecommitdiff
path: root/src/split.c
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2011-05-24 09:59:08 +0100
committerPádraig Brady <P@draigBrady.com>2011-05-24 16:29:48 +0100
commit7d44751f0ea9d353c14edffbf89dc61dcafa8f22 (patch)
treebc5c62e21f1a4feb2173e6f9b6fd2a9113c1a36a /src/split.c
parent9ead32a9be9543af3f1b73080a7391c89b1d784b (diff)
downloadcoreutils-7d44751f0ea9d353c14edffbf89dc61dcafa8f22.tar.gz
coreutils-7d44751f0ea9d353c14edffbf89dc61dcafa8f22.tar.bz2
coreutils-7d44751f0ea9d353c14edffbf89dc61dcafa8f22.zip
split: fix cases where -n l/... creates extraneous files
* src/split.c (lines_chunk_split): Ensure that data is only written to stdout when k specified. Also ensure that extra files are not created when there is more data available than reported in the file size. * tests/misc/split-lchunk: Verify that split -n l/k/n doesn't generate any files, and that -n l/n always generates n files. * NEWS: Mention the fix.
Diffstat (limited to 'src/split.c')
-rw-r--r--src/split.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/split.c b/src/split.c
index 05315e6fb..e09ebcec8 100644
--- a/src/split.c
+++ b/src/split.c
@@ -645,7 +645,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
&& ! ignorable (errno))
error (EXIT_FAILURE, errno, "%s", _("write error"));
}
- else
+ else if (! k)
cwrite (new_file_flag, bp, to_write);
n_written += to_write;
bp += to_write;
@@ -657,7 +657,15 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
while (next || chunk_end <= n_written - 1)
{
if (!next && bp == eob)
- break; /* replenish buf, before going to next chunk. */
+ {
+ /* 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++;
+ break;
+ }
chunk_no++;
if (k && chunk_no > k)
return;
@@ -666,7 +674,10 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
else
chunk_end += chunk_size;
if (chunk_end <= n_written - 1)
- cwrite (true, NULL, 0);
+ {
+ if (! k)
+ cwrite (true, NULL, 0);
+ }
else
next = false;
}