createWindowsEnvBlock
Caller must free result.
Function parameters
Parameters
- allocator:mem.Allocator
- env_map:*const EnvMap
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 createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ![]u16 {
// count bytes needed
const max_chars_needed = x: {
// Only need 2 trailing NUL code units for an empty environment
var max_chars_needed: usize = if (env_map.count() == 0) 2 else 1;
var it = env_map.iterator();
while (it.next()) |pair| {
// +1 for '='
// +1 for null byte
max_chars_needed += pair.key_ptr.len + pair.value_ptr.len + 2;
}
break :x max_chars_needed;
};
const result = try allocator.alloc(u16, max_chars_needed);
errdefer allocator.free(result);
var it = env_map.iterator();
var i: usize = 0;
while (it.next()) |pair| {
i += try unicode.wtf8ToWtf16Le(result[i..], pair.key_ptr.*);
result[i] = '=';
i += 1;
i += try unicode.wtf8ToWtf16Le(result[i..], pair.value_ptr.*);
result[i] = 0;
i += 1;
}
result[i] = 0;
i += 1;
// An empty environment is a special case that requires a redundant
// NUL terminator. CreateProcess will read the second code unit even
// though theoretically the first should be enough to recognize that the
// environment is empty (see https://nullprogram.com/blog/2023/08/23/)
if (env_map.count() == 0) {
result[i] = 0;
i += 1;
}
return try allocator.realloc(result, i);
}