Jump to page: 1 2
Thread overview
Getting environment variables?
Nov 22, 2008
Christopher Wright
Nov 23, 2008
Christopher Wright
Nov 23, 2008
BCS
May 02, 2013
Mark Fischer
Nov 22, 2008
Stewart Gordon
Nov 23, 2008
novice2
Nov 23, 2008
Stewart Gordon
Nov 24, 2008
novice2
Nov 24, 2008
John C
Nov 24, 2008
Christopher Wright
Nov 24, 2008
Denis Koroskin
Nov 23, 2008
torhu
Nov 23, 2008
Lars Ivar Igesund
November 22, 2008
Hey all,

How do I get environment variables in a D program? I specifically want the path to a user's home folder.

Ta muchly.
November 22, 2008
On Sat, Nov 22, 2008 at 12:55 PM, Christopher Wright <dhasenan@gmail.com> wrote:
> Hey all,
>
> How do I get environment variables in a D program? I specifically want the path to a user's home folder.
>
> Ta muchly.
>

In Tango, there's tango.sys.Environment (http://www.dsource.org/projects/tango/docs/current/tango.sys.Environment.html) which provides a nice interface to environment variables.

In Phobos, I think you have to use the C functions to get at environment variables, but std.path.expandTilde can be abused to get the home folder ;)
November 22, 2008
"Christopher Wright" <dhasenan@gmail.com> wrote in message news:gg9h3f$9uo$1@digitalmars.com...
> Hey all,
>
> How do I get environment variables in a D program?

std.c.stdlib.getenv
http://www.cplusplus.com/reference/clibrary/cstdlib/getenv.html

> I specifically want the path to a user's home folder.

Platform-dependent.

For Windows, you can use the concatenation of HOMEDRIVE and HOMEPATH, but I don't know under which versions of Windows these variables are actually set.

----------
import std.c.stdlib, std.string, std.stdio;

void main() {
   string homeDrive, homePath;
   homeDrive = toString(getenv("HOMEDRIVE")).dup;
   homePath = toString(getenv("HOMEPATH")).dup;

   writefln("%s%s", homeDrive, homePath);
}
----------

For Unix, I'm informed that the HOME environment variable does it.
http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2005-09/0092.html

HTH

Stewart.

-- 
My e-mail address is valid but not my primary mailbox.  Please keep replies on the 'group where everybody may benefit. 

November 23, 2008
Jarrett Billingsley wrote:
> On Sat, Nov 22, 2008 at 12:55 PM, Christopher Wright <dhasenan@gmail.com> wrote:
>> Hey all,
>>
>> How do I get environment variables in a D program? I specifically want the
>> path to a user's home folder.
>>
>> Ta muchly.
>>
> 
> In Tango, there's tango.sys.Environment
> (http://www.dsource.org/projects/tango/docs/current/tango.sys.Environment.html)
> which provides a nice interface to environment variables.
> 
> In Phobos, I think you have to use the C functions to get at
> environment variables, but std.path.expandTilde can be abused to get
> the home folder ;)

Thanks, that's exactly what I was looking for.

I thought (perhaps wrongly) C allowed you to declare main as taking a list of environment variables, which is why I asked here rather than Tango's forums. But D doesn't, so I probably should have asked at the library-specific forums.
November 23, 2008
Reply to Christopher,

> I thought (perhaps wrongly) C allowed you to declare main as taking a
> list of environment variables,

It does.


November 23, 2008
>     homeDrive = toString(getenv("HOMEDRIVE")).dup;
>     homePath = toString(getenv("HOMEPATH")).dup;

don't forget, that D char[] is utf8 and windows char* is 8-bit chars, not utf8.
so you should import std.windows.charset and use toMBSz() as D->WindowsAPI and fromMBSz as WindowsAPI->D string converter. for example:
homeDrive = fromMBSz(getenv("HOMEDRIVE")).dup;
November 23, 2008
"novice2" <sorry@noem.ail> wrote in message news:ggbd96$2drl$1@digitalmars.com...
>>     homeDrive = toString(getenv("HOMEDRIVE")).dup;
>>     homePath = toString(getenv("HOMEPATH")).dup;
>
> don't forget, that D char[] is utf8 and windows char* is 8-bit chars, not utf8.
> so you should import std.windows.charset and use toMBSz() as D->WindowsAPI and fromMBSz as WindowsAPI->D string converter. for example:
> homeDrive = fromMBSz(getenv("HOMEDRIVE")).dup;

Under what setups can the drive letter be a non-ASCII character?

But according to my experiments, getenv works in the OEM encoding rather than the Windows encoding, so you'd need

   homePath = fromMBSz(getenv("HOMEPATH"), 1).dup;

If you want to make sure all Unicode characters are preserved, you're best off using the Windows API

   wchar[] wpath;
   wpath.length = GetEnvironmentVariableW("HOMEPATH", null, 0);
   GetEnvironmentVariableW("HOMEPATH", wpath.ptr, wpath.length);

Stewart.

-- 
My e-mail address is valid but not my primary mailbox.  Please keep replies on the 'group where everybody may benefit. 

November 23, 2008
Christopher Wright wrote:
> Hey all,
> 
> How do I get environment variables in a D program? I specifically want the path to a user's home folder.
> 
> Ta muchly.

I think the 'correct' way on Windows is to use SHGetSpecialFolderPathA.

Something like this:

char[MAX_PATH] buf;
SHGetSpecialFolderPathA(null, buf.ptr, CSIDL_PERSONAL, false);
char[] dir = toString(buf.ptr);

or CSIDL_APPDATA, etc.
November 23, 2008
torhu wrote:

> Christopher Wright wrote:
>> Hey all,
>> 
>> How do I get environment variables in a D program? I specifically want the path to a user's home folder.
>> 
>> Ta muchly.
> 
> I think the 'correct' way on Windows is to use SHGetSpecialFolderPathA.
> 
> Something like this:
> 
> char[MAX_PATH] buf;
> SHGetSpecialFolderPathA(null, buf.ptr, CSIDL_PERSONAL, false);
> char[] dir = toString(buf.ptr);
> 
> or CSIDL_APPDATA, etc.

In tango this is available in tango.sys.win32.SpecialPath

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango
November 24, 2008
> Under what setups can the drive letter be a non-ASCII character?
any non-english windows have folders, usernames, etc with non-ascii chars, therefore this names presents in environment, registry, file API etc.

>     wchar[] wpath;
>     wpath.length = GetEnvironmentVariableW("HOMEPATH", null, 0);
>     GetEnvironmentVariableW("HOMEPATH", wpath.ptr, wpath.length);

i am afraid that windows API named *W works with UCS2 string.
but D wchar[] is UTF-16. since UCS2 is some sort of subset of UTF-16, then your code above is correct (then lvalue is D wchar[]).
but problems can appear in reverse situation - pass D wchar[] to windows API.

that is why utf strings in D annoy me - because all strings exchange D<->WindowsAPI shoud be passed thru to-utf and from-utf convertion. other programming languages works fine with windows, not required utf-support editors etc. sorry for offtopic.
« First   ‹ Prev
1 2