1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! env — process environment helpers.
//!
//! std.posix.getenv was removed in zig 0.16; this wraps libc getenv so the
//! four call sites that needed it don't each re-declare the extern shim.
const std = @import("std");
/// libc getenv — returns the value as a slice, or null if unset.
pub fn getenv(key: [*:0]const u8) ?[]const u8 {
const ptr = std.c.getenv(key) orelse return null;
return std.mem.sliceTo(ptr, 0);
}
/// parse an integer-valued env var, falling back to `default` if unset or unparseable.
pub fn parseEnvInt(comptime T: type, key: [*:0]const u8, default: T) T {
const val = getenv(key) orelse return default;
return std.fmt.parseInt(T, val, 10) catch default;
}