Blame sysdeps/unix/sysv/linux/ptsname.c

Packit 6c4009
/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <paths.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/ioctl.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/sysmacros.h>
Packit 6c4009
#include <termios.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
Packit 6c4009
/* Check if DEV corresponds to a master pseudo terminal device.  */
Packit 6c4009
#define MASTER_P(Dev)							\
Packit 6c4009
  (__gnu_dev_major ((Dev)) == 2						\
Packit 6c4009
   || (__gnu_dev_major ((Dev)) == 4					\
Packit 6c4009
       && __gnu_dev_minor ((Dev)) >= 128 && __gnu_dev_minor ((Dev)) < 192) \
Packit 6c4009
   || (__gnu_dev_major ((Dev)) >= 128 && __gnu_dev_major ((Dev)) < 136))
Packit 6c4009
Packit 6c4009
/* Check if DEV corresponds to a slave pseudo terminal device.  */
Packit 6c4009
#define SLAVE_P(Dev)							\
Packit 6c4009
  (__gnu_dev_major ((Dev)) == 3						\
Packit 6c4009
   || (__gnu_dev_major ((Dev)) == 4					\
Packit 6c4009
       && __gnu_dev_minor ((Dev)) >= 192 && __gnu_dev_minor ((Dev)) < 256) \
Packit 6c4009
   || (__gnu_dev_major ((Dev)) >= 136 && __gnu_dev_major ((Dev)) < 144))
Packit 6c4009
Packit 6c4009
/* Note that major number 4 corresponds to the old BSD style pseudo
Packit 6c4009
   terminal devices.  As of Linux 2.1.115 these are no longer
Packit 6c4009
   supported.  They have been replaced by major numbers 2 (masters)
Packit 6c4009
   and 3 (slaves).  */
Packit 6c4009
Packit 6c4009
/* Directory where we can find the slave pty nodes.  */
Packit 6c4009
#define _PATH_DEVPTS "/dev/pts/"
Packit 6c4009
Packit 6c4009
/* The are declared in getpt.c.  */
Packit 6c4009
extern const char __libc_ptyname1[] attribute_hidden;
Packit 6c4009
extern const char __libc_ptyname2[] attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Static buffer for `ptsname'.  */
Packit 6c4009
static char buffer[sizeof (_PATH_DEVPTS) + 20];
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Return the pathname of the pseudo terminal slave associated with
Packit 6c4009
   the master FD is open on, or NULL on errors.
Packit 6c4009
   The returned storage is good until the next call to this function.  */
Packit 6c4009
char *
Packit 6c4009
ptsname (int fd)
Packit 6c4009
{
Packit 6c4009
  return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__ptsname_internal (int fd, char *buf, size_t buflen, struct stat64 *stp)
Packit 6c4009
{
Packit 6c4009
  int save_errno = errno;
Packit 6c4009
  unsigned int ptyno;
Packit 6c4009
Packit 6c4009
  if (!__isatty (fd))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (ENOTTY);
Packit 6c4009
      return ENOTTY;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifdef TIOCGPTN
Packit 6c4009
  if (__ioctl (fd, TIOCGPTN, &ptyno) == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Buffer we use to print the number in.  For a maximum size for
Packit 6c4009
	 `int' of 8 bytes we never need more than 20 digits.  */
Packit 6c4009
      char numbuf[21];
Packit 6c4009
      const char *devpts = _PATH_DEVPTS;
Packit 6c4009
      const size_t devptslen = strlen (_PATH_DEVPTS);
Packit 6c4009
      char *p;
Packit 6c4009
Packit 6c4009
      numbuf[sizeof (numbuf) - 1] = '\0';
Packit 6c4009
      p = _itoa_word (ptyno, &numbuf[sizeof (numbuf) - 1], 10, 0);
Packit 6c4009
Packit 6c4009
      if (buflen < devptslen + (&numbuf[sizeof (numbuf)] - p))
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ERANGE);
Packit 6c4009
	  return ERANGE;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      memcpy (__stpcpy (buf, devpts), p, &numbuf[sizeof (numbuf)] - p);
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EINVAL)
Packit 6c4009
    return errno;
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
    {
Packit 6c4009
      char *p;
Packit 6c4009
Packit 6c4009
      if (buflen < strlen (_PATH_TTY) + 3)
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ERANGE);
Packit 6c4009
	  return ERANGE;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (__fxstat64 (_STAT_VER, fd, stp) < 0)
Packit 6c4009
	return errno;
Packit 6c4009
Packit 6c4009
      /* Check if FD really is a master pseudo terminal.  */
Packit 6c4009
      if (! MASTER_P (stp->st_rdev))
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ENOTTY);
Packit 6c4009
	  return ENOTTY;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      ptyno = __gnu_dev_minor (stp->st_rdev);
Packit 6c4009
Packit 6c4009
      if (ptyno / 16 >= strlen (__libc_ptyname1))
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ENOTTY);
Packit 6c4009
	  return ENOTTY;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      p = __stpcpy (buf, _PATH_TTY);
Packit 6c4009
      p[0] = __libc_ptyname1[ptyno / 16];
Packit 6c4009
      p[1] = __libc_ptyname2[ptyno % 16];
Packit 6c4009
      p[2] = '\0';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (__xstat64 (_STAT_VER, buf, stp) < 0)
Packit 6c4009
    return errno;
Packit 6c4009
Packit 6c4009
  /* Check if the name we're about to return really corresponds to a
Packit 6c4009
     slave pseudo terminal.  */
Packit 6c4009
  if (! S_ISCHR (stp->st_mode) || ! SLAVE_P (stp->st_rdev))
Packit 6c4009
    {
Packit 6c4009
      /* This really is a configuration problem.  */
Packit 6c4009
      __set_errno (ENOTTY);
Packit 6c4009
      return ENOTTY;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __set_errno (save_errno);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Store at most BUFLEN characters of the pathname of the slave pseudo
Packit 6c4009
   terminal associated with the master FD is open on in BUF.
Packit 6c4009
   Return 0 on success, otherwise an error number.  */
Packit 6c4009
int
Packit 6c4009
__ptsname_r (int fd, char *buf, size_t buflen)
Packit 6c4009
{
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  return __ptsname_internal (fd, buf, buflen, &st);
Packit 6c4009
}
Packit 6c4009
weak_alias (__ptsname_r, ptsname_r)