Re: /bin/ls formatting broken for non-C(?) locales

From: Baptiste Daroussin <bapt_at_freebsd.org>
Date: Wed, 25 Nov 2015 15:35:01 +0100
On Wed, Nov 25, 2015 at 05:06:17PM +0300, Andrey Chernov wrote:
> On 25.11.2015 16:51, Baptiste Daroussin wrote:
> > wcslcat works like strlcat:
> > to quote the manpage:
> > "It will append at most dstsize - strlen(dst) - 1 characters."
> > So here with your version it will be n - wcslen(wab_months[i]) -1
> > which won't fit what we want to do.
> > 
> > btw that makes me figure out that what I want is wcsncat because we do care to
> > make sure we have appended the right number of spaces at the end of the
> > abbreviated month based on character width, not the on the len of the wide
> > string
> > 
> > so I changed it
> > 
> > if ((n = max_month_width - wab_months_width[i]) > 0)
> >     wcsncat(wab_months[i], L"     "/* MAX_ABMON_WIDTH */, n);
> 
> Sorry, I initially mean wcsncat() functionality here, wcslcat() is
> sneaked in due to wrong memorizing.
> 
> About width counting and fallback...
> Without attempt to nicely truncate data damaged by unknown way the code
> will be simpler. Instead all that loop adding width one by one wchar, just:
> 
> width = wcswidth(wab_months[i], MAX_ABMON_WIDTH);
> if (width == -1) {
>     max_month_width = -1;
>     return;
> }
> wab_months_width[i] = width;
> 
> About
> /* NULL terminate to simplify padding later */
> wab_months[i][j] = L'\0';
> You don't need it, mbstowcs() null-terminates result, if there is a room.

right if should be only set on the month we do truncate to MAX_ABMON_WIDTH

> BTW, array size looks suspicious:
> static wchar_t wab_months[12][MAX_ABMON_WIDTH * 2 * MB_LEN_MAX];
> what MB_LEN_MAX doing here? This constant is for multiple-bytes encoded,
> not for wide chars.
Bad copy/paste sorry it should be "MAX_ABMON_WIDTH * 2"


I will fix
Bapt

Received on Wed Nov 25 2015 - 13:35:05 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:01 UTC