123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #define _ISOC99_SOURCE
- #include <stdio.h>
- #include <errno.h>
- #include <paths.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <sys/stat.h>
- #include <sys/sysmacros.h>
- #include <termios.h>
- #include <unistd.h>
- #include <bits/uClibc_uintmaxtostr.h>
- #if !defined __UNIX98PTY_ONLY__
- #define MASTER_P(Dev) \
- (major ((Dev)) == 2 \
- || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
- || (major ((Dev)) >= 128 && major ((Dev)) < 136))
- #define SLAVE_P(Dev) \
- (major ((Dev)) == 3 \
- || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
- || (major ((Dev)) >= 136 && major ((Dev)) < 144))
-
- extern const char _ptyname1[];
- extern const char _ptyname2[];
- #endif
- #define _PATH_DEVPTS "/dev/pts/"
- int ptsname_r (int fd, char *buf, size_t buflen)
- {
- int save_errno = errno;
- #if !defined __UNIX98PTY_ONLY__
- struct stat st;
- #endif
- int ptyno;
- if (buf == NULL)
- {
- errno = EINVAL;
- return EINVAL;
- }
- #if !defined __UNIX98PTY_ONLY__
- if (!isatty (fd))
- {
- errno = ENOTTY;
- return ENOTTY;
- }
- #elif !defined TIOCGPTN
- # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
- #endif
- #ifdef TIOCGPTN
- if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
- {
-
- char numbuf[__BUFLEN_INT10TOSTR];
- static const char devpts[] = _PATH_DEVPTS;
- char *p;
- p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
- if (buflen < sizeof devpts + &numbuf[sizeof numbuf - 1] - p)
- {
- errno = ERANGE;
- return ERANGE;
- }
- strcpy (buf, devpts);
- strcat (buf, p);
-
- errno = save_errno;
- return 0;
- }
- #endif
- #if defined __UNIX98PTY_ONLY__
- else
- {
-
- errno = ENOTTY;
- return ENOTTY;
- }
- #else
- # if !defined TIOCGPTN
- else if (errno == EINVAL)
- # endif
- {
- char *p;
- if (buflen < strlen (_PATH_TTY) + 3)
- {
- errno = ERANGE;
- return ERANGE;
- }
- if (fstat (fd, &st) < 0)
- return errno;
-
- if (! MASTER_P (st.st_rdev))
- {
- errno = ENOTTY;
- return ENOTTY;
- }
- ptyno = minor (st.st_rdev);
-
- if (major (st.st_rdev) == 4)
- ptyno -= 128;
- if (ptyno / 16 >= strlen (_ptyname1))
- {
- errno = ENOTTY;
- return ENOTTY;
- }
- strcpy (buf, _PATH_TTY);
- p = buf + strlen (buf);
- p[0] = _ptyname1[ptyno / 16];
- p[1] = _ptyname2[ptyno % 16];
- p[2] = '\0';
- }
- if (stat(buf, &st) < 0)
- return errno;
-
- if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
- {
-
- errno = ENOTTY;
- return ENOTTY;
- }
- #endif
- errno = save_errno;
- return 0;
- }
- char *
- ptsname (int fd)
- {
- static char buffer[sizeof (_PATH_DEVPTS) + 20];
- return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
- }
|