Blame sysdeps/posix/ttyname.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
char *__ttyname;
Packit 6c4009
Packit 6c4009
static char *getttyname (int fd, dev_t mydev, ino_t myino,
Packit 6c4009
			 int save, int *dostat);
Packit 6c4009
Packit 6c4009
Packit 6c4009
libc_freeres_ptr (static char *getttyname_name);
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
getttyname (int fd, dev_t mydev, ino_t myino, int save, int *dostat)
Packit 6c4009
{
Packit 6c4009
  static const char dev[] = "/dev";
Packit 6c4009
  static size_t namelen;
Packit 6c4009
  struct stat st;
Packit 6c4009
  DIR *dirstream;
Packit 6c4009
  struct dirent *d;
Packit 6c4009
Packit 6c4009
  dirstream = __opendir (dev);
Packit 6c4009
  if (dirstream == NULL)
Packit 6c4009
    {
Packit 6c4009
      *dostat = -1;
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  while ((d = __readdir (dirstream)) != NULL)
Packit 6c4009
    if (((ino_t) d->d_fileno == myino || *dostat)
Packit 6c4009
	&& strcmp (d->d_name, "stdin")
Packit 6c4009
	&& strcmp (d->d_name, "stdout")
Packit 6c4009
	&& strcmp (d->d_name, "stderr"))
Packit 6c4009
      {
Packit 6c4009
	size_t dlen = _D_ALLOC_NAMLEN (d);
Packit 6c4009
	if (sizeof (dev) + dlen > namelen)
Packit 6c4009
	  {
Packit 6c4009
	    free (getttyname_name);
Packit 6c4009
	    namelen = 2 * (sizeof (dev) + dlen); /* Big enough.  */
Packit 6c4009
	    getttyname_name = malloc (namelen);
Packit 6c4009
	    if (! getttyname_name)
Packit 6c4009
	      {
Packit 6c4009
		*dostat = -1;
Packit 6c4009
		/* Perhaps it helps to free the directory stream buffer.  */
Packit 6c4009
		(void) __closedir (dirstream);
Packit 6c4009
		return NULL;
Packit 6c4009
	      }
Packit 6c4009
	    *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1))
Packit 6c4009
	      = '/';
Packit 6c4009
	  }
Packit 6c4009
	(void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen);
Packit 6c4009
	if (stat (getttyname_name, &st) == 0
Packit 6c4009
#ifdef _STATBUF_ST_RDEV
Packit 6c4009
	    && S_ISCHR (st.st_mode) && st.st_rdev == mydev
Packit 6c4009
#else
Packit 6c4009
	    && (ino_t) d->d_fileno == myino && st.st_dev == mydev
Packit 6c4009
#endif
Packit 6c4009
	   )
Packit 6c4009
	  {
Packit 6c4009
	    (void) __closedir (dirstream);
Packit 6c4009
	    __ttyname = getttyname_name;
Packit 6c4009
	    __set_errno (save);
Packit 6c4009
	    return getttyname_name;
Packit 6c4009
	  }
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  (void) __closedir (dirstream);
Packit 6c4009
  __set_errno (save);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the pathname of the terminal FD is open on, or NULL on errors.
Packit 6c4009
   The returned storage is good only until the next call to this function.  */
Packit 6c4009
char *
Packit 6c4009
ttyname (int fd)
Packit 6c4009
{
Packit 6c4009
  struct stat st;
Packit 6c4009
  int dostat = 0;
Packit 6c4009
  char *name;
Packit 6c4009
  int save = errno;
Packit 6c4009
Packit 6c4009
  if (!__isatty (fd))
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  if (fstat (fd, &st) < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
#ifdef _STATBUF_ST_RDEV
Packit 6c4009
  name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
Packit 6c4009
#else
Packit 6c4009
  name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (!name && dostat != -1)
Packit 6c4009
    {
Packit 6c4009
      dostat = 1;
Packit 6c4009
#ifdef _STATBUF_ST_RDEV
Packit 6c4009
      name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
Packit 6c4009
#else
Packit 6c4009
      name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return name;
Packit 6c4009
}