August 07, 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:
>> 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:
>>>> 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/*
>>
>> No sorry, I meant how to find the directory in D code.
>
>
> Sorry, I misunderstood.

You're fine!  Thanks for trying though!
August 07, 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:
>> 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;

import std.stdio;

>
> void main(string[] args)
> {
> writeln(absolutePath(buildNormalizedPath(args[0])));
> }

Works for me.
August 07, 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:
>> 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])));
> }

I suppose that could work with a few modifications for
consistency.
THanks for the help everyone!
August 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.
August 07, 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:
>> 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])));
> }

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).
August 07, 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:
>> 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])));
>> }
>
> 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).

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.
August 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!

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();
}
August 07, 2013
...sorry, add import std.path; if you need the last two functions.
August 07, 2013
evilrat wrote:
> in theory should always return actual program path

Which theory? Especially in heteromorphic systems there is no such thing like a unique path between two accessible "points" in a file system.

-manfred
August 07, 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