Something went wrong. Try again.
Something went wrong. Try again.
1.3 kB · 41 lines
Zig
at main
123456789101112131415161718192021222324252627282930313233343536373839404142const std = @import("std");pub const Config = @import("Config.zig");const eql = std.mem.eql;
pub const CliArgs = struct { path: []const u8 = "/etc/limited-shell", command: ?[]const u8 = null,};
pub fn parseArgs(args: std.process.Args) !CliArgs { var iter = args.iterate(); _ = iter.skip(); var cli = CliArgs{}; while (iter.next()) |arg| { if (eql(u8, arg, "--config")) { cli.path = iter.next() orelse return error.InvalidCliArgs; } else if (eql(u8, arg, "-c")) { cli.command = iter.next() orelse return error.InvalidCliArgs; } } return cli;}
pub fn runCmd(alloc: std.mem.Allocator, io: std.Io, cmd: []const u8) !void { const res = try std.process.run(alloc, io, .{ .argv = &[_][]const u8{ "/bin/sh", "-c", cmd }, }); var buffer: [1024]u8 = undefined; var writer = std.Io.File.stdout().writer(io, &buffer); try print(res.stdout, buffer.len, &writer.interface); writer = std.Io.File.stderr().writer(io, &buffer); try print(res.stderr, buffer.len, &writer.interface);}
fn print(b: []u8, comptime ln: usize, writer: *std.Io.Writer) !void { for (0..b.len / ln + 1) |i| { const max = (i + 1) * ln; _ = try writer.write(b[i * ln .. if (max > b.len) b.len else max]); try writer.flush(); }}