diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2023-06-01 14:23:30 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-01 14:23:30 +0900 |
commit | a218ed569243e7544d8c4e3b348245a17f6c6a51 (patch) | |
tree | 6e439e7661b110bf2b3ee9fa6e2b8df54e72a92a /ext/pty | |
parent | 35da41b29bf0a1a8fd2cd7e1d7fcb036ca8c2c7c (diff) |
Hide the usage of `rb_io_t` where possible. (#7880)
This retries the compatible parts of the previously reverted PR so we can continue to update related code without breaking backwards compatibility.
Notes
Notes:
Merged-By: ioquatix <samuel@codeotaku.com>
Diffstat (limited to 'ext/pty')
-rw-r--r-- | ext/pty/pty.c | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/ext/pty/pty.c b/ext/pty/pty.c index acec33f9bf..0aca10bfa0 100644 --- a/ext/pty/pty.c +++ b/ext/pty/pty.c @@ -448,8 +448,10 @@ pty_close_pty(VALUE assoc) for (i = 0; i < 2; i++) { io = rb_ary_entry(assoc, i); - if (RB_TYPE_P(io, T_FILE) && 0 <= RFILE(io)->fptr->fd) + if (RB_TYPE_P(io, T_FILE)) { + /* it's OK to call rb_io_close again even if it's already closed */ rb_io_close(io); + } } return Qnil; } @@ -499,28 +501,21 @@ pty_open(VALUE klass) { int master_fd, slave_fd; char slavename[DEVICELEN]; - VALUE master_io, slave_file; - rb_io_t *master_fptr, *slave_fptr; - VALUE assoc; getDevice(&master_fd, &slave_fd, slavename, 1); - master_io = rb_obj_alloc(rb_cIO); - MakeOpenFile(master_io, master_fptr); - master_fptr->mode = FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX; - master_fptr->fd = master_fd; - master_fptr->pathv = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename)); + VALUE master_path = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename)); + VALUE master_io = rb_io_open_descriptor(rb_cIO, master_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX, master_path, RUBY_IO_TIMEOUT_DEFAULT, NULL); + + VALUE slave_path = rb_obj_freeze(rb_str_new_cstr(slavename)); + VALUE slave_file = rb_io_open_descriptor(rb_cFile, slave_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY, slave_path, RUBY_IO_TIMEOUT_DEFAULT, NULL); - slave_file = rb_obj_alloc(rb_cFile); - MakeOpenFile(slave_file, slave_fptr); - slave_fptr->mode = FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY; - slave_fptr->fd = slave_fd; - slave_fptr->pathv = rb_obj_freeze(rb_str_new_cstr(slavename)); + VALUE assoc = rb_assoc_new(master_io, slave_file); - assoc = rb_assoc_new(master_io, slave_file); if (rb_block_given_p()) { return rb_ensure(rb_yield, assoc, pty_close_pty, assoc); } + return assoc; } @@ -577,30 +572,27 @@ pty_getpty(int argc, VALUE *argv, VALUE self) { VALUE res; struct pty_info info; - rb_io_t *wfptr,*rfptr; - VALUE rport = rb_obj_alloc(rb_cFile); - VALUE wport = rb_obj_alloc(rb_cFile); char SlaveName[DEVICELEN]; - MakeOpenFile(rport, rfptr); - MakeOpenFile(wport, wfptr); - establishShell(argc, argv, &info, SlaveName); - rfptr->mode = rb_io_modestr_fmode("r"); - rfptr->fd = info.fd; - rfptr->pathv = rb_obj_freeze(rb_str_new_cstr(SlaveName)); + VALUE pty_path = rb_obj_freeze(rb_str_new_cstr(SlaveName)); + VALUE rport = rb_io_open_descriptor( + rb_cFile, info.fd, FMODE_READABLE, pty_path, RUBY_IO_TIMEOUT_DEFAULT, NULL + ); - wfptr->mode = rb_io_modestr_fmode("w") | FMODE_SYNC; - wfptr->fd = rb_cloexec_dup(info.fd); - if (wfptr->fd == -1) + int wpty_fd = rb_cloexec_dup(info.fd); + if (wpty_fd == -1) { rb_sys_fail("dup()"); - rb_update_max_fd(wfptr->fd); - wfptr->pathv = rfptr->pathv; + } + VALUE wport = rb_io_open_descriptor( + rb_cFile, wpty_fd, FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE | FMODE_SYNC, + pty_path, RUBY_IO_TIMEOUT_DEFAULT, NULL + ); res = rb_ary_new2(3); - rb_ary_store(res,0,(VALUE)rport); - rb_ary_store(res,1,(VALUE)wport); + rb_ary_store(res, 0, rport); + rb_ary_store(res, 1, wport); rb_ary_store(res,2,PIDT2NUM(info.child_pid)); if (rb_block_given_p()) { |