Back to index

glibc  2.9
ttyname.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,96,97,98,2000,2002 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 char *__ttyname;
00030 
00031 static char *getttyname (int fd, dev_t mydev, ino_t myino,
00032                       int save, int *dostat) internal_function;
00033 
00034 
00035 libc_freeres_ptr (static char *getttyname_name);
00036 
00037 static char *
00038 internal_function
00039 getttyname (fd, mydev, myino, save, dostat)
00040      int fd;
00041      dev_t mydev;
00042      ino_t myino;
00043      int save;
00044      int *dostat;
00045 {
00046   static const char dev[] = "/dev";
00047   static size_t namelen;
00048   struct stat st;
00049   DIR *dirstream;
00050   struct dirent *d;
00051 
00052   dirstream = __opendir (dev);
00053   if (dirstream == NULL)
00054     {
00055       *dostat = -1;
00056       return NULL;
00057     }
00058 
00059   while ((d = __readdir (dirstream)) != NULL)
00060     if (((ino_t) d->d_fileno == myino || *dostat)
00061        && strcmp (d->d_name, "stdin")
00062        && strcmp (d->d_name, "stdout")
00063        && strcmp (d->d_name, "stderr"))
00064       {
00065        size_t dlen = _D_ALLOC_NAMLEN (d);
00066        if (sizeof (dev) + dlen > namelen)
00067          {
00068            free (getttyname_name);
00069            namelen = 2 * (sizeof (dev) + dlen); /* Big enough.  */
00070            getttyname_name = malloc (namelen);
00071            if (! getttyname_name)
00072              {
00073               *dostat = -1;
00074               /* Perhaps it helps to free the directory stream buffer.  */
00075               (void) __closedir (dirstream);
00076               return NULL;
00077              }
00078            *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1))
00079              = '/';
00080          }
00081        (void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen);
00082        if (stat (getttyname_name, &st) == 0
00083 #ifdef _STATBUF_ST_RDEV
00084            && S_ISCHR (st.st_mode) && st.st_rdev == mydev
00085 #else
00086            && (ino_t) d->d_fileno == myino && st.st_dev == mydev
00087 #endif
00088           )
00089          {
00090            (void) __closedir (dirstream);
00091            __ttyname = getttyname_name;
00092            __set_errno (save);
00093            return getttyname_name;
00094          }
00095       }
00096 
00097   (void) __closedir (dirstream);
00098   __set_errno (save);
00099   return NULL;
00100 }
00101 
00102 /* Return the pathname of the terminal FD is open on, or NULL on errors.
00103    The returned storage is good only until the next call to this function.  */
00104 char *
00105 ttyname (fd)
00106      int fd;
00107 {
00108   struct stat st;
00109   int dostat = 0;
00110   char *name;
00111   int save = errno;
00112 
00113   if (!__isatty (fd))
00114     return NULL;
00115 
00116   if (fstat (fd, &st) < 0)
00117     return NULL;
00118 
00119 #ifdef _STATBUF_ST_RDEV
00120   name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
00121 #else
00122   name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
00123 #endif
00124 
00125   if (!name && dostat != -1)
00126     {
00127       dostat = 1;
00128 #ifdef _STATBUF_ST_RDEV
00129       name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
00130 #else
00131       name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
00132 #endif
00133     }
00134 
00135   return name;
00136 }