Re: [PATCH 1/2] mingw: get pw_name in UTF-8 format
- From
Johannes Schindelin <johannes.schindelin@gmx.de>
- Date
- Jul 4, 2019, 21:52 UTC
- Message-ID
- <nycvar.QRO.7.76.6.1907042350510.44@tvgsbejvaqbjf.bet>
- In-Reply-To
- <9bd9efde-8971-08f2-8417-1036286c9d1a@drbeat.li>
Hi Beat,
On Thu, 4 Jul 2019, Beat Bolli wrote:
Show 35 quoted lines
> On 27.06.19 11:37, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > Previously, we would have obtained the user name encoded in whatever the
> > current code page is.
> >
> > Note: the "user name" here does not denote the full name but instead the
> > short logon name.
> >
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> > compat/mingw.c | 10 ++++++++--
> > 1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/compat/mingw.c b/compat/mingw.c
> > index 9b6d2400e1..8526876262 100644
> > --- a/compat/mingw.c
> > +++ b/compat/mingw.c
> > @@ -1946,13 +1946,19 @@ struct passwd *getpwuid(int uid)
> > static unsigned initialized;
> > static char user_name[100];
> > static struct passwd *p;
> > + wchar_t buf[100];
> > DWORD len;
> >
> > if (initialized)
> > return p;
> >
> > - len = sizeof(user_name);
> > - if (!GetUserName(user_name, &len)) {
> > + len = sizeof(buf);
>
> I think this should be "len = sizeof(buf) / sizeof(buf[0])".
>
> GetUserNameW() takes the number of characters, not bytes.Good catch.
How about I use `ARRAY_SIZE(buf)`? Since this is already in `next`, I will prepare a follow-up patch.
Ciao, Dscho
Show 15 quoted lines
>
> > + if (!GetUserNameW(buf, &len)) {
> > + initialized = 1;
> > + return NULL;
> > + }
> > +
> > + if (xwcstoutf(user_name, buf, sizeof(user_name)) < 0) {
> > initialized = 1;
> > return NULL;
> > }
> >
>
> Cheers,
> Beat
>