bug#79497: fmt breaks lines when it does not need to
Pádraig Brady <[email protected]>
| Newsgroups | gmane.comp.gnu.core-utils.bugs |
|---|---|
| Message-ID | <[email protected]> |
On 23/09/2025 10:46, Bruno Haible via GNU coreutils Bug Reports wrote: > Hi, > > With the attached input file and coreutils-9.8, I see these > results from the 'fmt' command, in the en_US.UTF-8 locale > on a GNU system: > > $ fmt -w 12 -g 12 input.txt > Grüß > Gott. > Здравствуйте! > x=(-b±sqrt(b²-4ac))/(2a) > 日本語,中文,한글 > $ fmt -w 13 -g 13 input.txt > Grüß Gott. > Здравствуйте! > x=(-b±sqrt(b²-4ac))/(2a) > 日本語,中文,한글 > > Why does it take a width of 13, in order to keep the first two words > on a single line? That line has width 10. > > I would expect that already 'fmt -w 10 -g 10 input.txt' keeps these > two words together. Well the 10 vs 13 is due to fmt not being multi-byte aware (only fold(1) got the multi-byte treatment in coreutils 9.8). But yes there are still confusing things here. The man page says -w counts characters, while the info page says -w counts columns. The code seems to follow the man page and includes the \n in the char count. Note fold -w does not count the newline which is what I'd expect. We could change to that behavior without much compat issue I think. $ printf '%s' 'aafd as fd d' | wc -c 12 $ printf '%s' 'aafd as fd d' | git/coreutils/src/fmt -w12 -g12 aafd as fd d $ printf '%s' 'aafd as fd d' | git/coreutils/src/fmt -w13 -g13 aafd as fd d BTW I see fmt is not part of POSIX cheers, Padraig