OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / stdlib / ptsname.c
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.
4
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.
9
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.
14
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.  */
19
20 #define _ISOC99_SOURCE
21 #include <stdio.h>
22 #include <errno.h>
23 #include <paths.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 #include <termios.h>
30 #include <unistd.h>
31 #include <bits/uClibc_uintmaxtostr.h>
32
33 libc_hidden_proto(strcat)
34 libc_hidden_proto(strcpy)
35 libc_hidden_proto(strlen)
36 libc_hidden_proto(isatty)
37 libc_hidden_proto(ioctl)
38 libc_hidden_proto(fstat)
39 libc_hidden_proto(stat)
40
41 #if !defined __UNIX98PTY_ONLY__
42
43 /* Check if DEV corresponds to a master pseudo terminal device.  */
44 #define MASTER_P(Dev)                                                         \
45   (major ((Dev)) == 2                                                         \
46    || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192)     \
47    || (major ((Dev)) >= 128 && major ((Dev)) < 136))
48
49 /* Check if DEV corresponds to a slave pseudo terminal device.  */
50 #define SLAVE_P(Dev)                                                          \
51   (major ((Dev)) == 3                                                         \
52    || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256)     \
53    || (major ((Dev)) >= 136 && major ((Dev)) < 144))
54
55 /* Note that major number 4 corresponds to the old BSD style pseudo
56    terminal devices.  As of Linux 2.1.115 these are no longer
57    supported.  They have been replaced by major numbers 2 (masters)
58    and 3 (slaves).  */
59      
60 /* The are declared in getpt.c.  */
61 extern const char __libc_ptyname1[] attribute_hidden;
62 extern const char __libc_ptyname2[] attribute_hidden;
63
64 #endif
65
66 /* Directory where we can find the slave pty nodes.  */
67 #define _PATH_DEVPTS "/dev/pts/"
68
69 /* Store at most BUFLEN characters of the pathname of the slave pseudo
70    terminal associated with the master FD is open on in BUF.
71    Return 0 on success, otherwise an error number.  */
72 int ptsname_r (int fd, char *buf, size_t buflen)
73 {
74   int save_errno = errno;
75 #if !defined __UNIX98PTY_ONLY__
76   struct stat st;
77 #endif
78   int ptyno;
79
80   if (buf == NULL)
81     {
82       errno = EINVAL;
83       return EINVAL;
84     }
85
86 #if !defined __UNIX98PTY_ONLY__
87   if (!isatty (fd))
88     {
89       errno = ENOTTY;
90       return ENOTTY;
91     }
92 #elif !defined TIOCGPTN
93 # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
94 #endif
95 #ifdef TIOCGPTN
96   if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
97     {
98       /* Buffer we use to print the number in. */
99       char numbuf[__BUFLEN_INT10TOSTR];
100       static const char devpts[] = _PATH_DEVPTS;
101       char *p;
102
103       p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
104
105       if (buflen < sizeof(devpts) + (size_t)(&numbuf[sizeof(numbuf) - 1] - p))
106         {
107           errno = ERANGE;
108           return ERANGE;
109         }
110
111       strcpy (buf, devpts);
112       strcat (buf, p);
113       /* Note: Don't bother with stat on the slave name and checking the
114          driver's major device number - the ioctl above succeeded so
115          we know the fd was a Unix'98 master and the /dev/pts/ prefix
116          is set by definition.  If the name isn't really a slave PTY,
117          the system is misconfigured anyway - something else will fail
118          later.
119          */
120       errno = save_errno;
121       return 0;
122     }
123 #endif
124 #if defined __UNIX98PTY_ONLY__
125   else
126     {
127       /* If the ioctl fails it wasn't a Unix 98 master PTY */
128       errno = ENOTTY;
129       return ENOTTY;
130     }
131 #else
132 # if !defined TIOCGPTN
133   else if (errno == EINVAL)
134 # endif
135     {
136       char *p;
137
138       if (buflen < strlen (_PATH_TTY) + 3)
139         {
140           errno = ERANGE;
141           return ERANGE;
142         }
143
144       if (fstat (fd, &st) < 0)
145         return errno;
146
147       /* Check if FD really is a master pseudo terminal.  */
148       if (! MASTER_P (st.st_rdev))
149         {
150           errno = ENOTTY;
151           return ENOTTY;
152         }
153
154       ptyno = minor (st.st_rdev);
155       /* This is for the old BSD pseudo terminals.  As of Linux
156          2.1.115 these are no longer supported.  */
157       if (major (st.st_rdev) == 4)
158         ptyno -= 128;
159
160       if (ptyno / 16 >= strlen (__libc_ptyname1))
161         {
162           errno = ENOTTY;
163           return ENOTTY;
164         }
165
166       strcpy (buf, _PATH_TTY);
167       p = buf + strlen (buf);
168       p[0] = __libc_ptyname1[ptyno / 16];
169       p[1] = __libc_ptyname2[ptyno % 16];
170       p[2] = '\0';
171     }
172
173   if (stat(buf, &st) < 0)
174     return errno;
175
176   /* Check if the name we're about to return really corresponds to a
177      slave pseudo terminal.  */
178   if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
179     {
180       /* This really is a configuration problem.  */
181       errno = ENOTTY;
182       return ENOTTY;
183     }
184 #endif
185
186   errno = save_errno;
187   return 0;
188 }
189 libc_hidden_proto(ptsname_r)
190 libc_hidden_def(ptsname_r)
191
192 /* Return the pathname of the pseudo terminal slave assoicated with
193    the master FD is open on, or NULL on errors.
194    The returned storage is good until the next call to this function.  */
195 char *
196 ptsname (int fd)
197 {
198   static char buffer[sizeof (_PATH_DEVPTS) + 20];
199
200   return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
201 }