Back to index

glibc  2.9
Defines | Functions | Variables
ttyname_r.c File Reference
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Defines

#define MIN(a, b)   ((a) < (b) ? (a) : (b))

Functions

static int getttyname_r (int fd, char *buf, size_t buflen, dev_t mydev, ino_t myino, int save, int *dostat) __THROW internal_function
int __ttyname_r (int fd, char *buf, size_t buflen)

Variables

static const char dev [] = "/dev"

Define Documentation

#define MIN (   a,
  b 
)    ((a) < (b) ? (a) : (b))

Definition at line 30 of file ttyname_r.c.


Function Documentation

int __ttyname_r ( int  fd,
char *  buf,
size_t  buflen 
)

Definition at line 105 of file ttyname_r.c.

{
  struct stat st;
  int dostat = 0;
  int save = errno;
  int ret;

  /* Test for the absolute minimal size.  This makes life easier inside
     the loop.  */
  if (!buf)
    {
      __set_errno (EINVAL);
      return EINVAL;
    }

  if (buflen < (int) (sizeof (dev) + 1))
    {
      __set_errno (ERANGE);
      return ERANGE;
    }

  if (!__isatty (fd))
    {
      __set_errno (ENOTTY);
      return ENOTTY;
    }

  if (fstat (fd, &st) < 0)
    return errno;

  /* Prepare the result buffer.  */
  memcpy (buf, dev, sizeof (dev) - 1);
  buf[sizeof (dev) - 1] = '/';
  buflen -= sizeof (dev);

#ifdef _STATBUF_ST_RDEV
  ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
                    &dostat);
#else
  ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
                    &dostat);
#endif

  if (ret && dostat != -1)
    {
      dostat = 1;
#ifdef _STATBUF_ST_RDEV
      ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino,
                       save, &dostat);
#else
      ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino,
                       save, &dostat);
#endif
    }

  return ret;
}

Here is the call graph for this function:

static int internal_function getttyname_r ( int  fd,
char *  buf,
size_t  buflen,
dev_t  mydev,
ino_t  myino,
int  save,
int dostat 
) [static]

Definition at line 41 of file ttyname_r.c.

{
  struct stat st;
  DIR *dirstream;
  struct dirent *d;

  dirstream = __opendir (dev);
  if (dirstream == NULL)
    {
      *dostat = -1;
      return errno;
    }

  while ((d = __readdir (dirstream)) != NULL)
    if (((ino_t) d->d_fileno == myino || *dostat)
       && strcmp (d->d_name, "stdin")
       && strcmp (d->d_name, "stdout")
       && strcmp (d->d_name, "stderr"))
      {
       char *cp;
       size_t needed = _D_EXACT_NAMLEN (d) + 1;

       if (needed > buflen)
         {
           *dostat = -1;
           (void) __closedir (dirstream);
           __set_errno (ERANGE);
           return ERANGE;
         }

       cp = __stpncpy (&buf[sizeof (dev)], d->d_name, needed);
       cp[0] = '\0';

       if (stat (buf, &st) == 0
#ifdef _STATBUF_ST_RDEV
           && S_ISCHR (st.st_mode) && st.st_rdev == mydev
#else
           && (ino_t) d->d_fileno == myino && st.st_dev == mydev
#endif
          )
         {
           (void) __closedir (dirstream);
           __set_errno (save);
           return 0;
         }
      }

  (void) __closedir (dirstream);
  __set_errno (save);
  /* It is not clear what to return in this case.  `isatty' says FD
     refers to a TTY but no entry in /dev has this inode.  */
  return ENOTTY;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char dev[] = "/dev" [static]

Definition at line 33 of file ttyname_r.c.