Function createEnvironFromMap [src]
Creates a null-delimited environment variable block in the format
expected by POSIX, from a hash map plus options.
Prototype
pub fn createEnvironFromMap( arena: Allocator, map: *const EnvMap, options: CreateEnvironOptions, ) Allocator.Error![:null]?[*:0]u8
Parameters
arena: Allocator
map: *const EnvMap
options: CreateEnvironOptions
Source
pub fn createEnvironFromMap(
arena: Allocator,
map: *const EnvMap,
options: CreateEnvironOptions,
) Allocator.Error![:null]?[*:0]u8 {
const ZigProgressAction = enum { nothing, edit, delete, add };
const zig_progress_action: ZigProgressAction = a: {
const fd = options.zig_progress_fd orelse break :a .nothing;
const contains = map.get("ZIG_PROGRESS") != null;
if (fd >= 0) {
break :a if (contains) .edit else .add;
} else {
if (contains) break :a .delete;
}
break :a .nothing;
};
const envp_count: usize = c: {
var count: usize = map.count();
switch (zig_progress_action) {
.add => count += 1,
.delete => count -= 1,
.nothing, .edit => {},
}
break :c count;
};
const envp_buf = try arena.allocSentinel(?[*:0]u8, envp_count, null);
var i: usize = 0;
if (zig_progress_action == .add) {
envp_buf[i] = try std.fmt.allocPrintZ(arena, "ZIG_PROGRESS={d}", .{options.zig_progress_fd.?});
i += 1;
}
{
var it = map.iterator();
while (it.next()) |pair| {
if (mem.eql(u8, pair.key_ptr.*, "ZIG_PROGRESS")) switch (zig_progress_action) {
.add => unreachable,
.delete => continue,
.edit => {
envp_buf[i] = try std.fmt.allocPrintZ(arena, "{s}={d}", .{
pair.key_ptr.*, options.zig_progress_fd.?,
});
i += 1;
continue;
},
.nothing => {},
};
envp_buf[i] = try std.fmt.allocPrintZ(arena, "{s}={s}", .{ pair.key_ptr.*, pair.value_ptr.* });
i += 1;
}
}
assert(i == envp_count);
return envp_buf;
}