[817031f] | 1 | /* |
---|
| 2 | * This file was copied from newlib-1.8.2/newlib/unix/ttyname.c |
---|
| 3 | * and transformed into ttyname_r(). |
---|
| 4 | * |
---|
| 5 | * Copyright (c) 1988 The Regents of the University of California. |
---|
| 6 | * All rights reserved. |
---|
| 7 | * |
---|
| 8 | * Redistribution and use in source and binary forms, with or without |
---|
| 9 | * modification, are permitted provided that the following conditions |
---|
| 10 | * are met: |
---|
| 11 | * 1. Redistributions of source code must retain the above copyright |
---|
| 12 | * notice, this list of conditions and the following disclaimer. |
---|
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 14 | * notice, this list of conditions and the following disclaimer in the |
---|
| 15 | * documentation and/or other materials provided with the distribution. |
---|
| 16 | * 3. All advertising materials mentioning features or use of this software |
---|
| 17 | * must display the following acknowledgement: |
---|
| 18 | * This product includes software developed by the University of |
---|
| 19 | * California, Berkeley and its contributors. |
---|
| 20 | * 4. Neither the name of the University nor the names of its contributors |
---|
| 21 | * may be used to endorse or promote products derived from this software |
---|
| 22 | * without specific prior written permission. |
---|
| 23 | * |
---|
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 34 | * SUCH DAMAGE. |
---|
| 35 | * |
---|
| 36 | * $Id$ |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
| 40 | static char sccsid[] = "@(#)ttyname.c 5.10 (Berkeley) 5/6/91"; |
---|
| 41 | #endif /* LIBC_SCCS and not lint */ |
---|
| 42 | |
---|
| 43 | #include <sys/types.h> |
---|
| 44 | #include <sys/stat.h> |
---|
| 45 | #include <fcntl.h> |
---|
| 46 | #include <dirent.h> |
---|
| 47 | #include <termios.h> |
---|
| 48 | #include <unistd.h> |
---|
| 49 | #include <paths.h> |
---|
| 50 | #include <_syslist.h> |
---|
| 51 | #include <errno.h> |
---|
| 52 | |
---|
| 53 | #include <rtems/posix/seterr.h> |
---|
| 54 | |
---|
| 55 | /* |
---|
| 56 | * ttyname_r() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name |
---|
| 57 | */ |
---|
| 58 | |
---|
| 59 | int ttyname_r( |
---|
| 60 | int fd, |
---|
| 61 | char *name, |
---|
| 62 | int namesize |
---|
| 63 | ) |
---|
| 64 | { |
---|
| 65 | struct stat sb; |
---|
| 66 | struct termios tty; |
---|
| 67 | struct dirent *dirp; |
---|
| 68 | DIR *dp; |
---|
| 69 | struct stat dsb; |
---|
| 70 | char *rval; |
---|
| 71 | |
---|
| 72 | /* Must be a terminal. */ |
---|
| 73 | if (tcgetattr (fd, &tty) < 0) |
---|
| 74 | set_errno_and_return_minus_one(EBADF); |
---|
| 75 | |
---|
| 76 | /* Must be a character device. */ |
---|
| 77 | if (_fstat (fd, &sb) || !S_ISCHR (sb.st_mode)) |
---|
| 78 | set_errno_and_return_minus_one(EBADF); |
---|
| 79 | |
---|
| 80 | if ((dp = opendir (_PATH_DEV)) == NULL) |
---|
| 81 | set_errno_and_return_minus_one(EBADF); |
---|
| 82 | |
---|
| 83 | for (rval = NULL; (dirp = readdir (dp)) != NULL ;) |
---|
| 84 | { |
---|
| 85 | if (dirp->d_ino != sb.st_ino) |
---|
| 86 | continue; |
---|
| 87 | strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name); |
---|
| 88 | if (stat (name, &dsb) || sb.st_dev != dsb.st_dev || |
---|
| 89 | sb.st_ino != dsb.st_ino) |
---|
| 90 | continue; |
---|
| 91 | (void) closedir (dp); |
---|
| 92 | rval = name; |
---|
| 93 | break; |
---|
| 94 | } |
---|
| 95 | (void) closedir (dp); |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; |
---|
| 100 | |
---|
| 101 | /* |
---|
| 102 | * ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name |
---|
| 103 | */ |
---|
| 104 | |
---|
| 105 | char *ttyname( |
---|
| 106 | int fd |
---|
| 107 | ) |
---|
| 108 | { |
---|
| 109 | if ( !ttyname_r( fd, buf, sizeof(buf) ) ) |
---|
| 110 | return buf; |
---|
| 111 | return NULL; |
---|
| 112 | } |
---|
| 113 | |
---|