Skip to content

Commit a7ed842

Browse files
committed
Emulate fcntl.ioctl for command TIOCGWINSZ
1 parent 742acf9 commit a7ed842

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/core/IronPython.Modules/fcntl.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ a file object.
2727
""";
2828

2929

30+
// supporting fcntl.ioctl(fileno, termios.TIOCGWINSZ, buf)
31+
// where buf = array.array('h', [0, 0, 0, 0])
32+
public static object ioctl(CodeContext context, int fd, int cmd, [NotNone] IBufferProtocol arg, int mutate_flag = 1) {
33+
if (cmd == PythonTermios.TIOCGWINSZ) {
34+
using IPythonBuffer buf = arg.GetBuffer();
35+
36+
Span<short> winsize = stackalloc short[4];
37+
winsize[0] = (short)Console.WindowHeight;
38+
winsize[1] = (short)Console.WindowWidth;
39+
winsize[2] = (short)Console.BufferHeight; // buffer height and width are not accurate on macOS
40+
winsize[3] = (short)Console.BufferWidth;
41+
Span<byte> payload = MemoryMarshal.Cast<short, byte>(winsize);
42+
43+
if (buf.IsReadOnly || mutate_flag == 0) {
44+
byte[] res = buf.ToArray();
45+
payload.Slice(0, Math.Min(payload.Length, res.Length)).CopyTo(res);
46+
return Bytes.Make(res);
47+
} else {
48+
var res = buf.AsSpan();
49+
payload.Slice(0, Math.Min(payload.Length, res.Length)).CopyTo(res);
50+
return 0;
51+
}
52+
}
53+
throw new NotImplementedException($"ioctl: unsupported command {cmd}");
54+
}
55+
56+
3057
// FD Flags
3158
public static int FD_CLOEXEC = 1;
3259
public static int FASYNC => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x0040 : 0x2000;

0 commit comments

Comments
 (0)