source: rtems/cpukit/libcsupport/src/ttyname.c @ 690861c

4.104.114.84.95
Last change on this file since 690861c was 47f7dc5, checked in by Joel Sherrill <joel.sherrill@…>, on 04/03/02 at 14:22:58

2002-04-02 Ralf Corsepius <corsepiu@…>

  • libc/ctermid.c: Include <string.h>.
  • libc/ttyname.c: Ditto.
  • libc/ttyname_r.c: Ditto.
  • Property mode set to 100644
File size: 1.5 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 <string.h>
18#include <paths.h>
19#include <_syslist.h>
20#include <errno.h>
21
22#include <rtems/libio_.h>
23#include <rtems/seterr.h>
24
25int ttyname_r(
26  int   fd,
27  char *name,
28  int   namesize
29)
30{
31  struct stat sb;
32  struct termios tty;
33  struct dirent *dirp;
34  DIR *dp;
35  struct stat dsb;
36  char *rval;
37
38  /* Must be a terminal. */
39  if (tcgetattr (fd, &tty) < 0)
40    rtems_set_errno_and_return_minus_one(EBADF);
41
42  /* Must be a character device. */
43  if (fstat (fd, &sb) || !S_ISCHR (sb.st_mode))
44    rtems_set_errno_and_return_minus_one(EBADF);
45
46  if ((dp = opendir (_PATH_DEV)) == NULL)
47    rtems_set_errno_and_return_minus_one(EBADF);
48
49  for (rval = NULL; (dirp = readdir (dp)) != NULL ;)
50    {
51      if (dirp->d_ino != sb.st_ino)
52        continue;
53      strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name);
54      if (stat (name, &dsb) || sb.st_dev != dsb.st_dev ||
55          sb.st_ino != dsb.st_ino)
56        continue;
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.