bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getprogname


From: Bruno Haible
Subject: Re: getprogname
Date: Tue, 10 Jan 2006 15:50:14 +0100
User-agent: KMail/1.5

Peter O'Gorman wrote:
> Solaris seems to have a getexecname

Interesting. So this provides a fallback, like on glibc systems, for the
case when setprogname(argv[0]) has not been called.

> I'd suggest the following instead of Paul's proposal, as it allows
> the programmer to override the program name.

Well, Paul's proposal to use 'get/setprogname' also allows the
programmer to override the program name.

> static char * prog_name = NULL;
>
> void
> set_prog_name(char * name)
> {
>       if (prog_name) free(prog_name);
>       prog_name = strdup(name);
> }
>
> char *
> get_prog_name(void)
> {
>       char * name;
>       if (prog_name)
>               name = prog_name;
>       else
>       {
> #if defined(HAVE_GETPROGNAME)
> #include <stdlib.h>
>               name = getprogname();
> #elif defined(HAVE_GETEXECNAME)
> #include <stdlib.h>
>               name = getexecname();
> #elif defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
> #include <errno.h>
>               name = program_invocation_short_name;
> #else
>               name = "executable";
> #endif
>       }
>       return name;
> }

Well, my intention is basically this:

#if !(HAVE_GETPROGNAME && HAVE_SETPROGNAME) /* nothing to do on FreeBSD, NetBSD 
*/

static const char * progname = NULL;

void
setprogname(const char *arg)
{
  progname = arg;
}

const char *
getprogname()
{
  if (progname != NULL)
    /* actually not only basename, but also remove leading "-" and ".lt-" */
    return basename (progname);
  else
    {
      #if HAVE_PROGRAM_INVOCATION_SHORT_NAME /* glibc */
        return program_invocation_short_name;
      #elif HAVE_GETEXECNAME /* Solaris */
        return basename (getexecname ());
      #else
        return NULL;
      #endif
    }
}

#endif

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]