From 18a3b8149a45f11e4e24f791a4ded28c2ca2f598 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 17 Sep 2026 23:03:16 +0900 Subject: [PATCH] Detect the old style `ttyname_r` return type --- ext/io/console/console.c | 3 +++ ext/io/console/extconf.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ext/io/console/console.c b/ext/io/console/console.c index aa19901..602d069 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -2314,6 +2314,9 @@ console_ttyname(VALUE io) return rb_usascii_str_new_lit("con"); # elif defined HAVE_TTYNAME_R { +# ifdef TTYNAME_R_RETURNS_CHAR_P +# define ttyname_r(fd, name, size) (ttyname_r(fd, name, size) == NULL ? errno : 0) +# endif char termname[1024], *tn = termname; size_t size = sizeof(termname); int e; diff --git a/ext/io/console/extconf.rb b/ext/io/console/extconf.rb index 6837d48..9ef71bc 100644 --- a/ext/io/console/extconf.rb +++ b/ext/io/console/extconf.rb @@ -50,7 +50,18 @@ end have_func("rb_category_warn") have_const("RB_WARN_CATEGORY_DEPRECATED") - win32 or have_func("ttyname_r") or have_func("ttyname") + unless win32 + if have_func("ttyname_r") + ret = checking_for("type of ttyname_r()", "%s") {try_link(<<~C) ? "int" : "char *"} + #{cpp_include %} + int t(void) {char name[1024]; return ttyname_r(0, name, sizeof(name)) % 1024;} + #{MAIN_DOES_NOTHING('t')} + C + $defs << "-DTTYNAME_R_RETURNS_#{ret.tr_cpp}" + else + have_func("ttyname") + end + end have_func("rb_prepend_module") # not exported by TruffleRuby vk_tool = find_executable("gperf") create_makefile("io/console") {|conf|