linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: wwp <subscript@free.fr>
To: linux-c-programming@vger.kernel.org
Subject: Re: argv[0]
Date: Fri, 13 Jan 2006 10:52:44 +0100	[thread overview]
Message-ID: <20060113105244.53bda39b@localhost.localdomain> (raw)
In-Reply-To: <200601130016.23410.a.biardi@tiscali.it>

[-- 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);
}

  reply	other threads:[~2006-01-13  9:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-12 23:16 argv[0] a.biardi
2006-01-13  9:52 ` wwp [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060113105244.53bda39b@localhost.localdomain \
    --to=subscript@free.fr \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).