1. environ variables
- Posted by achury Jul 29, 2022
- 861 views
Hello
Is there a method to get the full list of environment variables?
I have a "dirty" method, execute on system:
set > environ.txt
and then load and examine the file.
But there is a "clean" method to do it?
Would be nice a standar library function that return the all the environment variables (at least their names), on one single sequence.
Best regards
Marco Achury
2. Re: environ variables
- Posted by achury Jul 29, 2022
- 860 views
This is the way I did it on DOS (Eu3.1)
3. Re: environ variables
- Posted by ghaberek (admin) Aug 01, 2022
- 819 views
Is there a method to get the full list of environment variables?
I have a "dirty" method, execute on system:
set > environ.txt
and then load and examine the file.
Agreed, this feels dirty. I prefer to use the operating system's functions to do these types of things natively.
But there is a "clean" method to do it?
Here is a function that will get all environment variables and their values on Windows or Linux:
include std/dll.e include std/machine.e include std/sequence.e ifdef WINDOWS then constant kernel32 = open_dll( "kernel32.dll" ) constant xGetEnvironmentStrings = define_c_func( kernel32, "GetEnvironmentStringsA", {}, C_POINTER ) elsifdef LINUX then constant libc = open_dll( "libc.so.6" ) constant _environ = define_c_var( libc, "environ" ) end ifdef public function get_environment_strings() sequence vars = {} ifdef WINDOWS then -- GetEnvironmentStrings function -- https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentstrings atom ptr = c_func( xGetEnvironmentStrings, {} ) sequence str = peek_string( ptr ) while length( str ) do vars = append( vars, stdseq:split(str,'=',0,2) ) ptr += length( str ) + 1 str = peek_string( ptr ) end while elsifdef LINUX then -- environ - user environment -- https://linux.die.net/man/7/environ atom ptr = peek_pointer( _environ ) object str = peek_pointer( ptr ) while str != NULL do str = peek_string( str ) vars = append( vars, stdseq:split(str,'=',0,2) ) ptr += sizeof( C_POINTER ) str = peek_pointer( ptr ) end while end ifdef return vars end function
Would be nice a standar library function that return the all the environment variables (at least their names), on one single sequence.
Agreed. I will try to work this in at some point. There are a lot of system calls like this that I'd like to include out of the box in the standard library.
-Greg