PƔdraig Brady <P@draigBrady.com> writes:
> On 24/08/2025 20:15, Collin Funk wrote:
>> PƔdraig Brady <P@draigBrady.com> writes:
>>
>>> I don't think getline() is a generic enough interface,
>>> especially for a utility used to introduce new lines.
>>>
>>> Consider for example you want 80x10 lines of "y",
>>> you could do this previously with:
>>>
>>> $ yes | tr -d '\n' | fold -w 80 | head -n 10
>>>
>>> While now fold will just consume all of memory.
>>> Instead you might fread() to an IO_BUFSIZE buffer?
>>
>> Good point.
>>
>> Using fread is also faster since it doesn't check for the delimiter upon
>> reading a character, and doesn't need to lock the stream each call
>> (since we use fread_unlocked).
>>
>> Will push the attached in a bit.
>
> Cool, that was quick!
> You might include something like the following test.
Actually, I realized that it is pointless to malloc line_out now. Since
the input buffer is a fixed size and we do not do case conversion or
anything that may change the number of bytes needed to represent the
output.
Will push this v2 patch in a bit instead.
> diff --git a/tests/fold/fold-characters.sh
> b/tests/fold/fold-characters.shindex 0b22aad6b..a6cc39b73 100755
> --- a/tests/fold/fold-characters.sh
> +++ b/tests/fold/fold-characters.sh
> @@ -58,4 +58,10 @@ compare column-exp2 column-out2 || fail=1
> fold --characters -w 10 input2 > character-out2 || fail=1
> compare character-exp2 character-out2 || fail=1
>
> +# Ensure bounded memory operation
> +vm=$(get_min_ulimit_v_ fold /dev/null) && {
> + yes | tr -d '\n' | (ulimit -v $(($vm+8000)) && fold 2>err) | head -n10 ||
> fail=1
> + compare /dev/null err || fail=1
> +}
> +
> Exit $fail
Thanks, I'll probably add this if I implement the --unicode option.
Since that will certainly require allocating memory based on my skimming
of unilbrk.h.
Collin