From 7abb2866863471eb6252ebec67ac2de56abda0db Mon Sep 17 00:00:00 2001
From: Gleb Mazovetskiy <glex.spb@gmail.com>
Date: Sat, 30 Nov 2024 08:57:43 +0000
Subject: [PATCH] darwin8 ttyname_r patch

---
 libgfortran/io/unix.c                                |  7 +++++++
 .../sanitizer_common_interceptors.inc                | 12 ++++++++++++
 2 files changed, 19 insertions(+)

diff --git libgfortran/io/unix.c libgfortran/io/unix.c
index 16600c855f2..80d2fad7ef8 100644
--- libgfortran/io/unix.c
+++ libgfortran/io/unix.c
@@ -2094,7 +2094,14 @@ stream_ttyname (stream *s  __attribute__ ((unused)),
 		size_t buflen  __attribute__ ((unused)))
 {
 #ifdef HAVE_TTYNAME_R
+#if __DARWIN_UNIX03
   return ttyname_r (((unix_stream *)s)->fd, buf, buflen);
+#else
+  char *p = ttyname_r (((unix_stream *)s)->fd, buf, buflen);
+  if (!p)
+    return errno;
+  return 0;
+#endif
 #elif defined HAVE_TTYNAME
   char *p;
   size_t plen;
diff --git libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
index 607ecae6808..2d2fceedf79 100644
--- libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
+++ libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
@@ -5036,6 +5036,7 @@ INTERCEPTOR(char *, ttyname, int fd) {
 #endif
 
 #if SANITIZER_INTERCEPT_TTYNAME_R
+#if __DARWIN_UNIX03
 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
@@ -5044,6 +5045,17 @@ INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
   return res;
 }
+#else
+INTERCEPTOR(char*, ttyname_r, int fd, char *name, SIZE_T namesize) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
+  char *res = REAL(ttyname_r)(fd, name, namesize);
+  if (res != nullptr)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
+  return res;
+}
+#endif
+
 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
 #else
 #define INIT_TTYNAME_R
-- 
2.43.0

