From c677e8873e0eae790a1b8b07844a10efa9f63a1e Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 24 Jun 2026 11:57:02 +0900 Subject: wip design for v2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- src/os.zig | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/os.zig (limited to 'src/os.zig') diff --git a/src/os.zig b/src/os.zig new file mode 100644 index 0000000..ad9fe23 --- /dev/null +++ b/src/os.zig @@ -0,0 +1,84 @@ +const std = @import("std"); +const c = @cImport({ + @cInclude("stdlib.h"); + @cInclude("time.h"); +}); + +const log = std.log.scoped(.os); + +pub fn captureStdout( + io: std.Io, + command: []const u8, + opts: struct { + cwd: std.process.Child.Cwd = .inherit, + environ_map: ?*const std.process.Environ.Map = null, + progress_node: std.Progress.Node = std.Progress.Node.none, + }, + w: *std.Io.Writer, +) !void { + log.debug("Running <<< {s}", .{command}); + + var p = try std.process.spawn(io, .{ + .argv = &.{ "sh", "-c", command }, + .cwd = opts.cwd, + .stdin = .ignore, + .stderr = .ignore, + .stdout = .pipe, + .environ_map = opts.environ_map, + .progress_node = opts.progress_node, + }); + + var buf: [1024]u8 = undefined; + var r = p.stdout.?.readerStreaming(io, &buf); + + _ = try w.sendFileAll(&r, .unlimited); + try w.flush(); + + const result = try p.wait(io); + if (result.exited != 0) return error.NonZeroExitCode; +} + +pub fn shell( + io: std.Io, + command: []const u8, + opts: struct { + cwd: std.process.Child.Cwd = .inherit, + environ_map: ?*const std.process.Environ.Map = null, + progress_node: std.Progress.Node = std.Progress.Node.none, + }, +) !void { + log.debug("Running Shell <<< {s}", .{command}); + + var p = try std.process.spawn(io, .{ + .argv = &.{ "sh", "-c", command }, + .cwd = opts.cwd, + .stdin = .ignore, + .stderr = .inherit, + .stdout = .inherit, + .environ_map = opts.environ_map, + .progress_node = opts.progress_node, + }); + + const result = try p.wait(io); + if (result.exited != 0) return error.NonZeroExitCode; +} + +// use libc strftime to format time string +pub fn formatTimestamp(alloc: std.mem.Allocator, ts: std.Io.Timestamp, fmt: [*c]const u8) ![]const u8 { + const mtime_str = try alloc.alloc(u8, std.os.linux.PATH_MAX); + var tm: c.struct_tm = undefined; + + const sec = ts.toSeconds(); + _ = c.localtime_r(&sec, &tm); + const rc = c.strftime( + mtime_str.ptr, + mtime_str.len, + fmt, + &tm, + ); + switch (std.posix.errno(rc)) { + .SUCCESS => {}, + else => |err| return std.posix.unexpectedErrno(err), + } + return try alloc.realloc(mtime_str, rc); +} -- cgit 1.4.1