Re: [PATCH v4 1/1] printf: add support for printing symbolic error names

From: Andy Shevchenko
Date: Mon Oct 14 2019 - 09:11:07 EST


On Mon, Oct 14, 2019 at 4:02 PM Petr Mladek <pmladek@xxxxxxxx> wrote:
> On Fri 2019-10-11 15:36:17, Rasmus Villemoes wrote:
> > It has been suggested several times to extend vsnprintf() to be able
> > to convert the numeric value of ENOSPC to print "ENOSPC". This
> > implements that as a %p extension: With %pe, one can do

> > +const char *errname(int err)
> > +{
> > + bool pos = err > 0;
> > + const char *name = __errname(err > 0 ? err : -err);
> > +
> > + return name ? name + pos : NULL;
>
> This made me to check C standard. It seems that "true" really has
> to be "1".

You may guarantee it by using !!.

return name ? name + !!(err > 0) : NULL;


But to me it seems like forgotten use of pos in the other case above.

Anyway, I would rather do

abs(err) in the first place

and simple use name + 1 in the second as you suggested with maybe a
comment that we skip E letter.

> But I think that I am not the only one who is not sure.
> I would prefer to make it less tricky and use, for example:
>
> const char *name = __errname(err > 0 ? err : -err);
> if (!name)
> return NULL;
>
> return err > 0 ? name + 1 : name;

> > +}


--
With Best Regards,
Andy Shevchenko