aarch64/fpu: Add vector variants of erf
[glibc.git] / support / support_openpty.c
blobfbf9f71e738e0d385f10eb97114975b44e4841ab
1 /* Open a pseudoterminal.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <support/tty.h>
20 #include <support/check.h>
21 #include <support/support.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include <fcntl.h>
28 #include <termios.h>
29 #include <sys/ioctl.h>
30 #include <unistd.h>
32 /* As ptsname, but allocates space for an appropriately-sized string
33 using malloc. */
34 static char *
35 xptsname (int fd)
37 int rv;
38 size_t buf_len = 128;
39 char *buf = xmalloc (buf_len);
40 for (;;)
42 rv = ptsname_r (fd, buf, buf_len);
43 if (rv)
44 FAIL_EXIT1 ("ptsname_r: %s", strerror (errno));
46 if (memchr (buf, '\0', buf_len))
47 return buf; /* ptsname succeeded and the buffer was not truncated */
49 buf_len *= 2;
50 buf = xrealloc (buf, buf_len);
54 void
55 support_openpty (int *a_outer, int *a_inner, char **a_name,
56 const struct termios *termp,
57 const struct winsize *winp)
59 int outer = -1, inner = -1;
60 char *namebuf = 0;
62 outer = posix_openpt (O_RDWR | O_NOCTTY);
63 if (outer == -1)
64 FAIL_EXIT1 ("posix_openpt: %s", strerror (errno));
66 if (grantpt (outer))
67 FAIL_EXIT1 ("grantpt: %s", strerror (errno));
69 if (unlockpt (outer))
70 FAIL_EXIT1 ("unlockpt: %s", strerror (errno));
73 #ifdef TIOCGPTPEER
74 inner = ioctl (outer, TIOCGPTPEER, O_RDWR | O_NOCTTY);
75 #endif
76 if (inner == -1)
78 /* The kernel might not support TIOCGPTPEER, fall back to open
79 by name. */
80 namebuf = xptsname (outer);
81 inner = open (namebuf, O_RDWR | O_NOCTTY);
82 if (inner == -1)
83 FAIL_EXIT1 ("%s: %s", namebuf, strerror (errno));
86 if (termp)
88 if (tcsetattr (inner, TCSAFLUSH, termp))
89 FAIL_EXIT1 ("tcsetattr: %s", strerror (errno));
91 #ifdef TIOCSWINSZ
92 if (winp)
94 if (ioctl (inner, TIOCSWINSZ, winp))
95 FAIL_EXIT1 ("TIOCSWINSZ: %s", strerror (errno));
97 #endif
99 if (a_name)
101 if (!namebuf)
102 namebuf = xptsname (outer);
103 *a_name = namebuf;
105 else
106 free (namebuf);
107 *a_outer = outer;
108 *a_inner = inner;