| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [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 | char *__ttyname; | 
|  | 29 |  | 
|  | 30 | static char *getttyname (int fd, dev_t mydev, ino_t myino, | 
|  | 31 | int save, int *dostat) internal_function; | 
|  | 32 |  | 
|  | 33 |  | 
|  | 34 | libc_freeres_ptr (static char *getttyname_name); | 
|  | 35 |  | 
|  | 36 | static char * | 
|  | 37 | internal_function | 
|  | 38 | getttyname (int fd, dev_t mydev, ino_t myino, int save, int *dostat) | 
|  | 39 | { | 
|  | 40 | static const char dev[] = "/dev"; | 
|  | 41 | static size_t namelen; | 
|  | 42 | struct stat st; | 
|  | 43 | DIR *dirstream; | 
|  | 44 | struct dirent *d; | 
|  | 45 |  | 
|  | 46 | dirstream = __opendir (dev); | 
|  | 47 | if (dirstream == NULL) | 
|  | 48 | { | 
|  | 49 | *dostat = -1; | 
|  | 50 | return NULL; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | while ((d = __readdir (dirstream)) != NULL) | 
|  | 54 | if (((ino_t) d->d_fileno == myino || *dostat) | 
|  | 55 | && strcmp (d->d_name, "stdin") | 
|  | 56 | && strcmp (d->d_name, "stdout") | 
|  | 57 | && strcmp (d->d_name, "stderr")) | 
|  | 58 | { | 
|  | 59 | size_t dlen = _D_ALLOC_NAMLEN (d); | 
|  | 60 | if (sizeof (dev) + dlen > namelen) | 
|  | 61 | { | 
|  | 62 | free (getttyname_name); | 
|  | 63 | namelen = 2 * (sizeof (dev) + dlen); /* Big enough.  */ | 
|  | 64 | getttyname_name = malloc (namelen); | 
|  | 65 | if (! getttyname_name) | 
|  | 66 | { | 
|  | 67 | *dostat = -1; | 
|  | 68 | /* Perhaps it helps to free the directory stream buffer.  */ | 
|  | 69 | (void) __closedir (dirstream); | 
|  | 70 | return NULL; | 
|  | 71 | } | 
|  | 72 | *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1)) | 
|  | 73 | = '/'; | 
|  | 74 | } | 
|  | 75 | (void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen); | 
|  | 76 | if (stat (getttyname_name, &st) == 0 | 
|  | 77 | #ifdef _STATBUF_ST_RDEV | 
|  | 78 | && S_ISCHR (st.st_mode) && st.st_rdev == mydev | 
|  | 79 | #else | 
|  | 80 | && (ino_t) d->d_fileno == myino && st.st_dev == mydev | 
|  | 81 | #endif | 
|  | 82 | ) | 
|  | 83 | { | 
|  | 84 | (void) __closedir (dirstream); | 
|  | 85 | __ttyname = getttyname_name; | 
|  | 86 | __set_errno (save); | 
|  | 87 | return getttyname_name; | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | (void) __closedir (dirstream); | 
|  | 92 | __set_errno (save); | 
|  | 93 | return NULL; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | /* Return the pathname of the terminal FD is open on, or NULL on errors. | 
|  | 97 | The returned storage is good only until the next call to this function.  */ | 
|  | 98 | char * | 
|  | 99 | ttyname (int fd) | 
|  | 100 | { | 
|  | 101 | struct stat st; | 
|  | 102 | int dostat = 0; | 
|  | 103 | char *name; | 
|  | 104 | int save = errno; | 
|  | 105 |  | 
|  | 106 | if (!__isatty (fd)) | 
|  | 107 | return NULL; | 
|  | 108 |  | 
|  | 109 | if (fstat (fd, &st) < 0) | 
|  | 110 | return NULL; | 
|  | 111 |  | 
|  | 112 | #ifdef _STATBUF_ST_RDEV | 
|  | 113 | name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat); | 
|  | 114 | #else | 
|  | 115 | name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat); | 
|  | 116 | #endif | 
|  | 117 |  | 
|  | 118 | if (!name && dostat != -1) | 
|  | 119 | { | 
|  | 120 | dostat = 1; | 
|  | 121 | #ifdef _STATBUF_ST_RDEV | 
|  | 122 | name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat); | 
|  | 123 | #else | 
|  | 124 | name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat); | 
|  | 125 | #endif | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | return name; | 
|  | 129 | } |