source: rtems/c/src/lib/libc/ttyname.c @ 9c49db4

4.104.114.84.95
Last change on this file since 9c49db4 was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

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