I’m porting the function isatty
that’s commonly found in POSIX systems and also implemented on Windows. It’s currently implemented like this:
package utils
import c "core:c/libc"
import "core:fmt"
import "core:os"
when ODIN_OS == .Windows {
foreign import libc "system:libucrt.lib"
} else when ODIN_OS == .Darwin {
foreign import libc "system:System.framework"
} else {
foreign import libc "system:c"
}
@(private)
IS_TTY_LINKNAME :: "isatty" when ODIN_OS != .Windows else "_isatty"
@(private)
FILENO_LINKNAME :: "fileno" when ODIN_OS != .Windows else "_fileno"
foreign libc {
@(link_name = IS_TTY_LINKNAME, private)
isatty :: proc(fd: c.int) -> c.int ---
@(link_name = FILENO_LINKNAME, private)
fileno :: proc(stream: ^c.FILE) -> c.int ---
}
is_tty :: proc(fd: ^c.FILE) -> b32 {
return transmute(b32)isatty(fileno(c.stdout))
}
But I think it would be better if is_tty
could receive an os.handle
instead of a ^c.File
. How can I do it?
(I know fileno
could be public but I want to eventually make a wrapper that receives an os.Handle
and I would run into the same problem)