diff options
author | Pádraig Brady <P@draigBrady.com> | 2012-12-19 19:27:10 +0000 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2012-12-21 15:55:19 +0000 |
commit | 76f606a9544bd67629bd4766ff6efe8bf42b20d9 (patch) | |
tree | be7a7617d93624af2e88a9c8aca32ea3dfea17bd /src | |
parent | 0568f72d2a09b247176b9ad162e41e3a475a1d53 (diff) | |
download | coreutils-76f606a9544bd67629bd4766ff6efe8bf42b20d9.tar.xz |
seq: fix newline output when -s specified
This regression was introduced in commit v8.19-132-g3786fb6.
* src/seq.c (seq_fast): Don't use puts() to output the first number,
and instead insert it into the buffer as for other numbers.
Also output the terminator unconditionally.
* tests/misc/seq.pl: Add some basic tests for the -s option.
* NEWS: Mention the fix.
* THANKS.in: Reported by Philipp Gortan.
Diffstat (limited to 'src')
-rw-r--r-- | src/seq.c | 20 |
1 files changed, 13 insertions, 7 deletions
@@ -419,30 +419,36 @@ seq_fast (char const *a, char const *b) bool ok = cmp (p, p_len, q, q_len) <= 0; if (ok) { - /* Buffer at least this many output lines per fwrite call. + /* Buffer at least this many numbers per fwrite call. This gives a speed-up of more than 2x over the unbuffered code when printing the first 10^9 integers. */ enum {N = 40}; char *buf = xmalloc (N * (n + 1)); char const *buf_end = buf + N * (n + 1); - puts (p); char *z = buf; + + /* Write first number to buffer. */ + z = mempcpy (z, p, p_len); + + /* Append separator then number. */ while (cmp (p, p_len, q, q_len) < 0) { + *z++ = *separator; incr (&p, &p_len); z = mempcpy (z, p, p_len); - *z++ = *separator; - if (buf_end - n - 1 < z) + /* If no place for another separator + number then + output buffer so far, and reset to start of buffer. */ + if (buf_end - (n + 1) < z) { fwrite (buf, z - buf, 1, stdout); z = buf; } } - /* Write any remaining, buffered output. */ - if (buf < z) - fwrite (buf, z - buf, 1, stdout); + /* Write any remaining buffered output, and the terminator. */ + *z++ = *terminator; + fwrite (buf, z - buf, 1, stdout); IF_LINT (free (buf)); } |