get full path of application

Discussion in 'C++' started by rh00667, Mar 6, 2007.

  1. rh00667

    rh00667 Guest

    hi all,

    i'm confused now. how i can get the full path of an application?

    if myapp is in a directory which belongs to PATH, argv[0] gives me the
    first token of cmd line, and not the real path of the executed
    program.

    so if i write anywhere:

    % myapp

    it runs ok from the file (for example) /usr/local/bin/myapp

    but argv[0] returns me

    ../myapp

    but i need

    /usr/local/bin/myapp

    thanks in advance
     
    rh00667, Mar 6, 2007
    #1
    1. Advertising

  2. rh00667

    Rolf Magnus Guest

    rh00667 wrote:

    > hi all,
    >
    > i'm confused now. how i can get the full path of an application?


    You can't.
     
    Rolf Magnus, Mar 6, 2007
    #2
    1. Advertising

  3. rh00667

    rh00667 Guest

    On Mar 6, 2:41 pm, Rolf Magnus <> wrote:

    > You can't.


    wow! any hint?

    or must i navegate PATH finding the first pathdir/myapp ??????
     
    rh00667, Mar 6, 2007
    #3
  4. rh00667

    rh00667 Guest

    On Mar 6, 2:41 pm, Rolf Magnus <> wrote:

    > You can't.


    wow! any hint?

    or must i navegate PATH finding the first pathdir/myapp ??????
     
    rh00667, Mar 6, 2007
    #4
  5. rh00667

    Kai-Uwe Bux Guest

    rh00667 wrote:

    > On Mar 6, 2:41 pm, Rolf Magnus <> wrote:
    >
    >> You can't.

    >
    > wow! any hint?


    It's compiler/OS specific. Standard C++ has no means of getting the path of
    the executable. You will need to use some platform specific code. What that
    code looks like, is a question you will have to ask in a forum for your
    particular compiler/OS.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 6, 2007
    #5
  6. rh00667

    Jerry Coffin Guest

    In article <>,
    says...
    > On Mar 6, 2:41 pm, Rolf Magnus <> wrote:
    >
    > > You can't.

    >
    > wow! any hint?


    Sure: any real answer is platform specific, so you need to ask where
    your platform is topical.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Mar 6, 2007
    #6
  7. rh00667

    Guest

    On Mar 6, 8:16 am, "rh00667" <> wrote:
    > hi all,
    >
    > i'm confused now. how i can get the full path of an application?
    >
    > if myapp is in a directory which belongs to PATH, argv[0] gives me the
    > first token of cmd line, and not the real path of the executed
    > program.
    >
    > so if i write anywhere:
    >
    > % myapp
    >
    > it runs ok from the file (for example) /usr/local/bin/myapp
    >
    > but argv[0] returns me
    >
    > ./myapp
    >
    > but i need
    >
    > /usr/local/bin/myapp
    >
    > thanks in advance

    Of course this is platform specific but the issue is common across
    platforms and any solutions would be useful across platforms. Here's
    what we do (which has nothing to do with C++ even though the apps
    are written in C++). Note that most of this stuff can be done in C++
    but it just seems easier to use a script since one is needed for
    launching the apps for other reasons.

    1. Start your applications via a script and have it know specifically
    where your application lives relative to where the script lives.

    2. Detect the full pathname of where your script lives. Here's a
    simple bash shell function which sets a variable INSDIR to the
    full pathname of where the script lives. The function allows
    the script to be started from any directory.

    install_directory ()
    {
    if [ `printf '%c' $0` == "/" ]; then
    INSDIR=`dirname $0`;
    else
    INSDIR=`pwd`"/"`dirname $0`;
    fi
    }

    3. Launch your application with a full pathname by manipulating the
    full path name of your script and the relative path of your
    executable
    to get the full patha name of your excutable.

    Hope that helps.
     
    , Mar 6, 2007
    #7
  8. rh00667

    Guest

    On Mar 6, 11:10 am, "" <>
    wrote:
    > On Mar 6, 8:16 am, "rh00667" <> wrote:
    >
    > > hi all,

    >
    > > i'm confused now. how i can get the full path of an application?

    >
    > > if myapp is in a directory which belongs to PATH, argv[0] gives me the
    > > first token of cmd line, and not the real path of the executed
    > > program.

    >
    > > so if i write anywhere:

    >
    > > % myapp

    >
    > > it runs ok from the file (for example) /usr/local/bin/myapp

    >
    > > but argv[0] returns me

    >
    > > ./myapp

    >
    > > but i need

    >
    > > /usr/local/bin/myapp

    >
    > > thanks in advance

    >
    > Of course this is platform specific but the issue is common across
    > platforms and any solutions would be useful across platforms. Here's
    > what we do (which has nothing to do with C++ even though the apps
    > are written in C++). Note that most of this stuff can be done in C++
    > but it just seems easier to use a script since one is needed for
    > launching the apps for other reasons.
    >
    > 1. Start your applications via a script and have it know specifically
    > where your application lives relative to where the script lives.
    >
    > 2. Detect the full pathname of where your script lives. Here's a
    > simple bash shell function which sets a variable INSDIR to the
    > full pathname of where the script lives. The function allows
    > the script to be started from any directory.
    >
    > install_directory ()
    > {
    > if [ `printf '%c' $0` == "/" ]; then
    > INSDIR=`dirname $0`;
    > else
    > INSDIR=`pwd`"/"`dirname $0`;
    > fi
    >
    > }
    >
    > 3. Launch your application with a full pathname by manipulating the
    > full path name of your script and the relative path of your
    > executable
    > to get the full patha name of your excutable.
    >
    > Hope that helps.


    You could also traverse the PATH environment variable, but you will
    have to know something about the OS you are using.

    If you are using UNIX, or Windoze with MiNGW, or Cygwin, you could
    call the command which the argv[0] as an argument. But then you would
    have to fork with a pipe to set its stdout to which can be a pain, or
    you can use system() and redirect it to a named pipe which you can
    read from more easliy. i.e. system("which prog > namedPipe").


    Adrian
     
    , Mar 6, 2007
    #8
  9. rh00667

    rh00667 Guest

    thank to all for comments and ideas!

    i tested this for redhat4:

    pid = getpid();
    sprintf(proc_exe, "/proc/%d/exe", pid);
    nr = readlink(proc_exe, buff, BUFFSIZE);
    buff[nr]=0;
    return buff;

    (defs, checks, cosmetics are not shown)

    tks!
     
    rh00667, Mar 6, 2007
    #9
  10. rh00667

    Guest

    On Mar 6, 2:20 pm, "rh00667" <> wrote:
    > thank to all for comments and ideas!
    >
    > i tested this for redhat4:
    >
    > pid = getpid();
    > sprintf(proc_exe, "/proc/%d/exe", pid);
    > nr = readlink(proc_exe, buff, BUFFSIZE);
    > buff[nr]=0;
    > return buff;
    >
    > (defs, checks, cosmetics are not shown)
    >
    > tks!


    Very proprietary, will work on only LINUX and I don't even know if it
    will work on all of them. :)


    Adrian
     
    , Mar 6, 2007
    #10
  11. rh00667

    rh00667 Guest

    > Very proprietary, will work on only LINUX and I don't even know if it
    > will work on all of them. :)


    yes, of course, i'm agree with you. as you saix, it is not standard
    form. i decided to test in my enviroment, and in future make the
    "select case" for each os included ....

    tks!
     
    rh00667, Mar 6, 2007
    #11
  12. rh00667

    rh00667 Guest

    > yes, of course, i'm agree with you. as you saiD, it is not standard
    > form. i decided to test in my enviroment, and in future make the
    > "select case" for each os included ....


    a bit simpler, sent by a friend. he tested in CentOs, Debian, RHEL,
    Knoppix, Ubuntu, ...

    length = readlink("/proc/self/exe", _pwd, _MAXPATH);
    _pwd[length] = '\0';
     
    rh00667, Mar 7, 2007
    #12
    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. PHaroZ
    Replies:
    5
    Views:
    94,828
    akash02sap
    Aug 17, 2012
  2. James
    Replies:
    2
    Views:
    13,925
    Andrew Thompson
    Dec 7, 2004
  3. Benjamin Han
    Replies:
    7
    Views:
    488
    Hartmut Goebel
    Jan 3, 2004
  4. xeroxero

    Get Current Full App Path?

    xeroxero, Dec 8, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    713
    Juan T. Llibre
    Dec 8, 2006
  5. dude
    Replies:
    5
    Views:
    1,717
    Jussi Piitulainen
    Mar 17, 2011
Loading...

Share This Page