Path to the current running executable

Discussion in 'C Programming' started by mathieu, Jun 25, 2008.

  1. mathieu

    mathieu Guest

    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
    mathieu, Jun 25, 2008
    #1
    1. Advertising

  2. mathieu

    mathieu Guest

    On Jun 25, 3:57 pm, mathieu <> wrote:
    > 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
    mathieu, Jun 25, 2008
    #2
    1. Advertising

  3. In article <>,
    mathieu <> wrote:
    >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.
    Kenny McCormack, Jun 25, 2008
    #3
  4. mathieu <> writes:
    > 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) <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"
    Keith Thompson, Jun 25, 2008
    #4
  5. mathieu

    mathieu Guest

    On Jun 25, 5:05 pm, (Kenny McCormack)
    wrote:
    > In article <>,
    >
    > mathieu <> wrote:
    > >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
    mathieu, Jun 25, 2008
    #5
  6. mathieu

    mathieu Guest

    On Jun 25, 5:52 pm, Keith Thompson <> wrote:
    > mathieu <> writes:
    > > 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
    mathieu, Jun 25, 2008
    #6
  7. In article <>,
    mathieu <> wrote:
    >On Jun 25, 5:52 pm, Keith Thompson <> wrote:


    >> <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
    Walter Roberson, Jun 25, 2008
    #7
  8. mathieu <> writes:
    > On Jun 25, 5:05 pm, (Kenny McCormack)
    > wrote:
    > > In article
    > > <>,
    > >
    > > mathieu <> wrote:
    > > >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.

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Jun 25, 2008
    #8
  9. mathieu

    viza Guest

    Hi

    On Wed, 25 Jun 2008 10:14:30 -0700, mathieu wrote:
    > On Jun 25, 5:52 pm, Keith Thompson <> wrote:


    >> <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.
    viza, Jun 25, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James
    Replies:
    6
    Views:
    19,040
    Chris Riesbeck
    Oct 22, 2003
  2. Replies:
    15
    Views:
    465
    Dave Thompson
    Aug 21, 2006
  3. pratap
    Replies:
    20
    Views:
    740
    Flash Gordon
    Mar 7, 2007
  4. Jason Cavett
    Replies:
    8
    Views:
    596
    Sanjay
    May 16, 2007
  5. Replies:
    0
    Views:
    419
Loading...

Share This Page