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 9 4855
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
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.
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"
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
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
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
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"
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.
>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.) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
...
|
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...
|
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
|
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...
|
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
|
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...
|
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.
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |