Does anyone have a method to get all environment v...
# multiplatform
m
Does anyone have a method to get all environment variables on Native (ideally as a
Map<String, String>
)? I was hoping to use the `extern char **environ` but it's not available in
platform.posix
. Is it accessible in a different way? Do I need to set this up in a cinterop to get access?
e
as the man page says,
environ
must be declared in the user program
(
_GNU_SOURCE
won't have any impact on
platform.posix
as it was built as part of stdlib, not in your project)
if you do declare a
extern char **environ
in a cinterop def file, you can map it to a
Map<String, String>
easily, e.g.
Copy code
buildMap {
    for (i in 0..Int.MAX_VALUE) {
        val kv = environ?.get(i)?.toKString() ?: break
        this[kv.substringBefore('=')] = kv.substringAfter('=', "")
    }
}
m
Oh I read that part of the man page but completely glazed over "if the
_GNU_SOURCE
feature test macro is defined" Thanks for setting me straight