'factor' is now much faster at identifying large prime numbers,
and significantly faster on composite numbers greater than 2^128.
- fold now exits promptly upon receiving a write error,
+ fold now exits immediately upon receiving a write error,
which is significant when reading large / unbounded inputs.
'seq' is more accurate with large integer start values.
return column;
}
+static void
+write_out (char const *line, size_t line_len, bool newline)
+{
+ if (fwrite (line, sizeof (char), line_len, stdout) != line_len
+ || (newline && putchar ('\n') < 0))
+ write_error ();
+}
+
/* Fold file FILENAME, or standard input if FILENAME is "-",
to stdout, with maximum line length WIDTH.
Return true if successful. */
}
if (g.ch == '\n')
{
- fwrite (line_out, sizeof (char), offset_out, stdout);
- putchar ('\n');
+ write_out (line_out, offset_out, /*newline=*/ true);
column = offset_out = 0;
continue;
}
{
logical_end += space_length;
/* Found a blank. Don't output the part after it. */
- fwrite (line_out, sizeof (char), logical_end, stdout);
- putchar ('\n');
+ write_out (line_out, logical_end, /*newline=*/ true);
/* Move the remainder to the beginning of the next line.
The areas being copied here might overlap. */
memmove (line_out, line_out + logical_end,
continue;
}
- fwrite (line_out, sizeof (char), offset_out, stdout);
- putchar ('\n');
+ write_out (line_out, offset_out, /*newline=*/ true);
column = offset_out = 0;
goto rescan;
}
zero. */
if (sizeof line_out <= offset_out + g.len)
{
- fwrite (line_out, sizeof (char), offset_out, stdout);
+ write_out (line_out, offset_out, /*newline=*/ false);
offset_out = 0;
}
if (feof (istream))
break;
- if (ferror (stdout))
- write_error ();
-
/* We read a full buffer of complete characters. */
offset_in = 0;
saved_errno = 0;
if (offset_out)
- fwrite (line_out, sizeof (char), offset_out, stdout);
+ write_out (line_out, offset_out, /*newline=*/ false);
if (STREQ (filename, "-"))
clearerr (istream);
test $(wc -l < out) -eq $(($IO_BUFSIZE_TIMES2 / 80)) || fail=1
# Ensure bounded memory operation.
+test -w /dev/full && test -c /dev/full &&
vm=$(get_min_ulimit_v_ fold /dev/null) && {
- head -c $IO_BUFSIZE_TIMES2 /dev/zero | tr -d '\n' \
- | (ulimit -v $(($vm+8000)) && fold 2>err) | head || fail=1
- compare /dev/null err || fail=1
+ # \303 results in EILSEQ on input
+ for c in '\n' '\0' '\303'; do
+ tr '\0' "$c" < /dev/zero | timeout 10 $SHELL -c \
+ "(ulimit -v $(($vm+8000)) && fold 2>err >/dev/full)"
+ { test $? = 124 || ! grep 'space' err >/dev/null; } &&
+ { fail=1; cat err; echo "fold didn't diagnose ENOSPC" >&2; }
+ done
}
Exit $fail