Skip to content

Commit dd22498

Browse files
committed
aio: implement openpty.c locally
To avoid linking with -lutil if possible Signed-off-by: Steve Bennett <[email protected]>
1 parent 50e3667 commit dd22498

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

auto.def

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,6 @@ if {[have-feature fork]} {
281281
if {[cc-check-function-in-lib backtrace execinfo]} {
282282
define-append LDLIBS [get-define lib_backtrace]
283283
}
284-
if {[cc-check-function-in-lib openpty util]} {
285-
define-append LDLIBS [get-define lib_openpty]
286-
}
287284

288285
if {[cc-check-functions sysinfo]} {
289286
cc-with {-includes sys/sysinfo.h} {
@@ -573,6 +570,12 @@ if {[have-feature windows]} {
573570
if {[have-feature termios.h]} {
574571
lappend extra_objs jim-tty.o
575572
}
573+
if {[have-feature termios.h] && [cc-check-functions posix_openpt]} {
574+
define-append AS_CFLAGS -DHAVE_OPENPTY
575+
lappend extra_objs openpty.o
576+
} elseif {[have-feature util.h]} {
577+
cc-check-function-in-lib openpty util
578+
}
576579

577580
if {[ext-get-status regexp] in {y m}} {
578581
if {![have-feature regcomp]} {

jim-aio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ static int JimAioOpenPtyCommand(Jim_Interp *interp, int argc, Jim_Obj *const *ar
24742474
return JIM_ERR;
24752475
}
24762476

2477-
/* Note: The replica path will be used for both handles slave */
2477+
/* Note: The replica path will be used for both handles */
24782478
return JimMakeChannelPair(interp, p, Jim_NewStringObj(interp, path, -1), "aio.pty%ld", 0, 0);
24792479
return JimMakeChannelPair(interp, p, Jim_NewStringObj(interp, path, -1), "aio.pty%ld", 0, 0);
24802480
}

openpty.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)