Function createWindowsEnvBlock [src]

Caller must free result.

Prototype

pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ![]u16

Parameters

allocator: mem.Allocatorenv_map: *const EnvMap

Source

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); }