|
| 1 | +/* |
| 2 | + * From musl: http://www.musl-libc.org/ |
| 3 | +---------------------------------------------------------------------- |
| 4 | +Copyright © 2005-2020 Rich Felker, et al. |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining |
| 7 | +a copy of this software and associated documentation files (the |
| 8 | +"Software"), to deal in the Software without restriction, including |
| 9 | +without limitation the rights to use, copy, modify, merge, publish, |
| 10 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | +permit persons to whom the Software is furnished to do so, subject to |
| 12 | +the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be |
| 15 | +included in all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +---------------------------------------------------------------------- |
| 25 | +*/ |
| 26 | +#include <stdlib.h> |
| 27 | +#include <fcntl.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <termios.h> |
| 30 | +#include <sys/ioctl.h> |
| 31 | +#include <signal.h> |
| 32 | +#include <stdio.h> |
| 33 | + |
| 34 | +/* We implement our own openpty() on Linux so that we don't need to link against libutil */ |
| 35 | + |
| 36 | +int openpty(int *pm, int *ps, char name[20], const struct termios *tio, const struct winsize *ws) |
| 37 | +{ |
| 38 | + sig_t old_signal; |
| 39 | + int ret = -1; |
| 40 | + int m = posix_openpt(O_RDWR|O_NOCTTY); |
| 41 | + if (m < 0) return -1; |
| 42 | + |
| 43 | + old_signal = signal(SIGCHLD, SIG_DFL); |
| 44 | + if (grantpt(m) >= 0) { |
| 45 | + if (unlockpt(m) >= 0) { |
| 46 | + char buf[20]; |
| 47 | + int s; |
| 48 | + |
| 49 | + if (!name) name = buf; |
| 50 | + snprintf(name, sizeof(buf), "%s", ptsname(m)); |
| 51 | + if ((s = open(name, O_RDWR|O_NOCTTY)) >= 0) { |
| 52 | + if (tio) tcsetattr(s, TCSANOW, tio); |
| 53 | + if (ws) ioctl(s, TIOCSWINSZ, ws); |
| 54 | + |
| 55 | + *pm = m; |
| 56 | + *ps = s; |
| 57 | + |
| 58 | + ret = 0; |
| 59 | + |
| 60 | + return 0; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + signal(SIGCHLD, old_signal); |
| 65 | + |
| 66 | + if (ret) { |
| 67 | + close(m); |
| 68 | + } |
| 69 | + return ret; |
| 70 | +} |
0 commit comments