Path to the current running executable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mathieu

    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
  • mathieu

    #2
    Re: Path to the current running executable

    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?]



    -M

    Comment

    • Kenny McCormack

      #3
      Re: Path to the current running executable

      In article <9b3aaf8d-1b3e-41b6-890a-2fe493998ed0@59 g2000hsb.google groups.com>,
      mathieu <mathieu.malate rre@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.

      Comment

      • Keith Thompson

        #4
        Re: Path to the current running executable

        mathieu <mathieu.malate rre@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) kst-u@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"

        Comment

        • mathieu

          #5
          Re: Path to the current running executable

          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:

          Download Grassroots DICOM for free. Cross-platform DICOM implementation. Grassroots DiCoM is a C++ library for DICOM medical files. It is accessible from Python, C#, Java and PHP.


          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

          Comment

          • mathieu

            #6
            Re: Path to the current running executable

            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

            Comment

            • Walter Roberson

              #7
              Re: Path to the current running executable

              In article <4265df18-f89f-4e55-8283-963a68243f73@59 g2000hsb.google groups.com>,
              mathieu <mathieu.malate rre@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

              Comment

              • Keith Thompson

                #8
                Re: Path to the current running executable

                mathieu <mathieu.malate rre@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:
                >
                Download Grassroots DICOM for free. Cross-platform DICOM implementation. Grassroots DiCoM is a C++ library for DICOM medical files. It is accessible from Python, C#, Java and PHP.

                >
                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) kst-u@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"

                Comment

                • viza

                  #9
                  Re: Path to the current running executable

                  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.

                  Comment

                  • Gordon Burditt

                    #10
                    Re: Path to the current running executable

                    >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.)

                    Comment

                    Working...