1. environ variables

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

new topic     » topic index » view message » categorize

2. Re: environ variables

This is the way I did it on DOS (Eu3.1)

https://www.rapideuphoria.com/environ.ex

new topic     » goto parent     » topic index » view message » categorize

3. Re: environ variables

achury said...

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.

achury said...

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 
achury said...

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: environ variables

Thanks for this routine!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu