xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <limits.h> |
| 20 | #include <stddef.h> |
| 21 | #include <dirent.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | |
| 28 | #ifndef MIN |
| 29 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 30 | #endif |
| 31 | |
| 32 | static const char dev[] = "/dev"; |
| 33 | |
| 34 | static int getttyname_r (int fd, char *buf, size_t buflen, |
| 35 | dev_t mydev, ino_t myino, int save, |
| 36 | int *dostat) __THROW internal_function; |
| 37 | |
| 38 | static int |
| 39 | internal_function |
| 40 | getttyname_r (int fd, char *buf, size_t buflen, dev_t mydev, ino_t myino, |
| 41 | int save, int *dostat) |
| 42 | { |
| 43 | struct stat st; |
| 44 | DIR *dirstream; |
| 45 | struct dirent *d; |
| 46 | |
| 47 | dirstream = __opendir (dev); |
| 48 | if (dirstream == NULL) |
| 49 | { |
| 50 | *dostat = -1; |
| 51 | return errno; |
| 52 | } |
| 53 | |
| 54 | while ((d = __readdir (dirstream)) != NULL) |
| 55 | if (((ino_t) d->d_fileno == myino || *dostat) |
| 56 | && strcmp (d->d_name, "stdin") |
| 57 | && strcmp (d->d_name, "stdout") |
| 58 | && strcmp (d->d_name, "stderr")) |
| 59 | { |
| 60 | char *cp; |
| 61 | size_t needed = _D_EXACT_NAMLEN (d) + 1; |
| 62 | |
| 63 | if (needed > buflen) |
| 64 | { |
| 65 | *dostat = -1; |
| 66 | (void) __closedir (dirstream); |
| 67 | __set_errno (ERANGE); |
| 68 | return ERANGE; |
| 69 | } |
| 70 | |
| 71 | cp = __stpncpy (&buf[sizeof (dev)], d->d_name, needed); |
| 72 | cp[0] = '\0'; |
| 73 | |
| 74 | if (stat (buf, &st) == 0 |
| 75 | #ifdef _STATBUF_ST_RDEV |
| 76 | && S_ISCHR (st.st_mode) && st.st_rdev == mydev |
| 77 | #else |
| 78 | && (ino_t) d->d_fileno == myino && st.st_dev == mydev |
| 79 | #endif |
| 80 | ) |
| 81 | { |
| 82 | (void) __closedir (dirstream); |
| 83 | __set_errno (save); |
| 84 | return 0; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | (void) __closedir (dirstream); |
| 89 | __set_errno (save); |
| 90 | /* It is not clear what to return in this case. `isatty' says FD |
| 91 | refers to a TTY but no entry in /dev has this inode. */ |
| 92 | return ENOTTY; |
| 93 | } |
| 94 | |
| 95 | /* Store at most BUFLEN character of the pathname of the terminal FD is |
| 96 | open on in BUF. Return 0 on success, otherwise an error number. */ |
| 97 | int |
| 98 | __ttyname_r (int fd, char *buf, size_t buflen) |
| 99 | { |
| 100 | struct stat st; |
| 101 | int dostat = 0; |
| 102 | int save = errno; |
| 103 | int ret; |
| 104 | |
| 105 | /* Test for the absolute minimal size. This makes life easier inside |
| 106 | the loop. */ |
| 107 | if (!buf) |
| 108 | { |
| 109 | __set_errno (EINVAL); |
| 110 | return EINVAL; |
| 111 | } |
| 112 | |
| 113 | if (buflen < (int) (sizeof (dev) + 1)) |
| 114 | { |
| 115 | __set_errno (ERANGE); |
| 116 | return ERANGE; |
| 117 | } |
| 118 | |
| 119 | if (!__isatty (fd)) |
| 120 | { |
| 121 | __set_errno (ENOTTY); |
| 122 | return ENOTTY; |
| 123 | } |
| 124 | |
| 125 | if (fstat (fd, &st) < 0) |
| 126 | return errno; |
| 127 | |
| 128 | /* Prepare the result buffer. */ |
| 129 | memcpy (buf, dev, sizeof (dev) - 1); |
| 130 | buf[sizeof (dev) - 1] = '/'; |
| 131 | buflen -= sizeof (dev); |
| 132 | |
| 133 | #ifdef _STATBUF_ST_RDEV |
| 134 | ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save, |
| 135 | &dostat); |
| 136 | #else |
| 137 | ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save, |
| 138 | &dostat); |
| 139 | #endif |
| 140 | |
| 141 | if (ret && dostat != -1) |
| 142 | { |
| 143 | dostat = 1; |
| 144 | #ifdef _STATBUF_ST_RDEV |
| 145 | ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, |
| 146 | save, &dostat); |
| 147 | #else |
| 148 | ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, |
| 149 | save, &dostat); |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | weak_alias (__ttyname_r, ttyname_r) |