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

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

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

    Say how to let a program A prints its binary location?
    Thanks.
    Last edited by meili100; 04-09-2008 at 05:05 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There's no real standard way of doing that, but argv[0] contains the name of the application, and in most shell's the path is included - however, that's no guarantee, as it's the code calling exec() [or one of it's close relatives] that is responsible for filling in argv[0].

    The only other option is to search in the path [which of course can also fail if the application was started with a path, but exec called without it, and the path does not contain the executable's directory].

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

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    Say how to let a program A prints its binary location?
    Thanks.
    getcwd() on UNIX, GetCurrentDirectory() on Windows.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    On windows you can use GetModuleFileName().

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    getcwd() on UNIX, GetCurrentDirectory() on Windows.
    That gives you the directory where the application is currently running, but not the location out of which the executable came.

    I agree that the question is not entirely clear. If I'm doing:
    Code:
    /home/me $ ls
    do you want to know the current directory (/home/me), or the /bin that is the executable /bin/ls's directory [and we're assuming that ls isn't built into the shell - which happens sometimes].

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

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Hmm -- I just realized the question is unclear. The subject line says "working dir" but in the body of your post you ask for the binary's location. Which one of these are you actually interested in?

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Does anybody know if there is a dirname() function?

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Sorry for the misleading, I mean the binary dir.
    In the "/home/me $ ls" case the output shall be /bin/ls

  9. #9

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    Sorry for the misleading, I mean the binary dir.
    In the "/home/me $ ls" case the output shall be /bin/ls
    In that case, the solution is messy and somewhat platform dependent. The value of argv[0] is a relative or absolute path to the binary itself. If it is absolute, then you already have your answer. If it is relative, then it is relative to the current directory, so you must get the curr dir and then walk upward until you reach the root of the filesystem to construct the full path.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    In that case, the solution is messy and somewhat platform dependent. The value of argv[0] is a relative or absolute path to the binary itself. If it is absolute, then you already have your answer. If it is relative, then it is relative to the current directory, so you must get the curr dir and then walk upward until you reach the root of the filesystem to construct the full path.
    And since argv[0] is provided by the caller to exec, not by the OS when it found the executable file, it's by no means guaranteed that the argv[0] is actually containing the path to the executable.

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

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    I found /proc/self/exe is linked to the binary location
    for example :
    /proc/self/exe -> /bin/ls

    But the question how to get the "/bin/ls" string?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A read from /proc/self/exe perhaps?

    Of coruse, if you want to do that with /bin/ls, then you need to recompile the source of /bin/ls - if you want to find '/home/me/myproject/bin/myprog', then you just need to open and read /proc/self/exe in your "myprog.c" or whatever your source file is.

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

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks, but
    Code:
    /proc/self/exe -> /home/me/myproject/bin/myprog
    is a link.
    How to get the
    Code:
    /home/me/myproject/bin/myprog

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    And since argv[0] is provided by the caller to exec, not by the OS when it found the executable file, it's by no means guaranteed that the argv[0] is actually containing the path to the executable.

    --
    Mats
    Very true, although in most cases it will be correct.

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, 12:40 PM