Back to index

glibc  2.9
ptsname.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <paths.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <sys/ioctl.h>
00025 #include <sys/stat.h>
00026 #include <sys/sysmacros.h>
00027 #include <termios.h>
00028 #include <unistd.h>
00029 
00030 #include <stdio-common/_itoa.h>
00031 
00032 /* Check if DEV corresponds to a master pseudo terminal device.  */
00033 #define MASTER_P(Dev)                                                         \
00034   (major ((Dev)) == 2                                                         \
00035    || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192)     \
00036    || (major ((Dev)) >= 128 && major ((Dev)) < 136))
00037 
00038 /* Check if DEV corresponds to a slave pseudo terminal device.  */
00039 #define SLAVE_P(Dev)                                                          \
00040   (major ((Dev)) == 3                                                         \
00041    || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256)     \
00042    || (major ((Dev)) >= 136 && major ((Dev)) < 144))
00043 
00044 /* Note that major number 4 corresponds to the old BSD style pseudo
00045    terminal devices.  As of Linux 2.1.115 these are no longer
00046    supported.  They have been replaced by major numbers 2 (masters)
00047    and 3 (slaves).  */
00048 
00049 /* Directory where we can find the slave pty nodes.  */
00050 #define _PATH_DEVPTS "/dev/pts/"
00051 
00052 /* The are declared in getpt.c.  */
00053 extern const char __libc_ptyname1[] attribute_hidden;
00054 extern const char __libc_ptyname2[] attribute_hidden;
00055 
00056 /* Static buffer for `ptsname'.  */
00057 static char buffer[sizeof (_PATH_DEVPTS) + 20];
00058 
00059 
00060 /* Return the pathname of the pseudo terminal slave assoicated with
00061    the master FD is open on, or NULL on errors.
00062    The returned storage is good until the next call to this function.  */
00063 char *
00064 ptsname (int fd)
00065 {
00066   return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
00067 }
00068 
00069 
00070 /* Store at most BUFLEN characters of the pathname of the slave pseudo
00071    terminal associated with the master FD is open on in BUF.
00072    Return 0 on success, otherwise an error number.  */
00073 int
00074 __ptsname_r (int fd, char *buf, size_t buflen)
00075 {
00076   int save_errno = errno;
00077   struct stat64 st;
00078   unsigned int ptyno;
00079 
00080   if (buf == NULL)
00081     {
00082       __set_errno (EINVAL);
00083       return EINVAL;
00084     }
00085 
00086   if (!__isatty (fd))
00087     {
00088       __set_errno (ENOTTY);
00089       return ENOTTY;
00090     }
00091 
00092 #ifdef TIOCGPTN
00093   if (__ioctl (fd, TIOCGPTN, &ptyno) == 0)
00094     {
00095       /* Buffer we use to print the number in.  For a maximum size for
00096          `int' of 8 bytes we never need more than 20 digits.  */
00097       char numbuf[21];
00098       const char *devpts = _PATH_DEVPTS;
00099       const size_t devptslen = strlen (_PATH_DEVPTS);
00100       char *p;
00101 
00102       numbuf[sizeof (numbuf) - 1] = '\0';
00103       p = _itoa_word (ptyno, &numbuf[sizeof (numbuf) - 1], 10, 0);
00104 
00105       if (buflen < devptslen + (&numbuf[sizeof (numbuf)] - p))
00106        {
00107          __set_errno (ERANGE);
00108          return ERANGE;
00109        }
00110 
00111       memcpy (__stpcpy (buf, devpts), p, &numbuf[sizeof (numbuf)] - p);
00112     }
00113   else if (errno == EINVAL)
00114 #endif
00115     {
00116       char *p;
00117 
00118       if (buflen < strlen (_PATH_TTY) + 3)
00119        {
00120          __set_errno (ERANGE);
00121          return ERANGE;
00122        }
00123 
00124       if (__fxstat64 (_STAT_VER, fd, &st) < 0)
00125        return errno;
00126 
00127       /* Check if FD really is a master pseudo terminal.  */
00128       if (! MASTER_P (st.st_rdev))
00129        {
00130          __set_errno (ENOTTY);
00131          return ENOTTY;
00132        }
00133 
00134       ptyno = minor (st.st_rdev);
00135       /* This is for the old BSD pseudo terminals.  As of Linux
00136          2.1.115 these are no longer supported.  */
00137       if (major (st.st_rdev) == 4)
00138        ptyno -= 128;
00139 
00140       if (ptyno / 16 >= strlen (__libc_ptyname1))
00141        {
00142          __set_errno (ENOTTY);
00143          return ENOTTY;
00144        }
00145 
00146       p = __stpcpy (buf, _PATH_TTY);
00147       p[0] = __libc_ptyname1[ptyno / 16];
00148       p[1] = __libc_ptyname2[ptyno % 16];
00149       p[2] = '\0';
00150     }
00151 
00152   if (__xstat64 (_STAT_VER, buf, &st) < 0)
00153     return errno;
00154 
00155   /* Check if the name we're about to return really corresponds to a
00156      slave pseudo terminal.  */
00157   if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
00158     {
00159       /* This really is a configuration problem.  */
00160       __set_errno (ENOTTY);
00161       return ENOTTY;
00162     }
00163 
00164   __set_errno (save_errno);
00165   return 0;
00166 }
00167 weak_alias (__ptsname_r, ptsname_r)