ptsname_r: Make it consistent with ptsname on OSF/1.
authorBruno Haible <bruno@clisp.org>
Sun, 24 Jun 2012 21:28:13 +0000 (23:28 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 24 Jun 2012 21:28:13 +0000 (23:28 +0200)
* lib/ptsname_r.c (__ptsname_r): Add a different implementation for
OSF/1.

ChangeLog
lib/ptsname_r.c

index 94e8a41..908dca4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2012-06-24  Bruno Haible  <bruno@clisp.org>
 
+       ptsname_r: Make it consistent with ptsname on OSF/1.
+       * lib/ptsname_r.c (__ptsname_r): Add a different implementation for
+       OSF/1.
+
+2012-06-24  Bruno Haible  <bruno@clisp.org>
+
        ttyname_r: Fix result on OSF/1, Solaris.
        * lib/ttyname_r.c (ttyname_r): Produce a NUL-terminated result.
 
index 3805347..ec314c6 100644 (file)
 # include <stdio.h>
 #endif
 
+#ifdef __osf__
+/* Get ioctl(), ISPTM.  */
+# include <sys/ioctl.h>
+/* Get the major, minor macros.  */
+# include <sys/sysmacros.h>
+# include <stdio.h>
+#endif
+
 
 /* Store at most BUFLEN characters of the pathname of the slave pseudo
    terminal associated with the master FD is open on in BUF.
@@ -107,6 +115,35 @@ __ptsname_r (int fd, char *buf, size_t buflen)
       }
     memcpy (buf, tmpbuf, n + 1);
   }
+#elif defined __osf__ /* OSF/1 */
+  /* This implementation returns /dev/pts/N, like ptsname() does.
+     Whereas the generic implementation below returns /dev/ttypN.
+     Both are correct, but let's be consistent with ptsname().  */
+  if (fstat (fd, &st) < 0)
+    return errno;
+  if (!S_ISCHR (st.st_mode))
+    {
+      errno = ENOTTY;
+      return errno;
+    }
+  {
+    int dev;
+    char tmpbuf[9 + 10 + 1];
+    int n;
+    dev = ioctl (fd, ISPTM, NULL);
+    if (dev < 0)
+      {
+        errno = ENOTTY;
+        return errno;
+      }
+    n = sprintf (tmpbuf, "/dev/pts/%u", minor (dev));
+    if (n >= buflen)
+      {
+        errno = ERANGE;
+        return errno;
+      }
+    memcpy (buf, tmpbuf, n + 1);
+  }
 #else
   if (!__isatty (fd))
     {