469,935 Members | 1,469 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

Path to the current running executable

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
Jun 27 '08 #1
9 4643
On Jun 25, 3:57 pm, mathieu <mathieu.malate...@gmail.comwrote:
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
Jun 27 '08 #2
In article <9b**********************************@59g2000hsb.g ooglegroups.com>,
mathieu <ma***************@gmail.comwrote:
>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.

Jun 27 '08 #3
mathieu <ma***************@gmail.comwrites:
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.</OT>
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().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4
On Jun 25, 5:05 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <9b3aaf8d-1b3e-41b6-890a-2fe493998...@59g2000hsb.googlegroups.com>,

mathieu <mathieu.malate...@gmail.comwrote:
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/view...xx?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
Jun 27 '08 #5
On Jun 25, 5:52 pm, Keith Thompson <ks...@mib.orgwrote:
mathieu <mathieu.malate...@gmail.comwrites:
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
Jun 27 '08 #6
In article <42**********************************@59g2000hsb.g ooglegroups.com>,
mathieu <ma***************@gmail.comwrote:
>On Jun 25, 5:52 pm, Keith Thompson <ks...@mib.orgwrote:
><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.

--
"The Romans believed that every man had his Genius, and every
woman her Juno." -- Thomas Bulfinch
Jun 27 '08 #7
mathieu <ma***************@gmail.comwrites:
On Jun 25, 5:05 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article
<9b3aaf8d-1b3e-41b6-890a-2fe493998...@59g2000hsb.googlegroups.com>,

mathieu <mathieu.malate...@gmail.comwrote:
>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/view...xx?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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
Hi

On Wed, 25 Jun 2008 10:14:30 -0700, mathieu wrote:
On Jun 25, 5:52 pm, Keith Thompson <ks...@mib.orgwrote:
><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.

Jun 27 '08 #9
>I'd like to be able to get the path to the current working executable
>(from inside it).
*THE* path? Is that anything like pressing *THE* key on *THE*
keyboard connected to *THE* computer?
>Technically this is easy, I simply have to collapse: getcwd and
argv[0]
That is unlikely to ever work. The executable is usually not in
the current working directory, but is more likely in a system
directory for executables (e.g. /bin or /usr/bin or $HOME/bin ).
UNIX/Posix shells search components of the environment variable
PATH (colon-separated paths) for where to find executables.

On a UNIX/Posix system, argv[0] may be an arbitrary (and malicious!)
string unrelated to any of the names of the executable.
>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) ?
If there is even the slightest security interrest by evildoers in
having you find the wrong executable, don't use argv[0] to find a
program to execute.

Isn't the "singleton pattern" something that makes the rather
short-sighted assumption that there is only one of something, and
more can't possibly exist? One and only one database, One and only
one nameserver, One and only one user, etc.? (There should at least
be TEST and PRODUCTION of just about any object.)

Jul 4 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by sathya moorthy via .NET 247 | last post: by
2 posts views Thread by Hayato Iriumi | last post: by
7 posts views Thread by Le, Thanh-Nhan | last post: by
7 posts views Thread by patrik.kahari | last post: by
4 posts views Thread by somank.sharma | last post: by
12 posts views Thread by =?ISO-8859-1?Q?Thomas_B=F8rlum?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.