Path to the current running executable

M

mathieu

I'd like to be able to get the path to the current working executable
(from inside it).

Technically this is easy, I simply have to collapse: getcwd and
argv[0]

Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Thanks
-Mathieu
 
M

mathieu

I'd like to be able to get the path to the current working executable
(from inside it).

Technically this is easy, I simply have to collapse: getcwd and
argv[0]

Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Thanks
-Mathieu

Ok, I'll rtfm myself:

[1.14 How can I find a process' executable file?]
http://www.faqs.org/faqs/unix-faq/programmer/faq/


-M
 
K

Kenny McCormack

I'd like to be able to get the path to the current working executable
(from inside it).

Technically this is easy, I simply have to collapse: getcwd and
argv[0]

Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Um, how do you have code running before main() is called?

Note: I'm not saying it is impossible; I'm actually curious about
how/what you are doing. But, of course, in the CLC context, they'll all
tell you that it is off-topic, blah, blah, blah.
 
K

Keith Thompson

mathieu said:
I'd like to be able to get the path to the current working executable
(from inside it).

Technically this is easy, I simply have to collapse: getcwd and
argv[0]

Note that getcwd() is not defined by the C standard. It is defined by
POSIX, which suggests that comp.unix.programmer might be a better
place for your question. <OT>And in Unix systems, argv[0] doesn't
*necessarily* hold valid information; it can be set by the
caller. said:
Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Let me guess, you want to do something like:

const char *const executable_name = <something>;

outside any function. There's certainly no standard C way to do that
(unless you use a macro whose value is specified when you compile,
but then the value won't change if the executable is moved somewhere
else). And I don't think you'll find any system-specific way to do
it either, particularly since a static initializer must be constant
-- but you'll have to ask in a system-specific forum to be sure.

(Strictly speaking, specifying a macro value at compilation time,
such as with a "-DFOO=BAR" compiler option, is also non-standard,
but you could do the same thing by updating a header file containing
a #define just before compiling.)

If you want this in a static variable at file scope (a "global"),
then your best bet is probably to declare the variable and assign
a value to it at the beginning of main().
 
M

mathieu

mathieu said:
I'd like to be able to get the path to the current working executable
(from inside it).
Technically this is easy, I simply have to collapse: getcwd and
argv[0]
Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Um, how do you have code running before main() is called?

A piece of code is better than a long explanation:

http://gdcm.svn.sourceforge.net/viewvc/gdcm/trunk/Source/DataDictionary/gdcmGlobal.cxx?view=markup

I think this is call the singleton pattern and must be describe in
stroustrup c++ AFAIK.
Note: I'm not saying it is impossible; I'm actually curious about
how/what you are doing. But, of course, in the CLC context, they'll all
tell you that it is off-topic, blah, blah, blah.

np, let me know if you have any question, this is actually a pretty
nice trick to know.

-Mathieu
 
M

mathieu

mathieu said:
I'd like to be able to get the path to the current working executable
(from inside it).
Technically this is easy, I simply have to collapse: getcwd and
argv[0]

Note that getcwd() is not defined by the C standard. It is defined by
POSIX, which suggests that comp.unix.programmer might be a better
place for your question. *

I understand, but I least in the limited set of platforms I am
supporting (Win32, *NIX, MaxOSX), I can work my way with getcwd &
_getcwd
<OT>And in Unix systems, argv[0] doesn't
*necessarily* hold valid information; it can be set by the
caller.</OT>

I vaguely remember that, but isn't it consider a bug ? I am hoping
that compiler released the last 10 years do not suffer this.
Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Let me guess, you want to do something like:

const char *const executable_name = <something>;

outside any function. There's certainly no standard C way to do that
(unless you use a macro whose value is specified when you compile,
but then the value won't change if the executable is moved somewhere
else).

As I explained in my previous post I used what I called the singleton
pattern. AFAIK this is portable and by design garantee to work.
And I don't think you'll find any system-specific way to do
it either, particularly since a static initializer must be constant
-- but you'll have to ask in a system-specific forum to be sure.

Correct. That's why I use a 'unsigned int' which in turn initialize a
class. I know this is C++, but my initial question was mostly C...
(Strictly speaking, specifying a macro value at compilation time,
such as with a "-DFOO=BAR" compiler option, is also non-standard,
but you could do the same thing by updating a header file containing
a #define just before compiling.)

If you want this in a static variable at file scope (a "global"),
then your best bet is probably to declare the variable and assign
a value to it at the beginning of main().

Actually this only work from an executable. Even if I setup my
singleton to have an initialize(const char*) member function, I'll
still have other problem. For instance the API of the lib is wrap in
python, so a main function do not make a lot of sense there ...

I am able to get something working from the build tree (using rpath +
relative path to source directory) and from hard-coded install tree.
But not from a tarbll that user could extract anywhere...

Thanks anyway, that was informative
-Mathieu
 
W

Walter Roberson

<OT>And in Unix systems, argv[0] doesn't
*necessarily* hold valid information; it can be set by the
caller.</OT>
I vaguely remember that, but isn't it consider a bug ? I am hoping
that compiler released the last 10 years do not suffer this.

It isn't a compiler issue, it is an operating system issue.
The POSIX exec*() calls -all- take an executable path name distinct
from the arguments to be placed in argv. The historical question was
whether an executable could change, at run-time, what was reported by 'ps',
by altering the argv[] parameters as it ran.
 
K

Keith Thompson

mathieu said:
mathieu said:
I'd like to be able to get the path to the current working executable
(from inside it).
Technically this is easy, I simply have to collapse: getcwd and
argv[0]
Well argv[0] comes in a little late, I'd like to have access to this
information before the 'main' function is called. Is there a way to
get the path to an executable (from inside it) ?

Um, how do you have code running before main() is called?

A piece of code is better than a long explanation:

http://gdcm.svn.sourceforge.net/viewvc/gdcm/trunk/Source/DataDictionary/gdcmGlobal.cxx?view=markup

I think this is call the singleton pattern and must be describe in
stroustrup c++ AFAIK.

That's C++, not C. C++ has classes with constructors, allowing code
to be executed before main() starts. C, the topic of this newsgroup,
does not.

If you want to discuss C++ constructors, try comp.lang.c++. If you
want to discuss getcwd() and so forth, try comp.unix.programmer.
 
V

viza

Hi

<OT>And in Unix systems, argv[0] doesn't *necessarily* hold valid
information; it can be set by the caller.</OT>

I vaguely remember that, but isn't it consider a bug ? I am hoping that
compiler released the last 10 years do not suffer this.

Absolutely not! This is used very deliberately in all sorts of brand-new
applications for a variety of reasons. There is no possibility of this
going away on *nix like systems in the foreseeable future.

Remember that on *nix, once a file is opened it no longer necessarily has
a name at all, and that it can have multiple completely equivalent names
(hard links). It is common practice to hard link an executable to
multiple names and have the application perform differently based upon
which name it is given as argv[0], which is conventionally set to the
basename of the link that was executed.

In this way argv[0] is no different to argv[1] or any other member of the
argument array, it can have any value that the parent process or user
chooses to give it. The only member that which has any firm restriction
placed upon it is argv[argc], which must be NULL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top