ruby-changes:70406
From: Nobuyoshi <ko1@a...>
Date: Tue, 21 Dec 2021 13:34:17 +0900 (JST)
Subject: [ruby-changes:70406] c29c2afafe (master): Refactor setup_narg and extract finish_narg
https://git.ruby-lang.org/ruby.git/commit/?id=c29c2afafe From c29c2afafe5bc550090959af24e7fbedd8e002e9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 23 Oct 2021 12:21:50 +0900 Subject: Refactor setup_narg and extract finish_narg --- io.c | 59 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/io.c b/io.c index da97102b56b..dc883f36b43 100644 --- a/io.c +++ b/io.c @@ -10092,7 +10092,7 @@ typedef int fcntl_arg_t; https://github.com/ruby/ruby/blob/trunk/io.c#L10092 #endif static long -fcntl_narg_len(int cmd) +fcntl_narg_len(ioctl_req_t cmd) { long len; @@ -10212,19 +10212,21 @@ fcntl_narg_len(int cmd) https://github.com/ruby/ruby/blob/trunk/io.c#L10212 } #else /* HAVE_FCNTL */ static long -fcntl_narg_len(int cmd) +fcntl_narg_len(ioctl_req_t cmd) { return 0; } #endif /* HAVE_FCNTL */ +#define NARG_SENTINEL 17 + static long -setup_narg(ioctl_req_t cmd, VALUE *argp, int io_p) +setup_narg(ioctl_req_t cmd, VALUE *argp, long (*narg_len)(ioctl_req_t)) { long narg = 0; VALUE arg = *argp; - if (NIL_P(arg) || arg == Qfalse) { + if (!RTEST(arg)) { narg = 0; } else if (FIXNUM_P(arg)) { @@ -10244,10 +10246,7 @@ setup_narg(ioctl_req_t cmd, VALUE *argp, int io_p) https://github.com/ruby/ruby/blob/trunk/io.c#L10246 long len, slen; *argp = arg = tmp; - if (io_p) - len = ioctl_narg_len(cmd); - else - len = fcntl_narg_len((int)cmd); + len = narg_len(cmd); rb_str_modify(arg); slen = RSTRING_LEN(arg); @@ -10259,7 +10258,7 @@ setup_narg(ioctl_req_t cmd, VALUE *argp, int io_p) https://github.com/ruby/ruby/blob/trunk/io.c#L10258 } /* a little sanity check here */ ptr = RSTRING_PTR(arg); - ptr[slen - 1] = 17; + ptr[slen - 1] = NARG_SENTINEL; narg = (long)(SIGNED_VALUE)ptr; } } @@ -10267,24 +10266,15 @@ setup_narg(ioctl_req_t cmd, VALUE *argp, int io_p) https://github.com/ruby/ruby/blob/trunk/io.c#L10266 return narg; } -#ifdef HAVE_IOCTL static VALUE -rb_ioctl(VALUE io, VALUE req, VALUE arg) +finish_narg(int retval, VALUE arg, const rb_io_t *fptr) { - ioctl_req_t cmd = NUM2IOCTLREQ(req); - rb_io_t *fptr; - long narg; - int retval; - - narg = setup_narg(cmd, &arg, 1); - GetOpenFile(io, fptr); - retval = do_ioctl(fptr->fd, cmd, narg); if (retval < 0) rb_sys_fail_path(fptr->pathv); if (RB_TYPE_P(arg, T_STRING)) { char *ptr; long slen; RSTRING_GETMEM(arg, ptr, slen); - if (ptr[slen-1] != 17) + if (ptr[slen-1] != NARG_SENTINEL) rb_raise(rb_eArgError, "return value overflowed string"); ptr[slen-1] = '\0'; } @@ -10292,6 +10282,21 @@ rb_ioctl(VALUE io, VALUE req, VALUE arg) https://github.com/ruby/ruby/blob/trunk/io.c#L10282 return INT2NUM(retval); } +#ifdef HAVE_IOCTL +static VALUE +rb_ioctl(VALUE io, VALUE req, VALUE arg) +{ + ioctl_req_t cmd = NUM2IOCTLREQ(req); + rb_io_t *fptr; + long narg; + int retval; + + narg = setup_narg(cmd, &arg, ioctl_narg_len); + GetOpenFile(io, fptr); + retval = do_ioctl(fptr->fd, cmd, narg); + return finish_narg(retval, arg, fptr); +} + /* * call-seq: * ios.ioctl(integer_cmd, arg) -> integer @@ -10369,20 +10374,10 @@ rb_fcntl(VALUE io, VALUE req, VALUE arg) https://github.com/ruby/ruby/blob/trunk/io.c#L10374 long narg; int retval; - narg = setup_narg(cmd, &arg, 0); + narg = setup_narg(cmd, &arg, fcntl_narg_len); GetOpenFile(io, fptr); retval = do_fcntl(fptr->fd, cmd, narg); - if (retval < 0) rb_sys_fail_path(fptr->pathv); - if (RB_TYPE_P(arg, T_STRING)) { - char *ptr; - long slen; - RSTRING_GETMEM(arg, ptr, slen); - if (ptr[slen-1] != 17) - rb_raise(rb_eArgError, "return value overflowed string"); - ptr[slen-1] = '\0'; - } - - return INT2NUM(retval); + return finish_narg(retval, arg, fptr); } /* -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/