From 747ebfb6281ecc49f4d39a07cb5b10ac7c871578 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Apr 2022 09:32:03 -0700 Subject: [PATCH] fix pty resizing on darwin --- src/Pty.zig | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Pty.zig b/src/Pty.zig index aa0e9e589..cf03c823b 100644 --- a/src/Pty.zig +++ b/src/Pty.zig @@ -6,18 +6,27 @@ const Pty = @This(); const std = @import("std"); const builtin = @import("builtin"); const testing = std.testing; -const linux = std.os.linux; const fd_t = std.os.fd_t; -const winsize = linux.winsize; + const c = switch (builtin.os.tag) { .macos => @cImport({ - @cInclude("util.h"); + @cInclude("sys/ioctl.h"); // ioctl and constants + @cInclude("util.h"); // openpty() }), else => @cImport({ @cInclude("pty.h"); }), }; +/// Redeclare this winsize struct so we can just use a Zig struct. This +/// layout should be correct on all tested platforms. +const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + /// The file descriptors for the master and slave side of the pty. master: fd_t, slave: fd_t, @@ -52,7 +61,7 @@ pub fn deinit(self: *Pty) void { /// Return the size of the pty. pub fn getSize(self: Pty) !winsize { var ws: winsize = undefined; - if (linux.ioctl(self.master, linux.T.IOCGWINSZ, @ptrToInt(&ws)) < 0) + if (c.ioctl(self.master, c.TIOCGWINSZ, @ptrToInt(&ws)) < 0) return error.IoctlFailed; return ws; @@ -60,7 +69,7 @@ pub fn getSize(self: Pty) !winsize { /// Set the size of the pty. pub fn setSize(self: Pty, size: winsize) !void { - if (linux.ioctl(self.master, linux.T.IOCSWINSZ, @ptrToInt(&size)) < 0) + if (c.ioctl(self.master, c.TIOCSWINSZ, @ptrToInt(&size)) < 0) return error.IoctlFailed; }