How can a binary get its working dir at run-time?

This is a discussion on How can a binary get its working dir at run-time? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by meili100 Thanks, but Code: /proc/self/exe -> /home/me/myproject/bin/myprog is a link. How to get the Code: /home/me/myproject/bin/myprog You ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by meili100 View Post
    Thanks, but
    Code:
    /proc/self/exe -> /home/me/myproject/bin/myprog
    is a link.
    How to get the
    Code:
    /home/me/myproject/bin/myprog
    You could always parse ls -l or something . . . .

    . . . or, if you can convince "open" or whatever not to follow symbolic links, you could just read the file directly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by meili100 View Post
    Thanks, but
    Code:
    /proc/self/exe -> /home/me/myproject/bin/myprog
    is a link.
    How to get the
    Code:
    /home/me/myproject/bin/myprog
    Ok, so you probably need to use opendir()/readdir()/closedir() to open/read/close the directory entry then. Unfortunately, you won't be able to spawn a process and use something like a "ls | awk ..." or "ls | sed ...", since you would then get the path to "ls", rather than your application, which I assume is what you are actually after [although I suppose the stuff is also in a numbered process /proc/<pid>/exe (or similar) directory, so I suppose that would be possible to use for such scripting tricks].

    Edit: Note that you would have to do (pseudo-code)
    Code:
    x = opendir("\proc\self");
    while(readdir(dirinfo))
    {
       if (dirinfo.fname == "exe") 
       {
          getlinkcontent();
          break;
       }
    }
    closedir(x);

    --
    Mats
    Last edited by matsp; 04-09-2008 at 05:53 PM. Reason: Clarify/example.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. Please help - Run time error
    By barkercs in forum C Programming
    Replies: 6
    Last Post: 05-03-2006, 11:15 AM
  3. Average asymptotic run time?
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-03-2005, 06:47 PM
  4. Run time error at client side query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-01-2005, 03:02 AM
  5. Replies: 2
    Last Post: 02-28-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21