getEnvMap
Returns a snapshot of the environment variables of the current process.
Any modifications to the resulting EnvMap will not be reflected in the environment, and
likewise, any future modifications to the environment will not be reflected in the EnvMap.
Caller owns resulting EnvMap and should call its deinit fn when done.
Function parameters
Parameters
Type definitions in this namespace
Types
- ArgIteratorWindows
- Iterator that implements the Windows command-line parsing algorithm.
- ArgIteratorGeneralOptions
- Optional parameters for `ArgIteratorGeneral`
- ArgIterator
- Cross-platform command line argument iterator.
Functions in this namespace
Functions
- getCwd
- The result is a slice of `out_buffer`, from index `0`.
- getCwdAlloc
- Caller must free the returned memory.
- getEnvMap
- Returns a snapshot of the environment variables of the current process.
- getEnvVarOwned
- Caller must free returned memory.
- hasEnvVarConstant
- On Windows, `key` must be valid WTF-8.
- hasNonEmptyEnvVarConstant
- On Windows, `key` must be valid WTF-8.
- parseEnvVarInt
- Parses an environment variable as an integer.
- hasEnvVar
- On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
- hasNonEmptyEnvVar
- On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
- getenvW
- Windows-only.
- ArgIteratorGeneral
- A general Iterator to parse a string into a set of arguments
- args
- Holds the command-line arguments, with the program name as the first entry.
- argsWithAllocator
- You must deinitialize iterator's internal buffers by calling `deinit` when done.
- argsAlloc
- Caller must call argsFree on result.
- getUserInfo
- POSIX function which gets a uid from username.
- posixGetUserInfo
- TODO this reads /etc/passwd.
- execv
- Replaces the current process image with the executed process.
- execve
- Replaces the current process image with the executed process.
- totalSystemMemory
- Returns the total system memory, in bytes as a u64.
- cleanExit
- Indicate that we are now terminating with a successful exit code.
- raiseFileDescriptorLimit
- Raise the open file descriptor limit.
- createEnvironFromMap
- Creates a null-delimited environment variable block in the format
- createEnvironFromExisting
- Creates a null-delimited environment variable block in the format
- createWindowsEnvBlock
- Caller must free result.
- fatal
- Logs an error and then terminates the process with exit code 1.
Error sets in this namespace
Error Sets
Tells whether calling the `execv` or `execve` functions will be a compile error.
Values
Source
Implementation
pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {
var result = EnvMap.init(allocator);
errdefer result.deinit();
if (native_os == .windows) {
const ptr = windows.peb().ProcessParameters.Environment;
var i: usize = 0;
while (ptr[i] != 0) {
const key_start = i;
// There are some special environment variables that start with =,
// so we need a special case to not treat = as a key/value separator
// if it's the first character.
// https://devblogs.microsoft.com/oldnewthing/20100506-00/?p=14133
if (ptr[key_start] == '=') i += 1;
while (ptr[i] != 0 and ptr[i] != '=') : (i += 1) {}
const key_w = ptr[key_start..i];
const key = try unicode.wtf16LeToWtf8Alloc(allocator, key_w);
errdefer allocator.free(key);
if (ptr[i] == '=') i += 1;
const value_start = i;
while (ptr[i] != 0) : (i += 1) {}
const value_w = ptr[value_start..i];
const value = try unicode.wtf16LeToWtf8Alloc(allocator, value_w);
errdefer allocator.free(value);
i += 1; // skip over null byte
try result.putMove(key, value);
}
return result;
} else if (native_os == .wasi and !builtin.link_libc) {
var environ_count: usize = undefined;
var environ_buf_size: usize = undefined;
const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size);
if (environ_sizes_get_ret != .SUCCESS) {
return posix.unexpectedErrno(environ_sizes_get_ret);
}
if (environ_count == 0) {
return result;
}
const environ = try allocator.alloc([*:0]u8, environ_count);
defer allocator.free(environ);
const environ_buf = try allocator.alloc(u8, environ_buf_size);
defer allocator.free(environ_buf);
const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr);
if (environ_get_ret != .SUCCESS) {
return posix.unexpectedErrno(environ_get_ret);
}
for (environ) |env| {
const pair = mem.sliceTo(env, 0);
var parts = mem.splitScalar(u8, pair, '=');
const key = parts.first();
const value = parts.rest();
try result.put(key, value);
}
return result;
} else if (builtin.link_libc) {
var ptr = std.c.environ;
while (ptr[0]) |line| : (ptr += 1) {
var line_i: usize = 0;
while (line[line_i] != 0 and line[line_i] != '=') : (line_i += 1) {}
const key = line[0..line_i];
var end_i: usize = line_i;
while (line[end_i] != 0) : (end_i += 1) {}
const value = line[line_i + 1 .. end_i];
try result.put(key, value);
}
return result;
} else {
for (std.os.environ) |line| {
var line_i: usize = 0;
while (line[line_i] != 0 and line[line_i] != '=') : (line_i += 1) {}
const key = line[0..line_i];
var end_i: usize = line_i;
while (line[end_i] != 0) : (end_i += 1) {}
const value = line[line_i + 1 .. end_i];
try result.put(key, value);
}
return result;
}
}