1 | /* ptsname -- return the name of a pty slave given an FD to the pty master |
2 | Copyright (C) 1999-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <string.h> |
21 | #include <sys/stat.h> |
22 | #include <hurd.h> |
23 | #include <hurd/fd.h> |
24 | #include <hurd/term.h> |
25 | |
26 | |
27 | /* Return the pathname of the pseudo terminal slave associated with |
28 | the master FD is open on, or NULL on errors. |
29 | The returned storage is good until the next call to this function. */ |
30 | char * |
31 | ptsname (int fd) |
32 | { |
33 | static string_t peername; |
34 | error_t err; |
35 | |
36 | err = __ptsname_r (fd, peername, sizeof (peername)); |
37 | |
38 | return err ? NULL : peername; |
39 | } |
40 | |
41 | |
42 | /* We don't need STP, but fill it for conformity with the Linux version... */ |
43 | int |
44 | __ptsname_internal (int fd, char *buf, size_t buflen, struct stat64 *stp) |
45 | { |
46 | string_t peername; |
47 | size_t len; |
48 | error_t err; |
49 | int ttype; |
50 | |
51 | if (HURD_DPORT_USE (fd, __term_get_bottom_type (port, &ttype)) == 0) |
52 | { |
53 | /* get_bottom_type suceeded, this is the slave side. */ |
54 | errno = ENOTTY; |
55 | return ENOTTY; |
56 | } |
57 | |
58 | if (err = HURD_DPORT_USE (fd, __term_get_peername (port, peername))) |
59 | { |
60 | if (err == EMIG_BAD_ID || err == EOPNOTSUPP) |
61 | err = ENOTTY; |
62 | return __hurd_dfail (fd, err), errno; |
63 | } |
64 | |
65 | len = __strnlen (peername, sizeof peername - 1) + 1; |
66 | if (len > buflen) |
67 | { |
68 | errno = ERANGE; |
69 | return ERANGE; |
70 | } |
71 | |
72 | if (stp) |
73 | { |
74 | if (__stat64 (peername, stp) < 0) |
75 | return errno; |
76 | } |
77 | |
78 | memcpy (buf, peername, len); |
79 | return 0; |
80 | } |
81 | |
82 | |
83 | /* Store at most BUFLEN characters of the pathname of the slave pseudo |
84 | terminal associated with the master FD is open on in BUF. |
85 | Return 0 on success, otherwise an error number. */ |
86 | int |
87 | __ptsname_r (int fd, char *buf, size_t buflen) |
88 | { |
89 | return __ptsname_internal (fd, buf, buflen, NULL); |
90 | } |
91 | libc_hidden_def (__ptsname_r) |
92 | weak_alias (__ptsname_r, ptsname_r) |
93 | |