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

4.104.114.84.95
Last change on this file since e565720a was e565720a, checked in by Joel Sherrill <joel.sherrill@…>, on 05/24/01 at 21:43:08

2000-05-24 Fernando Ruiz Casas <fernando.ruiz@…>

  • libc/ttyname.c (ttyname_r): Removed duplicate call to closedir().
  • libc/getpwent.c: Create a more robust /etc/passwd and /etc/group.
  • libc/base_fs.c: Change permissions of files and directories. Now uses octal constants.
  • 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      rval = name;
57      break;
58    }
59  (void) closedir (dp);
60  return 0;
61}
62
63static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
64
65/*
66 *  ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
67 */
68
69char *ttyname(
70  int fd
71)
72{
73  if ( !ttyname_r( fd, buf, sizeof(buf) ) )
74    return buf;
75  return NULL;
76}
77
Note: See TracBrowser for help on using the repository browser.