1  /* ptsname -- return the name of a pty slave given an FD to the pty master
       2     Copyright (C) 1999-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <errno.h>
      20  #include <string.h>
      21  #include <sys/stat.h>
      22  #include <hurd.h>
      23  #include <hurd/fd.h>
      24  #include <hurd/term.h>
      25  
      26  
      27  /* Return the pathname of the pseudo terminal slave associated with
      28     the master FD is open on, or NULL on errors.
      29     The returned storage is good until the next call to this function.  */
      30  char *
      31  ptsname (int fd)
      32  {
      33    static string_t peername;
      34    error_t err;
      35  
      36    err = __ptsname_r (fd, peername, sizeof (peername));
      37  
      38    return err ? NULL : peername;
      39  }
      40  
      41  
      42  /* We don't need STP, but fill it for conformity with the Linux version...  */
      43  int
      44  __ptsname_internal (int fd, char *buf, size_t buflen, struct stat64 *stp)
      45  {
      46    string_t peername;
      47    size_t len;
      48    error_t err;
      49    int ttype;
      50  
      51    if (HURD_DPORT_USE (fd, __term_get_bottom_type (port, &ttype)) == 0)
      52      /* get_bottom_type succeeded, this is the slave side.  */
      53      return __hurd_fail (ENOTTY), ENOTTY;
      54  
      55    if (err = HURD_DPORT_USE (fd, __term_get_peername (port, peername)))
      56      {
      57        if (err == EMIG_BAD_ID || err == EOPNOTSUPP)
      58  	err = ENOTTY;
      59        return __hurd_dfail (fd, err), errno;
      60      }
      61  
      62    len = __strnlen (peername, sizeof peername - 1) + 1;
      63    if (len > buflen)
      64      return __hurd_fail (ERANGE), ERANGE;
      65  
      66    if (stp)
      67      {
      68        if (__stat64 (peername, stp) < 0)
      69  	return errno;
      70      }
      71  
      72    memcpy (buf, peername, len);
      73    return 0;
      74  }
      75  
      76  
      77  /* Store at most BUFLEN characters of the pathname of the slave pseudo
      78     terminal associated with the master FD is open on in BUF.
      79     Return 0 on success, otherwise an error number.  */
      80  int
      81  __ptsname_r (int fd, char *buf, size_t buflen)
      82  {
      83    return __ptsname_internal (fd, buf, buflen, NULL);
      84  }
      85  libc_hidden_def (__ptsname_r)
      86  weak_alias (__ptsname_r, ptsname_r)