digitalmars.D - Finding the path to a executable?
- Alan (10/10) Aug 06 2013 Hello! This may seem like a simple question, maybe an
- Andre Artus (5/15) Aug 06 2013 Is this what you are looking for?
- Alan (2/20) Aug 06 2013 No sorry, I meant how to find the directory in D code.
- Andre Artus (2/24) Aug 06 2013 Sorry, I misunderstood.
- Alan (2/27) Aug 06 2013 You're fine! Thanks for trying though!
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (11/21) Aug 06 2013 First program argument is the absolute path to the executable. (I am not...
- Alan (4/33) Aug 06 2013 Just missed your response! And no that doesn't fetch the
- evilrat (12/22) Aug 06 2013 the problem with this that its not absolute path, it is whatever
- evilrat (11/21) Aug 06 2013 you mean absolute path at runtime? this is passed as args[0] in
- Alan (7/33) Aug 06 2013 Sorry about the section mishap, I'm new here!
- evilrat (7/12) Aug 06 2013 putting all together try this(not tested)
- Andre Artus (3/15) Aug 06 2013 Works for me.
- Alan (4/16) Aug 06 2013 I suppose that could work with a few modifications for
- Kapps (6/18) Aug 06 2013 I'd imagine (but I could be mistaken here) that that uses the
- evilrat (4/25) Aug 06 2013 yes it probably may return wrong path when messed up with setcwd,
- Manfred Nowak (4/5) Aug 07 2013 Which theory? Especially in heteromorphic systems there is no such thing...
- Kapps (5/15) Aug 06 2013 The approach that I use is http://pastie.org/8214264 - The
- Tommi (59/69) Aug 07 2013 This should work on at least couple of systems, but I haven't
- Tommi (1/1) Aug 07 2013 ...sorry, add import std.path; if you need the last two functions.
- Nick Sabalausky (7/86) Aug 07 2013 Yes, this is the way to do it. Never use args[0]: it's useless as it
- H. S. Teoh (15/103) Aug 07 2013 +1. If you invoke your program from the shell, it will generally be
- Nick Sabalausky (6/13) Aug 10 2013 Hmm, interesting tool. And you're right, I can't think of a better way
- Nick Sabalausky (6/23) Aug 10 2013 Pull request:
- Jacob Carlborg (5/15) Aug 07 2013 There's a pull request for that:
Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!
Aug 06 2013
On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!Is this what you are looking for? find ~/projects/program/ -type f -perm +111 or ls -d -1 ~/projects/program/*
Aug 06 2013
On Wednesday, 7 August 2013 at 06:08:44 UTC, Andre Artus wrote:On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:No sorry, I meant how to find the directory in D code.Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!Is this what you are looking for? find ~/projects/program/ -type f -perm +111 or ls -d -1 ~/projects/program/*
Aug 06 2013
On Wednesday, 7 August 2013 at 06:10:35 UTC, Alan wrote:On Wednesday, 7 August 2013 at 06:08:44 UTC, Andre Artus wrote:Sorry, I misunderstood.On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:No sorry, I meant how to find the directory in D code.Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!Is this what you are looking for? find ~/projects/program/ -type f -perm +111 or ls -d -1 ~/projects/program/*
Aug 06 2013
On Wednesday, 7 August 2013 at 06:25:26 UTC, Andre Artus wrote:On Wednesday, 7 August 2013 at 06:10:35 UTC, Alan wrote:You're fine! Thanks for trying though!On Wednesday, 7 August 2013 at 06:08:44 UTC, Andre Artus wrote:Sorry, I misunderstood.On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:No sorry, I meant how to find the directory in D code.Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!Is this what you are looking for? find ~/projects/program/ -type f -perm +111 or ls -d -1 ~/projects/program/*
Aug 06 2013
On 08/06/2013 10:31 PM, Alan wrote:Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!First program argument is the absolute path to the executable. (I am not sure whether this is portable.) import std.stdio; import std.path; void main(string[] args) { writefln("I am program '%s' in '%s'.", baseName(args[0]), dirName(args[0])); } Ali
Aug 06 2013
On Wednesday, 7 August 2013 at 06:10:16 UTC, Ali Çehreli wrote:On 08/06/2013 10:31 PM, Alan wrote:Just missed your response! And no that doesn't fetch the absolute path, just the relative path. Thanks for help so far though!Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!First program argument is the absolute path to the executable. (I am not sure whether this is portable.) import std.stdio; import std.path; void main(string[] args) { writefln("I am program '%s' in '%s'.", baseName(args[0]), dirName(args[0])); } Ali
Aug 06 2013
On Wednesday, 7 August 2013 at 06:10:16 UTC, Ali Çehreli wrote:First program argument is the absolute path to the executable. (I am not sure whether this is portable.) import std.stdio; import std.path; void main(string[] args) { writefln("I am program '%s' in '%s'.", baseName(args[0]), dirName(args[0])); } Alithe problem with this that its not absolute path, it is whatever string you started your app, i.e. lets assume ~/location/program is executable, and we call it -- cd ~/location cd ../ ./location/prog --- args[0] would contaion ./location/prog but combine it with cwd and this would be (always?) the exact program path
Aug 06 2013
On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!you mean absolute path at runtime? this is passed as args[0] in main(string[] args). though it contains not path but string used to launch ur program( for example "./program" when launched from its location, and full program path when double-clicked). also you can use getcwd() from std.path. but you can't rely on it as both of this takes "caller" location. may be this helps you : http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx http://man7.org/linux/man-pages/man3/getcwd.3.html p.s. such question better ask in learn section
Aug 06 2013
On Wednesday, 7 August 2013 at 06:13:59 UTC, evilrat wrote:On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Sorry about the section mishap, I'm new here! Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. Thanks for all the help so far!Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!you mean absolute path at runtime? this is passed as args[0] in main(string[] args). though it contains not path but string used to launch ur program( for example "./program" when launched from its location, and full program path when double-clicked). also you can use getcwd() from std.path. but you can't rely on it as both of this takes "caller" location. may be this helps you : http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx http://man7.org/linux/man-pages/man3/getcwd.3.html p.s. such question better ask in learn section
Aug 06 2013
On Wednesday, 7 August 2013 at 06:18:26 UTC, Alan wrote:Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. Thanks for all the help so far!putting all together try this(not tested) import std.path; void main(string[] args) { writeln(absolutePath(buildNormalizedPath(args[0]))); }
Aug 06 2013
On Wednesday, 7 August 2013 at 06:22:25 UTC, evilrat wrote:On Wednesday, 7 August 2013 at 06:18:26 UTC, Alan wrote:import std.stdio;Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. Thanks for all the help so far!putting all together try this(not tested) import std.path;void main(string[] args) { writeln(absolutePath(buildNormalizedPath(args[0]))); }Works for me.
Aug 06 2013
On Wednesday, 7 August 2013 at 06:22:25 UTC, evilrat wrote:On Wednesday, 7 August 2013 at 06:18:26 UTC, Alan wrote:I suppose that could work with a few modifications for consistency. THanks for the help everyone!Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. Thanks for all the help so far!putting all together try this(not tested) import std.path; void main(string[] args) { writeln(absolutePath(buildNormalizedPath(args[0]))); }
Aug 06 2013
On Wednesday, 7 August 2013 at 06:22:25 UTC, evilrat wrote:On Wednesday, 7 August 2013 at 06:18:26 UTC, Alan wrote:I'd imagine (but I could be mistaken here) that that uses the current working directory as the base. I've had issues with this in the old std.path where the CWD is different from the program when starting it up (possibly related to shortcuts, or perhaps just the IDE setting it manually).Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. Thanks for all the help so far!putting all together try this(not tested) import std.path; void main(string[] args) { writeln(absolutePath(buildNormalizedPath(args[0]))); }
Aug 06 2013
On Wednesday, 7 August 2013 at 06:38:29 UTC, Kapps wrote:On Wednesday, 7 August 2013 at 06:22:25 UTC, evilrat wrote:yes it probably may return wrong path when messed up with setcwd, but when taken at startup this in theory should always return actual program path.On Wednesday, 7 August 2013 at 06:18:26 UTC, Alan wrote:I'd imagine (but I could be mistaken here) that that uses the current working directory as the base. I've had issues with this in the old std.path where the CWD is different from the program when starting it up (possibly related to shortcuts, or perhaps just the IDE setting it manually).Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. Thanks for all the help so far!putting all together try this(not tested) import std.path; void main(string[] args) { writeln(absolutePath(buildNormalizedPath(args[0]))); }
Aug 06 2013
evilrat wrote:in theory should always return actual program pathWhich theory? Especially in heteromorphic systems there is no such thing like a unique path between two accessible "points" in a file system. -manfred
Aug 07 2013
On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!The approach that I use is http://pastie.org/8214264 - The implementation probably has a fair few flaws though in regards to buffer sizes (and only supports Windows / Linux). It's worked in the basic situations I've tried though.
Aug 06 2013
On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!This should work on at least couple of systems, but I haven't tested this on other than Mac. Also, it's not completely robust, because paths could be longer than 4096 chars. version(Windows) { import std.c.windows.windows; } else version(OSX) { private extern(C) int _NSGetExecutablePath(char* buf, uint* bufsize); } else version(linux) { import std.c.linux.linux; } else { static assert(0); } import std.conv; // Returns the full path to the currently running executable string executablePath() { static string cachedExecutablePath; if (!cachedExecutablePath) { char[4096] buf; uint filePathLength; version(Windows) { filePathLength = GetModuleFileNameA(null, buf.ptr, buf.length - 1); assert(filePathLength != 0); } else version(OSX) { filePathLength = cast(uint) (buf.length - 1); int res = _NSGetExecutablePath(buf.ptr, &filePathLength); assert(res == 0); } else version(linux) { filePathLength = readlink(toStringz(selfExeLink), buf.ptr, buf.length - 1); } else { static assert(0); } cachedExecutablePath = to!string(buf[0 .. filePathLength]); } return cachedExecutablePath; } // Returns the file name of the currently running executable string executableName() { return executablePath().baseName(); } // Returns the path to the directory of the currently running executable string executableDirPath() { return executablePath().dirName(); }
Aug 07 2013
...sorry, add import std.path; if you need the last two functions.
Aug 07 2013
On Wed, 07 Aug 2013 09:13:03 +0200 "Tommi" <tommitissari hotmail.com> wrote:On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Yes, this is the way to do it. Never use args[0]: it's useless as it doesn't follow symlinks (might not even follow aliases, though I'm not certain), and strictly-speaking can't even be guaranteed to be correct at all anyway. If it were up to me, args[0] would be eliminated outright.Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!This should work on at least couple of systems, but I haven't tested this on other than Mac. Also, it's not completely robust, because paths could be longer than 4096 chars. version(Windows) { import std.c.windows.windows; } else version(OSX) { private extern(C) int _NSGetExecutablePath(char* buf, uint* bufsize); } else version(linux) { import std.c.linux.linux; } else { static assert(0); } import std.conv; // Returns the full path to the currently running executable string executablePath() { static string cachedExecutablePath; if (!cachedExecutablePath) { char[4096] buf; uint filePathLength; version(Windows) { filePathLength = GetModuleFileNameA(null, buf.ptr, buf.length - 1); assert(filePathLength != 0); } else version(OSX) { filePathLength = cast(uint) (buf.length - 1); int res = _NSGetExecutablePath(buf.ptr, &filePathLength); assert(res == 0); } else version(linux) { filePathLength = readlink(toStringz(selfExeLink), buf.ptr, buf.length - 1); } else { static assert(0); } cachedExecutablePath = to!string(buf[0 .. filePathLength]); } return cachedExecutablePath; } // Returns the file name of the currently running executable string executableName() { return executablePath().baseName(); } // Returns the path to the directory of the currently running executable string executableDirPath() { return executablePath().dirName(); }
Aug 07 2013
On Wed, Aug 07, 2013 at 12:55:10PM -0400, Nick Sabalausky wrote:On Wed, 07 Aug 2013 09:13:03 +0200 "Tommi" <tommitissari hotmail.com> wrote:+1. If you invoke your program from the shell, it will generally be correct (though unhelpful if the program was found through $PATH, because the shell only passes the bare program name in argv[0], not the full path, leaving you having to search through $PATH all over again -- and even then, there's no guarantee the shell wasn't configured NOT to pass $PATH along: some security models recommend not doing so). But if your program was invoked from another program, there's no guarantee at all what args[0] contains. For all you know, it could be "/path/to/rootkit/maliciousProgram.exe".On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:Yes, this is the way to do it. Never use args[0]: it's useless as it doesn't follow symlinks (might not even follow aliases, though I'm not certain), and strictly-speaking can't even be guaranteed to be correct at all anyway.Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!This should work on at least couple of systems, but I haven't tested this on other than Mac. Also, it's not completely robust, because paths could be longer than 4096 chars. version(Windows) { import std.c.windows.windows; } else version(OSX) { private extern(C) int _NSGetExecutablePath(char* buf, uint* bufsize); } else version(linux) { import std.c.linux.linux; } else { static assert(0); } import std.conv; // Returns the full path to the currently running executable string executablePath() { static string cachedExecutablePath; if (!cachedExecutablePath) { char[4096] buf; uint filePathLength; version(Windows) { filePathLength = GetModuleFileNameA(null, buf.ptr, buf.length - 1); assert(filePathLength != 0); } else version(OSX) { filePathLength = cast(uint) (buf.length - 1); int res = _NSGetExecutablePath(buf.ptr, &filePathLength); assert(res == 0); } else version(linux) { filePathLength = readlink(toStringz(selfExeLink), buf.ptr, buf.length - 1); } else { static assert(0); } cachedExecutablePath = to!string(buf[0 .. filePathLength]); } return cachedExecutablePath; } // Returns the file name of the currently running executable string executableName() { return executablePath().baseName(); } // Returns the path to the directory of the currently running executable string executableDirPath() { return executablePath().dirName(); }If it were up to me, args[0] would be eliminated outright.Naw, there are some valid use cases for it. Take a look at busybox, for example. :) T -- Не дорог подарок, дорога любовь.
Aug 07 2013
On Wed, 7 Aug 2013 10:06:19 -0700 "H. S. Teoh" <hsteoh quickfur.ath.cx> wrote:On Wed, Aug 07, 2013 at 12:55:10PM -0400, Nick Sabalausky wrote:Hmm, interesting tool. And you're right, I can't think of a better way than args[0] to do what it does. In any case, Phobos needs to have this "get current executable's path" function if it doesn't already.If it were up to me, args[0] would be eliminated outright.Naw, there are some valid use cases for it. Take a look at busybox, for example. :)
Aug 10 2013
On Sun, 11 Aug 2013 00:45:43 -0400 Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> wrote:On Wed, 7 Aug 2013 10:06:19 -0700 "H. S. Teoh" <hsteoh quickfur.ath.cx> wrote:Pull request: https://github.com/D-Programming-Language/phobos/pull/1463 It's updated from an older version that I had in here: https://bitbucket.org/Abscissa/semitwistdtools/src/c7f89f7cd2c086591b544d5bffc536827ae6f763/src/semitwist/util/io.d?at=master#cl-190On Wed, Aug 07, 2013 at 12:55:10PM -0400, Nick Sabalausky wrote:Hmm, interesting tool. And you're right, I can't think of a better way than args[0] to do what it does. In any case, Phobos needs to have this "get current executable's path" function if it doesn't already.If it were up to me, args[0] would be eliminated outright.Naw, there are some valid use cases for it. Take a look at busybox, for example. :)
Aug 10 2013
On 2013-08-07 07:31, Alan wrote:Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help!There's a pull request for that: https://github.com/D-Programming-Language/phobos/pull/1224 -- /Jacob Carlborg
Aug 07 2013