bump version to 1.0.28
[uclibc-ng.git] / libc / stdlib / ptsname.c
blob2d3b3173149114f9ce2ac98430d235a95c1ef909
1 /* Copyright (C) 1998, 2000, 2001, 2002 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 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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <errno.h>
21 #include <paths.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <sys/stat.h>
26 #include <sys/sysmacros.h>
27 #include <termios.h>
28 #include <unistd.h>
29 #include <bits/uClibc_uintmaxtostr.h>
32 #if !defined __UNIX98PTY_ONLY__
34 /* Check if DEV corresponds to a master pseudo terminal device. */
35 #define MASTER_P(Dev) \
36 (major ((Dev)) == 2 \
37 || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
38 || (major ((Dev)) >= 128 && major ((Dev)) < 136))
40 /* Check if DEV corresponds to a slave pseudo terminal device. */
41 #define SLAVE_P(Dev) \
42 (major ((Dev)) == 3 \
43 || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
44 || (major ((Dev)) >= 136 && major ((Dev)) < 144))
46 /* Note that major number 4 corresponds to the old BSD style pseudo
47 terminal devices. As of Linux 2.1.115 these are no longer
48 supported. They have been replaced by major numbers 2 (masters)
49 and 3 (slaves). */
51 /* The are declared in getpt.c. */
52 extern const char __libc_ptyname1[] attribute_hidden;
53 extern const char __libc_ptyname2[] attribute_hidden;
55 #endif
57 /* Directory where we can find the slave pty nodes. */
58 #define _PATH_DEVPTS "/dev/pts/"
60 /* Store at most BUFLEN characters of the pathname of the slave pseudo
61 terminal associated with the master FD is open on in BUF.
62 Return 0 on success, otherwise an error number. */
63 int ptsname_r (int fd, char *buf, size_t buflen)
65 int save_errno = errno;
66 #if !defined __UNIX98PTY_ONLY__
67 struct stat st;
68 #endif
69 int ptyno;
71 if (buf == NULL)
73 errno = EINVAL;
74 return EINVAL;
77 #if !defined __UNIX98PTY_ONLY__
78 if (!isatty (fd))
80 errno = ENOTTY;
81 return ENOTTY;
83 #elif !defined TIOCGPTN
84 # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
85 #endif
86 #ifdef TIOCGPTN
87 if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
89 /* Buffer we use to print the number in. */
90 char numbuf[__BUFLEN_INT10TOSTR];
91 static const char devpts[] = _PATH_DEVPTS;
92 char *p;
94 p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
96 if (buflen < sizeof(devpts) + (size_t)(&numbuf[sizeof(numbuf) - 1] - p))
98 errno = ERANGE;
99 return ERANGE;
102 strcpy (buf, devpts);
103 strcat (buf, p);
104 /* Note: Don't bother with stat on the slave name and checking the
105 driver's major device number - the ioctl above succeeded so
106 we know the fd was a Unix'98 master and the /dev/pts/ prefix
107 is set by definition. If the name isn't really a slave PTY,
108 the system is misconfigured anyway - something else will fail
109 later.
111 errno = save_errno;
112 return 0;
114 #endif
115 #if defined __UNIX98PTY_ONLY__
116 else
118 /* If the ioctl fails it wasn't a Unix 98 master PTY */
119 errno = ENOTTY;
120 return ENOTTY;
122 #else
123 # if defined TIOCGPTN
124 else if (errno == EINVAL)
125 # endif
127 char *p;
129 if (buflen < strlen (_PATH_TTY) + 3)
131 errno = ERANGE;
132 return ERANGE;
135 if (fstat (fd, &st) < 0)
136 return errno;
138 /* Check if FD really is a master pseudo terminal. */
139 if (! MASTER_P (st.st_rdev))
141 errno = ENOTTY;
142 return ENOTTY;
145 ptyno = minor (st.st_rdev);
146 /* This is for the old BSD pseudo terminals. As of Linux
147 2.1.115 these are no longer supported. */
148 if (major (st.st_rdev) == 4)
149 ptyno -= 128;
151 if (ptyno / 16 >= strlen (__libc_ptyname1))
153 errno = ENOTTY;
154 return ENOTTY;
157 strcpy (buf, _PATH_TTY);
158 p = buf + strlen (buf);
159 p[0] = __libc_ptyname1[ptyno / 16];
160 p[1] = __libc_ptyname2[ptyno % 16];
161 p[2] = '\0';
164 if (stat(buf, &st) < 0)
165 return errno;
167 /* Check if the name we're about to return really corresponds to a
168 slave pseudo terminal. */
169 if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
171 /* This really is a configuration problem. */
172 errno = ENOTTY;
173 return ENOTTY;
175 #endif
177 errno = save_errno;
178 return 0;
180 libc_hidden_def(ptsname_r)
182 /* Return the pathname of the pseudo terminal slave assoicated with
183 the master FD is open on, or NULL on errors.
184 The returned storage is good until the next call to this function. */
185 char *
186 ptsname (int fd)
188 static char buffer[sizeof (_PATH_DEVPTS) + 20];
190 return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;