www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting environment variables?

reply Christopher Wright <dhasenan gmail.com> writes:
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.
Nov 22 2008
next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
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 ;)
Nov 22 2008
parent reply Christopher Wright <dhasenan gmail.com> writes:
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.
Nov 22 2008
next sibling parent BCS <ao pathlink.com> writes:
Reply to Christopher,

 I thought (perhaps wrongly) C allowed you to declare main as taking a
 list of environment variables,
It does.
Nov 22 2008
prev sibling parent "Mark Fischer" <marqfischer yahoo.com> writes:
Better late than never...

On Sunday, 23 November 2008 at 02:28:30 UTC, Christopher Wright
wrote:
...
 I thought (perhaps wrongly) C allowed you to declare main as 
 taking a list of environment variables, which is why I asked
Indeed, on Unix { not POSIX } and Windows: From Wiki: ====================== Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must always be int;[3] for example, Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h: int main(int argc, char **argv, char **envp); Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:[4] ====================== Mark ~~~~
May 02 2013
prev sibling next sibling parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"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.
Nov 22 2008
parent reply novice2 <sorry noem.ail> writes:
     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;
Nov 23 2008
parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"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.
Nov 23 2008
parent reply novice2 <sorry noem.ail> writes:
 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.
Nov 23 2008
parent reply John C <johnch_atms hotmail.com> writes:
novice2 Wrote:

 i am afraid that windows API named *W works with UCS2 string.
 but D wchar[] is UTF-16.
Wrong - Windows has used UTF-16 as native since Windows 2000.
Nov 24 2008
parent reply Christopher Wright <dhasenan gmail.com> writes:
John C wrote:
 novice2 Wrote:
 
 i am afraid that windows API named *W works with UCS2 string.
 but D wchar[] is UTF-16.
Wrong - Windows has used UTF-16 as native since Windows 2000.
Actually, you're both right. UCS2 is UTF-16.
Nov 24 2008
parent "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 24 Nov 2008 16:53:20 +0300, Christopher Wright  
<dhasenan gmail.com> wrote:

 John C wrote:
 novice2 Wrote:

 i am afraid that windows API named *W works with UCS2 string.
 but D wchar[] is UTF-16.
Wrong - Windows has used UTF-16 as native since Windows 2000.
Actually, you're both right. UCS2 is UTF-16.
No. A quote from Wikipedia: "Because of the technical similarities and upwards compatibility from UCS-2 to UTF-16, the two encodings are often erroneously conflated and used as if interchangeable, so that strings encoded in UTF-16 are sometimes misidentified as being encoded in UCS-2." "UTF-16 is the native internal representation of text in the Microsoft Windows 2000/XP/2003/Vista/CE; Qualcomm BREW operating systems; the Java and .NET bytecode environments; Mac OS X's Cocoa and Core Foundation frameworks; and the Qt cross-platform graphical widget toolkit." "Older Windows NT systems (prior to Windows 2000) only support UCS-2."
Nov 24 2008
prev sibling parent reply torhu <no spam.invalid> writes:
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.
Nov 23 2008
parent Lars Ivar Igesund <larsivar igesund.net> writes:
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
Nov 23 2008