linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* argv[0]
@ 2006-01-12 23:16 a.biardi
  2006-01-13  9:52 ` argv[0] wwp
  2006-01-13 12:26 ` argv[0] Steve Graegert
  0 siblings, 2 replies; 6+ messages in thread
From: a.biardi @ 2006-01-12 23:16 UTC (permalink / raw)
  To: linux-c-programming


Hi,

Is there any C function that can tell me what argv[0] is, outside 
main()?

I thought to use getpid() and then look at /proc/<pid>/cmdline but 
doesn't seem portable. Any hints?

Thanks,
Andrea.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: argv[0]
  2006-01-12 23:16 argv[0] a.biardi
@ 2006-01-13  9:52 ` wwp
  2006-01-13 11:52   ` argv[0] a.biardi
  2006-01-13 12:26 ` argv[0] Steve Graegert
  1 sibling, 1 reply; 6+ messages in thread
From: wwp @ 2006-01-13  9:52 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 590 bytes --]

Hello Andrea,


On Fri, 13 Jan 2006 00:16:23 +0100 a.biardi@tiscali.it wrote:

> 
> Hi,
> 
> Is there any C function that can tell me what argv[0] is, outside 
> main()?
> 
> I thought to use getpid() and then look at /proc/<pid>/cmdline but 
> doesn't seem portable. Any hints?

For instance, in main(), you can store argc and argv into global const
pointers that you can use anywhere else.

Here's an example (see the attached files):

$ gcc main.c elsewhere.c
$ ./a.out

shows:
main: 1 0xbf8526f4 './a.out'
sample1: 1 0xbf8526f4 './a.out'
sample2: 1 0xbf8526f4 './a.out'


HTH,

-- 
wwp

[-- Attachment #2: elsewhere.c --]
[-- Type: text/x-csrc, Size: 168 bytes --]

#include <stdio.h>

extern int prg_argc;
extern char** prg_argv;

void outside_from_main_2(void)
{
	printf("sample2: %d %p '%s'\n", prg_argc, prg_argv, prg_argv[0]);
}

[-- Attachment #3: elsewhere.h --]
[-- Type: text/x-chdr, Size: 32 bytes --]

void outside_from_main_2(void);

[-- Attachment #4: main.c --]
[-- Type: text/x-csrc, Size: 341 bytes --]

#include <stdio.h>

int prg_argc=0;
char** prg_argv=NULL;

void outside_from_main_1(void)
{
	printf("sample1: %d %p '%s'\n", prg_argc, prg_argv, prg_argv[0]);
}

int main(int argc, char** argv)
{
	printf("main: %d %p '%s'\n", argc, argv, argv[0]);
	prg_argc=argc;
	prg_argv=argv;
	outside_from_main_1();
	outside_from_main_2();

	exit(0);
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: argv[0]
  2006-01-13  9:52 ` argv[0] wwp
@ 2006-01-13 11:52   ` a.biardi
  2006-01-13 12:19     ` argv[0] Neil Horman
  0 siblings, 1 reply; 6+ messages in thread
From: a.biardi @ 2006-01-13 11:52 UTC (permalink / raw)
  To: linux-c-programming

On Friday 13 January 2006 10:52, you wrote:

> Hello Andrea,
>
> On Fri, 13 Jan 2006 00:16:23 +0100 a.biardi@tiscali.it wrote:
> > Hi,
> >
> > Is there any C function that can tell me what argv[0] is, outside
> > main()?
> >
> > I thought to use getpid() and then look at /proc/<pid>/cmdline
> > but doesn't seem portable. Any hints?
>
> For instance, in main(), you can store argc and argv into global
> const pointers that you can use anywhere else.
>
> Here's an example (see the attached files):
>
> $ gcc main.c elsewhere.c
> $ ./a.out
>
> shows:
> main: 1 0xbf8526f4 './a.out'
> sample1: 1 0xbf8526f4 './a.out'
> sample2: 1 0xbf8526f4 './a.out'
>
>
> HTH,

Thanks, I know that. But I am developing a library, and have no access 
to the real main().

I could ask the user of my library to place a call to an init() 
function in main(), passing argv[0] along, but I don't like this 
solution very much.

Assuming I don't have access to main(), is there any other way to 
retrieve argv[0]?

Thanks,
Andrea.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: argv[0]
  2006-01-13 11:52   ` argv[0] a.biardi
@ 2006-01-13 12:19     ` Neil Horman
  0 siblings, 0 replies; 6+ messages in thread
From: Neil Horman @ 2006-01-13 12:19 UTC (permalink / raw)
  To: a.biardi; +Cc: linux-c-programming

On Fri, Jan 13, 2006 at 12:52:26PM +0100, a.biardi@tiscali.it wrote:
> On Friday 13 January 2006 10:52, you wrote:
> 
> > Hello Andrea,
> >
> > On Fri, 13 Jan 2006 00:16:23 +0100 a.biardi@tiscali.it wrote:
> > > Hi,
> > >
> > > Is there any C function that can tell me what argv[0] is, outside
> > > main()?
> > >
> > > I thought to use getpid() and then look at /proc/<pid>/cmdline
> > > but doesn't seem portable. Any hints?
> >
> > For instance, in main(), you can store argc and argv into global
> > const pointers that you can use anywhere else.
> >
> > Here's an example (see the attached files):
> >
> > $ gcc main.c elsewhere.c
> > $ ./a.out
> >
> > shows:
> > main: 1 0xbf8526f4 './a.out'
> > sample1: 1 0xbf8526f4 './a.out'
> > sample2: 1 0xbf8526f4 './a.out'
> >
> >
> > HTH,
> 
> Thanks, I know that. But I am developing a library, and have no access 
> to the real main().
> 
> I could ask the user of my library to place a call to an init() 
> function in main(), passing argv[0] along, but I don't like this 
> solution very much.
> 
> Assuming I don't have access to main(), is there any other way to 
> retrieve argv[0]?
> 
This probably isn't too much more portable than interrogating
/proc/<pid>/cmdline, but I expect that since argv is passed in on the stack to
main, you could probably compute the top of the applications stack, and find the
argv pointer that way.  You may have to make a few assumptions about the system
architecture, but it can be done.  Check out the backtrace function in glibc for
an example (at least on x86) of how finding the top of stack is done there.

Regards
Neil

> Thanks,
> Andrea.
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
/***************************************************
 *Neil Horman
 *Software Engineer
 *gpg keyid: 1024D / 0x92A74FA1 - http://pgp.mit.edu
 ***************************************************/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: argv[0]
  2006-01-12 23:16 argv[0] a.biardi
  2006-01-13  9:52 ` argv[0] wwp
@ 2006-01-13 12:26 ` Steve Graegert
  2006-02-24 13:59   ` argv[0] a.biardi
  1 sibling, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2006-01-13 12:26 UTC (permalink / raw)
  To: linux-c-programming

On 1/13/06, a.biardi@tiscali.it <a.biardi@tiscali.it> wrote:
>
> Hi,
>
> Is there any C function that can tell me what argv[0] is, outside
> main()?
>
> I thought to use getpid() and then look at /proc/<pid>/cmdline but
> doesn't seem portable. Any hints?

The arguments are pushed on the stack upon process creation.  Once the
process is running there is little chance to get hands on them except
for the /proc approach.

	\Steve

--

Steve Graegert <graegerts@gmail.com>
Software Consultant {C/C++ && Java && .NET}
Office: +49 9131 7123988
Mobile: +49 1520 9289212

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: argv[0]
  2006-01-13 12:26 ` argv[0] Steve Graegert
@ 2006-02-24 13:59   ` a.biardi
  0 siblings, 0 replies; 6+ messages in thread
From: a.biardi @ 2006-02-24 13:59 UTC (permalink / raw)
  To: linux-c-programming

On Friday 13 January 2006 13:26, you wrote:

> On 1/13/06, a.biardi@tiscali.it <a.biardi@tiscali.it> wrote:
> > Hi,
> >
> > Is there any C function that can tell me what argv[0] is, outside
> > main()?
> >
> > I thought to use getpid() and then look at /proc/<pid>/cmdline
> > but doesn't seem portable. Any hints?
>
> The arguments are pushed on the stack upon process creation.  Once
> the process is running there is little chance to get hands on them
> except for the /proc approach.
>
> 	\Steve
>

Just in case anybody needs it, I think I found something inside 
errno.h (GNU-only):

/* The full and simple forms of the name with which the program was
   invoked.  These variables are set up automatically at startup based
   on  the value of ARGV[0] (this works only if you use GNU ld). */
extern char *program_invocation_name, *program_invocation_short_name;¶

Bye,
Andrea.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-02-24 13:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-12 23:16 argv[0] a.biardi
2006-01-13  9:52 ` argv[0] wwp
2006-01-13 11:52   ` argv[0] a.biardi
2006-01-13 12:19     ` argv[0] Neil Horman
2006-01-13 12:26 ` argv[0] Steve Graegert
2006-02-24 13:59   ` argv[0] a.biardi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).