1 /* Copyright (C) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
29 #include "pty-internal.h"
31 #include <stdio-common/_itoa.h>
32 #include <sys/sysmacros.h>
34 /* Given the file descriptor of a master pty, return the pathname
35 of the associated slave. */
37 static char namebuf[PTYNAMELEN];
38 extern const char __ptyname1[], __ptyname2[]; /* Defined in getpt.c. */
44 return __ptsname_r (fd, namebuf, PTYNAMELEN) != 0 ? NULL : namebuf;
48 __ptsname_r (fd, buf, buflen)
56 char nbuf[PTYNAMELEN], idbuf[6];
60 static int tiocgptn_works = 1;
78 if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
89 if (__fxstat (_STAT_VER, fd, &st) < 0)
92 ptyno = minor (st.st_rdev);
93 if (major (st.st_rdev) == 4)
99 /* Two different possible naming schemes for pty slaves:
103 __stpcpy (__stpcpy (nbuf, "/dev/pts/"),
104 _itoa_word (ptyno, &idbuf[5], 10, 0));
105 if (__xstat (_STAT_VER, nbuf, &st) < 0)
110 /* ...and the BSD way. */
113 nbuf[8] = __ptyname1[ptyno / 16];
114 nbuf[9] = __ptyname2[ptyno % 16];
117 if (__xstat (_STAT_VER, nbuf, &st) < 0)
121 if (buflen < strlen (nbuf) + 1)
123 __set_errno (ERANGE);
127 cp = __stpncpy (buf, nbuf, buflen);
133 weak_alias (__ptsname_r, ptsname_r)