Skip to content

Instantly share code, notes, and snippets.

@rchog

rchog/thing.ml Secret

Created March 24, 2024 12:50
Show Gist options
  • Save rchog/ac9303c0d6b4d0deb74b006534749722 to your computer and use it in GitHub Desktop.
Save rchog/ac9303c0d6b4d0deb74b006534749722 to your computer and use it in GitHub Desktop.
type winsize
let get_ws () =
let open Ctypes in
let open Foreign in
let winsize : winsize structure typ = structure "winsize" in
let ws_row = field winsize "ws_row" ushort in
let ws_col = field winsize "ws_col" ushort in
let ws_xpixel = field winsize "ws_xpixel" ushort in
let ws_ypixel = field winsize "ws_ypixel" ushort in
let () = seal winsize in
let ioctl =
foreign "ioctl" ~check_errno:true
(int @-> ulong @-> ptr winsize @-> returning int)
in
(* No clue how to fetch this value from libc without including some silly c
"library" that just has:
#include <sys/ioctl.h>
int TIOCGWINSZ() { return TIOCGWINSZ; }
This constant is correct for my Linux system at present.
*)
let tiocgwinsz = 21523 in
let ws = make winsize in
let _ = ioctl 0 (Unsigned.ULong.of_int tiocgwinsz) @@ addr ws in
( Unsigned.UShort.to_int (getf ws ws_col),
Unsigned.UShort.to_int (getf ws ws_row) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment