Updated to fedora-glibc-20080802T0809
[glibc/history.git] / sysdeps / unix / sysv / linux / ptsname.c
blob9c364b18b4df18d44c79f35adc27c42d0bd1abe7
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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>
30 #include <stdio-common/_itoa.h>
32 /* Check if DEV corresponds to a master pseudo terminal device. */
33 #define MASTER_P(Dev) \
34 (major ((Dev)) == 2 \
35 || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
36 || (major ((Dev)) >= 128 && major ((Dev)) < 136))
38 /* Check if DEV corresponds to a slave pseudo terminal device. */
39 #define SLAVE_P(Dev) \
40 (major ((Dev)) == 3 \
41 || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
42 || (major ((Dev)) >= 136 && major ((Dev)) < 144))
44 /* Note that major number 4 corresponds to the old BSD style pseudo
45 terminal devices. As of Linux 2.1.115 these are no longer
46 supported. They have been replaced by major numbers 2 (masters)
47 and 3 (slaves). */
49 /* Directory where we can find the slave pty nodes. */
50 #define _PATH_DEVPTS "/dev/pts/"
52 /* The are declared in getpt.c. */
53 extern const char __libc_ptyname1[] attribute_hidden;
54 extern const char __libc_ptyname2[] attribute_hidden;
56 /* Static buffer for `ptsname'. */
57 static char buffer[sizeof (_PATH_DEVPTS) + 20];
60 /* Return the pathname of the pseudo terminal slave assoicated with
61 the master FD is open on, or NULL on errors.
62 The returned storage is good until the next call to this function. */
63 char *
64 ptsname (int fd)
66 return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
70 /* Store at most BUFLEN characters of the pathname of the slave pseudo
71 terminal associated with the master FD is open on in BUF.
72 Return 0 on success, otherwise an error number. */
73 int
74 __ptsname_r (int fd, char *buf, size_t buflen)
76 int save_errno = errno;
77 struct stat64 st;
78 unsigned int ptyno;
80 if (buf == NULL)
82 __set_errno (EINVAL);
83 return EINVAL;
86 if (!__isatty (fd))
88 __set_errno (ENOTTY);
89 return ENOTTY;
92 #ifdef TIOCGPTN
93 if (__ioctl (fd, TIOCGPTN, &ptyno) == 0)
95 /* Buffer we use to print the number in. For a maximum size for
96 `int' of 8 bytes we never need more than 20 digits. */
97 char numbuf[21];
98 const char *devpts = _PATH_DEVPTS;
99 const size_t devptslen = strlen (_PATH_DEVPTS);
100 char *p;
102 numbuf[sizeof (numbuf) - 1] = '\0';
103 p = _itoa_word (ptyno, &numbuf[sizeof (numbuf) - 1], 10, 0);
105 if (buflen < devptslen + (&numbuf[sizeof (numbuf)] - p))
107 __set_errno (ERANGE);
108 return ERANGE;
111 memcpy (__stpcpy (buf, devpts), p, &numbuf[sizeof (numbuf)] - p);
113 else if (errno == EINVAL)
114 #endif
116 char *p;
118 if (buflen < strlen (_PATH_TTY) + 3)
120 __set_errno (ERANGE);
121 return ERANGE;
124 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
125 return errno;
127 /* Check if FD really is a master pseudo terminal. */
128 if (! MASTER_P (st.st_rdev))
130 __set_errno (ENOTTY);
131 return ENOTTY;
134 ptyno = minor (st.st_rdev);
135 /* This is for the old BSD pseudo terminals. As of Linux
136 2.1.115 these are no longer supported. */
137 if (major (st.st_rdev) == 4)
138 ptyno -= 128;
140 if (ptyno / 16 >= strlen (__libc_ptyname1))
142 __set_errno (ENOTTY);
143 return ENOTTY;
146 p = __stpcpy (buf, _PATH_TTY);
147 p[0] = __libc_ptyname1[ptyno / 16];
148 p[1] = __libc_ptyname2[ptyno % 16];
149 p[2] = '\0';
152 if (__xstat64 (_STAT_VER, buf, &st) < 0)
153 return errno;
155 /* Check if the name we're about to return really corresponds to a
156 slave pseudo terminal. */
157 if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
159 /* This really is a configuration problem. */
160 __set_errno (ENOTTY);
161 return ENOTTY;
164 __set_errno (save_errno);
165 return 0;
167 weak_alias (__ptsname_r, ptsname_r)