Zig 0.16: Environ

This commit is contained in:
LN Liberda 2026-05-23 04:17:52 +02:00
parent 0703b62a1a
commit 4e74821cf9
3 changed files with 6 additions and 7 deletions

View file

@ -74,8 +74,7 @@ fn deleteItem(dir: std.Io.Dir, path: [:0]const u8, ptr: *align(1) ?*model.Entry)
// Returns true if the item has been deleted successfully.
fn deleteCmd(path: [:0]const u8, ptr: *align(1) ?*model.Entry) bool {
{
var env = std.process.getEnvMap(main.allocator) catch unreachable;
defer env.deinit();
var env = main.environ_map.clone(main.allocator) catch unreachable;
env.put("NCDU_DELETE_PATH", path) catch unreachable;
// Since we're passing the path as an environment variable and go through
@ -83,7 +82,7 @@ fn deleteCmd(path: [:0]const u8, ptr: *align(1) ?*model.Entry) bool {
// shell escaping.
const cmd = std.fmt.allocPrint(main.allocator, "{s} \"$NCDU_DELETE_PATH\"", .{main.config.delete_command}) catch unreachable;
defer main.allocator.free(cmd);
ui.runCmd(&.{"/bin/sh", "-c", cmd}, null, &env, true);
ui.runCmd(&.{"/bin/sh", "-c", cmd}, null, env, true);
}
const stat = scan.statAt(std.fs.cwd(), path, false, null) catch {

View file

@ -609,7 +609,7 @@ pub fn main(init: std.process.Init) void {
},
.shell => {
const shell = environ_map.get("NCDU_SHELL") orelse environ_map.get("SHELL") orelse "/bin/sh";
ui.runCmd(&.{shell}, browser.dir_path, false);
ui.runCmd(&.{shell}, browser.dir_path, environ_map.clone(allocator) catch unreachable, false);
state = .browse;
},
.delete => {

View file

@ -645,10 +645,10 @@ fn waitInput() void {
_ = rd.interface.discardDelimiterExclusive('\n') catch unreachable;
}
pub fn runCmd(cmd: []const []const u8, cwd: ?[]const u8, reporterr: bool) void {
pub fn runCmd(cmd: []const []const u8, cwd: ?[]const u8, environ: std.process.Environ.Map, reporterr: bool) void {
deinit();
defer init();
var env = main.environ_map.clone(main.allocator) catch unreachable;
var env = @constCast(&environ);
defer env.deinit();
// NCDU_LEVEL can only count to 9, keeps the implementation simple.
@ -664,7 +664,7 @@ pub fn runCmd(cmd: []const []const u8, cwd: ?[]const u8, reporterr: bool) void {
var child = std.process.spawn(main.io, .{
.argv = cmd,
.cwd = if (cwd) |p| .{ .path = p } else .inherit,
.environ_map = &env,
.environ_map = env,
}) catch |e| die("Failed to spawn process: {s}", .{errorString(e)});
const term = child.wait(main.io) catch |e| blk: {