os: appendEnv

This commit is contained in:
Mitchell Hashimoto
2023-09-20 13:02:06 -07:00
parent bd528f5c11
commit ea4bc95f43
2 changed files with 49 additions and 0 deletions

48
src/os/env.zig Normal file
View File

@ -0,0 +1,48 @@
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
/// Append a value to an environment variable such as PATH.
/// The returned value is always allocated so it must be freed.
pub fn appendEnv(
alloc: Allocator,
current: []const u8,
value: []const u8,
) ![]u8 {
// If there is no prior value, we return it as-is
if (current.len == 0) return try alloc.dupe(u8, value);
// Otherwise we must prefix.
const sep = switch (builtin.os.tag) {
.windows => ";",
else => ":",
};
return try std.fmt.allocPrint(alloc, "{s}{s}{s}", .{
current,
sep,
value,
});
}
test "appendEnv empty" {
const testing = std.testing;
const alloc = testing.allocator;
const result = try appendEnv(alloc, "", "foo");
defer alloc.free(result);
try testing.expectEqualStrings(result, "foo");
}
test "appendEnv existing" {
const testing = std.testing;
const alloc = testing.allocator;
const result = try appendEnv(alloc, "a:b", "foo");
defer alloc.free(result);
if (builtin.os.tag == .windows) {
try testing.expectEqualStrings(result, "a:b;foo");
} else {
try testing.expectEqualStrings(result, "a:b:foo");
}
}

View File

@ -1,6 +1,7 @@
//! The "os" package contains utilities for interfacing with the operating
//! system.
pub usingnamespace @import("env.zig");
pub usingnamespace @import("file.zig");
pub usingnamespace @import("flatpak.zig");
pub usingnamespace @import("homedir.zig");