Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getting main's argv[0]

1 view
Skip to first unread message

viza

unread,
Oct 9, 2008, 11:48:54 AM10/9/08
to
Hi all

Is there a usual way of accessing main's argv[0] from other functions, or
some string containing the same thing, but without passing an additional
parameter or defining my own global?

Gnu provides program_invocation_name, but what about *BSD or others?

If there is no general way, what are the system specific methods?

Thanks

viza

Lew Pitcher

unread,
Oct 9, 2008, 12:16:55 PM10/9/08
to
On October 9, 2008 11:48, in comp.unix.programmer, viza
(tom....@gm-il.com.obviouschange.invalid) wrote:

> Hi all
>
> Is there a usual way of accessing main's argv[0] from other functions, or
> some string containing the same thing, but without passing an additional
> parameter or defining my own global?

There is no general way to do this.

> Gnu provides program_invocation_name, but what about *BSD or others?
>
> If there is no general way, what are the system specific methods?

In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
contains a list of argv strings

FWIW, argv[0] is not guaranteed to be the name of the program invoked.
It /is/ the first argument value passed to the exec() family of functions,
and /by convention/ is the name of the program. However, there is
no /requirement/ that argv[0] name a program (witness the argv[0] of your
login shell, which is typically named '-', no matter /which/ binary is
used).

HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


viza

unread,
Oct 9, 2008, 12:18:19 PM10/9/08
to
Hi

On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:


> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:

>> Is there a usual way of accessing main's argv[0] from other functions,
>> or some string containing the same thing, but without passing an
>> additional parameter or defining my own global?
>>

>> Gnu provides program_invocation_name, but what about *BSD or others?
>>
>> If there is no general way, what are the system specific methods?
>
> In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
> contains a list of argv strings

I'm hoping for a symbol (variable or function) that the gives the pointer
just after the start of the stack where this might be found. This varies
depending on architecture so perhaps the various libc's define something.

Any ideas?


> FWIW, argv[0] is not guaranteed to be the name of the program invoked.

Yes I realise that, all I want is a name by which the program should
identify itself in logs.

Thanks
viza

Rick Jones

unread,
Oct 9, 2008, 2:32:35 PM10/9/08
to
viza <tom....@gm-il.com.obviouschange.invalid> wrote:
> Yes I realise that, all I want is a name by which the program should
> identify itself in logs.

Do we know that that which launches the executable will "normalize"
the path used to launch it? If it is launched as "foo" and then later
launched as "/usr/local/bin/foo" will argv[0] match in both cases? If
not will your application "parse" argv[0] to do the normalization
itself?

Will your program append other information to uniquely identify two
concurrent intsances of "foo" running?

It sounds like the most portable thing to do is to compile-in a
default id and allow an overrid via the command line or config file.

rick jones
--
The computing industry isn't as much a game of "Follow The Leader" as
it is one of "Ring Around the Rosy" or perhaps "Duck Duck Goose."
- Rick Jones
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...

Nate Eldredge

unread,
Oct 9, 2008, 2:51:06 PM10/9/08
to
viza <tom....@gm-il.com.obviouschange.invalid> writes:

> Hi
>
> On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
>> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
>
>>> Is there a usual way of accessing main's argv[0] from other functions,
>>> or some string containing the same thing, but without passing an
>>> additional parameter or defining my own global?
>>>
>>> Gnu provides program_invocation_name, but what about *BSD or others?

It looks like some flavors of BSD have a function '_getprogname()' and a
variable '__progname'. It seems to be undocumented and could change.

>>> If there is no general way, what are the system specific methods?
>>
>> In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
>> contains a list of argv strings
>
> I'm hoping for a symbol (variable or function) that the gives the pointer
> just after the start of the stack where this might be found. This varies
> depending on architecture so perhaps the various libc's define something.

I don't think so. You might be able to unwind the stack by chasing the
frame pointers, but some compiler options might make this impossible.

> Any ideas?
>
>
>> FWIW, argv[0] is not guaranteed to be the name of the program invoked.
>
> Yes I realise that, all I want is a name by which the program should
> identify itself in logs.

I presume you're writing a library, so that main() is not your function.
I think your best bet is to make the program tell you: provide a
set_program_name() function or something that the user is expected to
call in main(). It's awkward, yes, but I think anything else is going
to be very painful and unreliable.

Giorgos Keramidas

unread,
Oct 9, 2008, 4:36:51 PM10/9/08
to
On Thu, 09 Oct 2008 15:48:54 GMT, viza <tom....@gm-il.com.obviouschange.invalid> wrote:
> Hi all
>
> Is there a usual way of accessing main's argv[0] from other functions,
> or some string containing the same thing, but without passing an
> additional parameter or defining my own global?
>
> Gnu provides program_invocation_name, but what about *BSD or others?

The BSDs include a getprogname() and setprogname() pair of functions.

Giorgos Keramidas

unread,
Oct 9, 2008, 4:40:54 PM10/9/08
to
On Thu, 09 Oct 2008 11:51:06 -0700, Nate Eldredge <na...@vulcan.lan> wrote:
>viza <tom....@gm-il.com.obviouschange.invalid> writes:
>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
>>>On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
>>>> Is there a usual way of accessing main's argv[0] from other functions,
>>>> or some string containing the same thing, but without passing an
>>>> additional parameter or defining my own global?
>>>>
>>>> Gnu provides program_invocation_name, but what about *BSD or others?
>
> It looks like some flavors of BSD have a function '_getprogname()' and a
> variable '__progname'. It seems to be undocumented and could change.

Please don't use the `internal' _getprogname() and __progname symbols,
but their `public' version: getprogname(). They are defined by stdlib.h
in the BSDs and their manpage states:

HISTORY
These functions first appeared in NetBSD 1.6, and made their way
into FreeBSD 4.4.

For older versions of FreeBSD you can always define a program-specific
global, but if you are still using such an old release, the lack of
getprogname() is likely to be a very minor source of trouble. The
releases that are older than 4.4 have been superseded by more modern
FreeBSD releases, and they are very likely to have security bugs that
are unfixed.

Marc

unread,
Oct 9, 2008, 5:03:11 PM10/9/08
to
Giorgos Keramidas wrote:

>> Is there a usual way of accessing main's argv[0] from other functions,
>> or some string containing the same thing, but without passing an
>> additional parameter or defining my own global?
>>
>> Gnu provides program_invocation_name, but what about *BSD or others?
>
> The BSDs include a getprogname() and setprogname() pair of functions.

And Solaris has getexecname. Now googling for the 3 variants leads to a
piece of code in mesa (the graphics library) that defines a macro
GET_PROGRAM_NAME as a wrapper to these.

Andrew Gabriel

unread,
Oct 9, 2008, 5:09:45 PM10/9/08
to
In article <86zlld8...@vulcan.lan>,

Nate Eldredge <na...@vulcan.lan> writes:
> viza <tom....@gm-il.com.obviouschange.invalid> writes:
>
>> Hi
>>
>> On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
>>> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
>>
>>>> Is there a usual way of accessing main's argv[0] from other functions,
>>>> or some string containing the same thing, but without passing an
>>>> additional parameter or defining my own global?
>>>>
>>>> Gnu provides program_invocation_name, but what about *BSD or others?
>
> It looks like some flavors of BSD have a function '_getprogname()' and a
> variable '__progname'. It seems to be undocumented and could change.

On Solaris, getexecname(3C).

--
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]

Nate Eldredge

unread,
Oct 9, 2008, 5:19:10 PM10/9/08
to
Giorgos Keramidas <kera...@ceid.upatras.gr> writes:

> On Thu, 09 Oct 2008 11:51:06 -0700, Nate Eldredge <na...@vulcan.lan> wrote:
>>viza <tom....@gm-il.com.obviouschange.invalid> writes:
>>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
>>>>On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
>>>>> Is there a usual way of accessing main's argv[0] from other functions,
>>>>> or some string containing the same thing, but without passing an
>>>>> additional parameter or defining my own global?
>>>>>
>>>>> Gnu provides program_invocation_name, but what about *BSD or others?
>>
>> It looks like some flavors of BSD have a function '_getprogname()' and a
>> variable '__progname'. It seems to be undocumented and could change.
>
> Please don't use the `internal' _getprogname() and __progname symbols,
> but their `public' version: getprogname(). They are defined by stdlib.h
> in the BSDs and their manpage states:

Thanks. I was looking at the source for syslog and crt0 which use the
internal ones, so I didn't see the public version. Obviously that is
preferable.

Jason C

unread,
Oct 11, 2008, 5:38:30 PM10/11/08
to
On Oct 9, 5:19 pm, Nate Eldredge <n...@vulcan.lan> wrote:
> Giorgos Keramidas <keram...@ceid.upatras.gr> writes:

> > On Thu, 09 Oct 2008 11:51:06 -0700, Nate Eldredge <n...@vulcan.lan> wrote:
> >>viza <tom.v...@gm-il.com.obviouschange.invalid> writes:
> >>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
> >>>>On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
> >>>>> Is there a usual way of accessing main's argv[0] from other functions,
> >>>>> or some string containing the same thing, but without passing an
> >>>>> additional parameter or defining my own global?
>
> >>>>> Gnu provides program_invocation_name, but what about *BSD or others?
>
> >> It looks like some flavors of BSD have a function '_getprogname()' and a
> >> variable '__progname'.  It seems to be undocumented and could change.
>
> > Please don't use the `internal' _getprogname() and __progname symbols,
> > but their `public' version: getprogname().  They are defined by stdlib.h
> > in the BSDs and their manpage states:
>
> Thanks.  I was looking at the source for syslog and crt0 which use the
> internal ones, so I didn't see the public version.  Obviously that is
> preferable.

It's not going to change while a program is running. You would get
exactly what you want by storing a copy of argv[0] in a *gasp* global
variable when the program starts, and referring to it elsewhere.

If you are using it for logging and it makes you feel more
comfortable, simply have some concept of "initializing" logging on
program startup and store the value of argv[0] there.

Jason

viza

unread,
Oct 13, 2008, 3:48:18 AM10/13/08
to
On Sat, 11 Oct 2008 14:38:30 -0700, Jason C wrote:
> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:

>> Is there a usual way of accessing main's argv[0] from other
>> functions, or some string containing the same thing, but without
>> passing an additional parameter or defining my own global?

> It's not going to change while a program is running. You would get


> exactly what you want by storing a copy of argv[0] in a *gasp* global
> variable when the program starts, and referring to it elsewhere.

As Nate correctly identified my library cannot modify every application's
main().

Kenny McCormack

unread,
Oct 14, 2008, 10:08:13 AM10/14/08
to
In article <f9qHk.23329$wG3...@newsfe23.ams2>,

It sounds like you've got your answer, which is: There's no (POSIX)
portable way to do it, but there are (differing) ways to do it in many
of the major Unix flavors in common use today. The fact that you are
writing a library, not an application, means that you can't get "inside
main()" to do any dirty work.

So, your best bet is to "ifdef" for the major Unix flavors in common use
today.

BTW, in Linux, I find /proc/self/exe to be very useful.

viza

unread,
Oct 14, 2008, 12:08:21 PM10/14/08
to
Hi

On Tue, 14 Oct 2008 14:08:13 +0000, Kenny McCormack wrote:
> <tom....@gm-il.com.obviouschange.invalid> wrote:
>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
>>> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
>>
>>>> Is there a usual way of accessing main's argv[0] from other
>>>> functions, or some string containing the same thing, but without
>>>> passing an additional parameter or defining my own global?

> It sounds like you've got your answer, which is: There's no (POSIX)


> portable way to do it, but there are (differing) ways to do it in many
> of the major Unix flavors in common use today. The fact that you are
> writing a library, not an application, means that you can't get "inside
> main()" to do any dirty work.
>
> So, your best bet is to "ifdef" for the major Unix flavors in common use
> today.
>
> BTW, in Linux, I find /proc/self/exe to be very useful.

Since I want to use OpenBSD which doesn't seem to have any of the methods
mentioned I guess I'll have to have a function that copies the name, and
require the user to call it from main. I do so hate forcing clutter on
applications' main. :-(.

Thanks for all the answers.

viza

Nate Eldredge

unread,
Oct 14, 2008, 2:30:40 PM10/14/08
to
viza <tom....@gm-il.com.obviouschange.invalid> writes:

OpenBSD does have an undocumented variable __progname, from looking at
the source. It's not in any of the header files so you could declare it
yourself:

extern char *__progname;

Of course, it could go away, etc, but at least you'd know at compile
time. It's possible but unlikely that they'd keep the variable but
change the meaning.

You might consider filing a feature request for OpenBSD to implement and
document the getprogname() interface to match other BSDs.


0 new messages