source: rtems/c/src/lib/libc/ttyname.c @ acc25ee

4.104.114.84.95
Last change on this file since acc25ee was e4b25c4, checked in by Joel Sherrill <joel.sherrill@…>, on 11/30/99 at 13:46:28

Corrected so they would compile with POSIX disabled.

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/*
2 *  ttyname_r() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
3 *
4 *  $Id$
5 */
6
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <dirent.h>
11#include <termios.h>
12#include <unistd.h>
13#include <paths.h>
14#include <_syslist.h>
15#include <errno.h>
16
17#include "libio_.h"
18
19
20int ttyname_r(
21  int   fd,
22  char *name,
23  int   namesize
24)
25{
26  struct stat sb;
27  struct termios tty;
28  struct dirent *dirp;
29  DIR *dp;
30  struct stat dsb;
31  char *rval;
32
33  /* Must be a terminal. */
34  if (tcgetattr (fd, &tty) < 0)
35    set_errno_and_return_minus_one(EBADF);
36
37  /* Must be a character device. */
38  if (_fstat (fd, &sb) || !S_ISCHR (sb.st_mode))
39    set_errno_and_return_minus_one(EBADF);
40
41  if ((dp = opendir (_PATH_DEV)) == NULL)
42    set_errno_and_return_minus_one(EBADF);
43
44  for (rval = NULL; (dirp = readdir (dp)) != NULL ;)
45    {
46      if (dirp->d_ino != sb.st_ino)
47        continue;
48      strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name);
49      if (stat (name, &dsb) || sb.st_dev != dsb.st_dev ||
50          sb.st_ino != dsb.st_ino)
51        continue;
52      (void) closedir (dp);
53      rval = name;
54      break;
55    }
56  (void) closedir (dp);
57  return 0;
58}
59
60static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
61
62/*
63 *  ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
64 */
65
66char *ttyname(
67  int fd
68)
69{
70  if ( !ttyname_r( fd, buf, sizeof(buf) ) )
71    return buf;
72  return NULL;
73}
74
Note: See TracBrowser for help on using the repository browser.