╭ ssh.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 3 pub const ConnectAddress = struct { 4 host: []const u8, 5 port: u16, 6 }; 7 8 pub fn parseConnectAddress(value: []const u8) !ConnectAddress { 9 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidConnectAddress; 10 if (colon == 0 or colon + 1 >= value.len) return error.InvalidConnectAddress; 11 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 12 return .{ .host = value[0..colon], .port = port }; 13 } 14 15 pub fn copyFd(src: std.posix.fd_t, dst: std.posix.fd_t) !void { 16 var buffer: [4096]u8 = undefined; 17 while (true) { 18 const len = try std.posix.read(src, &buffer); 19 if (len == 0) break; 20 try writeAllFd(dst, buffer[0..len]); 21 } 22 } 23 24 pub fn writeAllFd(fd: std.posix.fd_t, bytes: []const u8) !void { 25 var written: usize = 0; 26 while (written < bytes.len) { 27 const rc = std.posix.system.write(fd, bytes[written..].ptr, bytes.len - written); 28 switch (std.posix.errno(rc)) { 29 .SUCCESS => written += @intCast(rc), 30 .INTR => continue, 31 .PIPE => return error.BrokenPipe, 32 else => return error.WriteFailed, 33 } 34 } 35 } 36