4.104.114.84.95
Rev | Line | |
---|
[c7016198] | 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 <rtems/posix/seterr.h> |
---|
| 18 | |
---|
| 19 | int ttyname_r( |
---|
| 20 | int fd, |
---|
| 21 | char *name, |
---|
| 22 | int namesize |
---|
| 23 | ) |
---|
| 24 | { |
---|
| 25 | struct stat sb; |
---|
| 26 | struct termios tty; |
---|
| 27 | struct dirent *dirp; |
---|
| 28 | DIR *dp; |
---|
| 29 | struct stat dsb; |
---|
| 30 | char *rval; |
---|
| 31 | |
---|
| 32 | /* Must be a terminal. */ |
---|
| 33 | if (tcgetattr (fd, &tty) < 0) |
---|
| 34 | set_errno_and_return_minus_one(EBADF); |
---|
| 35 | |
---|
| 36 | /* Must be a character device. */ |
---|
| 37 | if (_fstat (fd, &sb) || !S_ISCHR (sb.st_mode)) |
---|
| 38 | set_errno_and_return_minus_one(EBADF); |
---|
| 39 | |
---|
| 40 | if ((dp = opendir (_PATH_DEV)) == NULL) |
---|
| 41 | set_errno_and_return_minus_one(EBADF); |
---|
| 42 | |
---|
| 43 | for (rval = NULL; (dirp = readdir (dp)) != NULL ;) |
---|
| 44 | { |
---|
| 45 | if (dirp->d_ino != sb.st_ino) |
---|
| 46 | continue; |
---|
| 47 | strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name); |
---|
| 48 | if (stat (name, &dsb) || sb.st_dev != dsb.st_dev || |
---|
| 49 | sb.st_ino != dsb.st_ino) |
---|
| 50 | continue; |
---|
| 51 | (void) closedir (dp); |
---|
| 52 | rval = name; |
---|
| 53 | break; |
---|
| 54 | } |
---|
| 55 | (void) closedir (dp); |
---|
| 56 | return 0; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; |
---|
| 60 | |
---|
| 61 | /* |
---|
| 62 | * ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name |
---|
| 63 | */ |
---|
| 64 | |
---|
| 65 | char *ttyname( |
---|
| 66 | int fd |
---|
| 67 | ) |
---|
| 68 | { |
---|
| 69 | if ( !ttyname_r( fd, buf, sizeof(buf) ) ) |
---|
| 70 | return buf; |
---|
| 71 | return NULL; |
---|
| 72 | } |
---|
| 73 | |
---|
Note: See
TracBrowser
for help on using the repository browser.