c81f23504cc7fac58e6cb6eb825daa7a108e5e75
[platform/upstream/linaro-glibc.git] / sysdeps / unix / sysv / linux / ptsname.c
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.
4
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.
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    Library General Public License for more details.
14
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.  */
19
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <termios.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include "pty-internal.h"
30
31 #include <stdio-common/_itoa.h>
32 #include <sys/sysmacros.h>
33
34 /* Given the file descriptor of a master pty, return the pathname
35    of the associated slave.  */
36
37 static char namebuf[PTYNAMELEN];
38 extern const char __ptyname1[], __ptyname2[]; /* Defined in getpt.c.  */
39
40 char *
41 ptsname (fd)
42      int fd;
43 {
44   return __ptsname_r (fd, namebuf, PTYNAMELEN) != 0 ? NULL : namebuf;
45 }
46
47 int
48 __ptsname_r (fd, buf, buflen)
49      int fd;
50      char *buf;
51      size_t buflen;
52 {
53   struct stat st;
54   int save = errno;
55   int ptyno;
56   char nbuf[PTYNAMELEN], idbuf[6];
57   char *cp;
58
59 #ifdef TIOCGPTN
60   static int tiocgptn_works = 1;
61 #endif
62
63   if (!buf)
64     {
65       __set_errno (EINVAL);
66       return EINVAL;
67     }
68
69   if (!__isatty (fd))
70     {
71       __set_errno (ENOTTY);
72       return ENOTTY;
73     }
74
75 #ifdef TIOCGPTN
76   if (tiocgptn_works)
77     {
78       if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
79         goto gotit;
80       else
81         {
82           if(errno != EINVAL)
83             return errno;
84           else
85             tiocgptn_works = 0;
86         }
87     }
88 #endif
89   if (__fxstat (_STAT_VER, fd, &st) < 0)
90     return errno;
91
92   ptyno = minor (st.st_rdev);
93   if (major (st.st_rdev) == 4)
94     ptyno -= 128;
95
96 #ifdef TIOCGPTN
97  gotit:
98 #endif
99   /* Two different possible naming schemes for pty slaves:
100      the SVr4 way.  */
101
102   idbuf[5] = '\0';
103   __stpcpy (__stpcpy (nbuf, "/dev/pts/"),
104             _itoa_word (ptyno, &idbuf[4], 10, 0));
105   if (__xstat (_STAT_VER, nbuf, &st) < 0)
106     {
107       if (errno != ENOENT)
108         return errno;
109
110       /* ...and the BSD way.  */
111       nbuf[5]  = 't';
112       nbuf[7]  = 'y';
113       nbuf[8]  = __ptyname1[ptyno / 16];
114       nbuf[9]  = __ptyname2[ptyno % 16];
115       nbuf[10] = '\0';
116
117       if (__xstat (_STAT_VER, nbuf, &st) < 0)
118         return errno;
119     }
120
121   if (buflen < strlen (nbuf) + 1)
122     {
123       __set_errno (ERANGE);
124       return ERANGE;
125     }
126
127   cp = __stpncpy (buf, nbuf, buflen);
128   cp[0] = '\0';
129
130   __set_errno (save);
131   return 0;
132 }
133 weak_alias (__ptsname_r, ptsname_r)