Tweaking the program name for <err.h> functions

enh enh at google.com
Fri Mar 8 00:47:18 UTC 2024


On Thu, Mar 7, 2024 at 4:31 PM Guillem Jover <guillem at hadrons.org> wrote:
>
> Hi!
>
> On Thu, 2024-03-07 at 23:24:32 +0100, Alejandro Colomar wrote:
> > I'm interested in knowing if the <err.h> functions have a supported way
> > of tweaking the prefix they print.
> >
> > GNU's similar <error.h> functions support changing
> >
> >       extern char *program_invocation_name;
> >
> > for that, as per the error(3) manual page:
> >
> >      The  program  name  printed by error() is the value of the global
> >      variable   program_invocation_name(3).    program_invocation_name
> >      initially  has  the same value as main()’s argv[0].  The value of
> >      this variable can be modified to change the output of error().
> >
> >
> > I have experimented with err(3), and it seems you can tweak it by
> > modifying 'program_invocation_short_name', but I'd like to know if
> > that's supported and/or portable.  I guess that since the pointer is a
> > GNU extension, while the functions come from the BSDs, that's not
> > portable, and thus not supported.
>
> That is not portable because the BSDs do not support that variable.
> But all BSDs for which I've got a checkout (FreeBSD, NetBSD, OpenBSD,
> DragonflyBSD) support changing it via setprogname(), but that's not
> available in glibc (libbsd provides it though).

(yeah, bionic -- having its <err.h> based on OpenBSD -- uses
setprogname() for this.)

> libbsd and cosmopolitan libc support setting it via setprogname() or
> program_invocation_short_name.
>
> musl libc supports setting it via program_invocation_short_name.
>
> uclibc might support __progname and/or program_invocation_short_name
> depending on how it has been configured, but it does not support
> setting the err(3) program name via those.
>
> > Maybe it would be interesting to get the BSDs to support these pointers?
>
> They already have a way to control the program name though. (And
> it seems to me that using functions instead of global variables is
> superior. :)
>
> Thanks,
> Guillem


More information about the libbsd mailing list