Back to index

glibc  2.9
ttyname_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,95,96,97,98,2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <limits.h>
00021 #include <stddef.h>
00022 #include <dirent.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <unistd.h>
00026 #include <string.h>
00027 #include <stdlib.h>
00028 
00029 #ifndef MIN
00030 # define MIN(a, b) ((a) < (b) ? (a) : (b))
00031 #endif
00032 
00033 static const char dev[] = "/dev";
00034 
00035 static int getttyname_r (int fd, char *buf, size_t buflen,
00036                       dev_t mydev, ino_t myino, int save,
00037                       int *dostat) __THROW internal_function;
00038 
00039 static int
00040 internal_function
00041 getttyname_r (fd, buf, buflen, mydev, myino, save, dostat)
00042      int fd;
00043      char *buf;
00044      size_t buflen;
00045      dev_t mydev;
00046      ino_t myino;
00047      int save;
00048      int *dostat;
00049 {
00050   struct stat st;
00051   DIR *dirstream;
00052   struct dirent *d;
00053 
00054   dirstream = __opendir (dev);
00055   if (dirstream == NULL)
00056     {
00057       *dostat = -1;
00058       return errno;
00059     }
00060 
00061   while ((d = __readdir (dirstream)) != NULL)
00062     if (((ino_t) d->d_fileno == myino || *dostat)
00063        && strcmp (d->d_name, "stdin")
00064        && strcmp (d->d_name, "stdout")
00065        && strcmp (d->d_name, "stderr"))
00066       {
00067        char *cp;
00068        size_t needed = _D_EXACT_NAMLEN (d) + 1;
00069 
00070        if (needed > buflen)
00071          {
00072            *dostat = -1;
00073            (void) __closedir (dirstream);
00074            __set_errno (ERANGE);
00075            return ERANGE;
00076          }
00077 
00078        cp = __stpncpy (&buf[sizeof (dev)], d->d_name, needed);
00079        cp[0] = '\0';
00080 
00081        if (stat (buf, &st) == 0
00082 #ifdef _STATBUF_ST_RDEV
00083            && S_ISCHR (st.st_mode) && st.st_rdev == mydev
00084 #else
00085            && (ino_t) d->d_fileno == myino && st.st_dev == mydev
00086 #endif
00087           )
00088          {
00089            (void) __closedir (dirstream);
00090            __set_errno (save);
00091            return 0;
00092          }
00093       }
00094 
00095   (void) __closedir (dirstream);
00096   __set_errno (save);
00097   /* It is not clear what to return in this case.  `isatty' says FD
00098      refers to a TTY but no entry in /dev has this inode.  */
00099   return ENOTTY;
00100 }
00101 
00102 /* Store at most BUFLEN character of the pathname of the terminal FD is
00103    open on in BUF.  Return 0 on success,  otherwise an error number.  */
00104 int
00105 __ttyname_r (fd, buf, buflen)
00106      int fd;
00107      char *buf;
00108      size_t buflen;
00109 {
00110   struct stat st;
00111   int dostat = 0;
00112   int save = errno;
00113   int ret;
00114 
00115   /* Test for the absolute minimal size.  This makes life easier inside
00116      the loop.  */
00117   if (!buf)
00118     {
00119       __set_errno (EINVAL);
00120       return EINVAL;
00121     }
00122 
00123   if (buflen < (int) (sizeof (dev) + 1))
00124     {
00125       __set_errno (ERANGE);
00126       return ERANGE;
00127     }
00128 
00129   if (!__isatty (fd))
00130     {
00131       __set_errno (ENOTTY);
00132       return ENOTTY;
00133     }
00134 
00135   if (fstat (fd, &st) < 0)
00136     return errno;
00137 
00138   /* Prepare the result buffer.  */
00139   memcpy (buf, dev, sizeof (dev) - 1);
00140   buf[sizeof (dev) - 1] = '/';
00141   buflen -= sizeof (dev);
00142 
00143 #ifdef _STATBUF_ST_RDEV
00144   ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
00145                     &dostat);
00146 #else
00147   ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
00148                     &dostat);
00149 #endif
00150 
00151   if (ret && dostat != -1)
00152     {
00153       dostat = 1;
00154 #ifdef _STATBUF_ST_RDEV
00155       ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino,
00156                        save, &dostat);
00157 #else
00158       ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino,
00159                        save, &dostat);
00160 #endif
00161     }
00162 
00163   return ret;
00164 }
00165 
00166 weak_alias (__ttyname_r, ttyname_r)