473,729 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4939
On Jun 25, 3:57 pm, mathieu <mathieu.malate ...@gmail.comwr ote:
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************ *************** *******@59g2000 hsb.googlegroup s.com>,
mathieu <ma************ ***@gmail.comwr ote:
>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.comwr ites:
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.progr ammer 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_Keit h) 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...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <9b3aaf8d-1b3e-41b6-890a-2fe493998...@59 g2000hsb.google groups.com>,

mathieu <mathieu.malate ...@gmail.comwr ote:
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.orgw rote:
mathieu <mathieu.malate ...@gmail.comwr ites:
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.progr ammer 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(cons t 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************ *************** *******@59g2000 hsb.googlegroup s.com>,
mathieu <ma************ ***@gmail.comwr ote:
>On Jun 25, 5:52 pm, Keith Thompson <ks...@mib.orgw rote:
><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.comwr ites:
On Jun 25, 5:05 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article
<9b3aaf8d-1b3e-41b6-890a-2fe493998...@59 g2000hsb.google groups.com>,

mathieu <mathieu.malate ...@gmail.comwr ote:
>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.progr ammer.

--
Keith Thompson (The_Other_Keit h) 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.orgw rote:
><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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1579
by: Steven Bethard | last post by:
This has probably been answered before, but my Google skills have failed me so far... Is there an os independent way of checking to see if a particular executable is on the path? Basically what I want to do is run code like: i, o, e = os.popen3(executable_name) but I'd like to give an informative error if 'executable_name' doesn't refer to an executable on the path. The idea is to differentiate between errors generated by not being...
5
61939
by: sathya moorthy via .NET 247 | last post by:
(Type your message here) hi. i am doing a small application project in c#. can any one help me how to find the directory of the current running program. how to create the input box in c# program. -------------------------------- From: sathya moorthy ----------------------- Posted by a user from .NET 247 (http://www.dotnet247.com/)
2
9650
by: Hayato Iriumi | last post by:
Hello, I have a need to kill a process. It sounds each enough, but the problem is that I have multiple executables running from different paths. Say, I have MyService.exe running from C:\FolderA and the same MyService.exe running in C:\FolderB. I want to kill the process that is running under C:\FolderA. So I guess I have a need to get the Process ID for the executable under C:\FolderA. How can I kill the process when you know the...
7
7991
by: Le, Thanh-Nhan | last post by:
Hi, How can I retrieve the path of the current application (written in C#)? In VB6 I can do it with property path of app object (app.path). Thanks Nhan
4
5465
by: bruno.fischel | last post by:
Hi, I was wondering if there was a way to know the absolute path of the library "currently running". For example, in my function foo, how can I get the path of the library containing the definition of foo? The library path will be different from the "current directory" (.) since the current directory will be the directpry of the prgram that loaded the lib; not the path to the library itself.
7
5077
by: gmax2006 | last post by:
Hi, I use RedHat linux. How can I find where exactly the current python script is running? I use this code: #test.py import os,sys
7
24787
by: patrik.kahari | last post by:
Is there a c++ function similar to getcwd that does not give you the working directory but the directory or path of the currently running executable? I need this because im porting a game that uses relative paths to images. This works fine when the game is executed directly from the directory its in (as in ./game &). It does not work when its executed from another location (as in /root/games/game &) Regards Patrik
4
3026
by: somank.sharma | last post by:
I am running an exe created in C. I need the full path (absolute path) of this exe. In the first argument of main, I am getting the exe name. How can I get the full path for this exe.
12
8599
by: =?ISO-8859-1?Q?Thomas_B=F8rlum?= | last post by:
Hey all, I'm writing a c++ program that needs to read a file. I'm trying to read a file that is in the same directory as the executable. Everything works fine if I execute the program while in the program's directory. What I need to do is read that file regardless of where (cwd) I execute the program from, without hardcoding the absolute since the program might be moved or be in differant locations on other computers.
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.