From 0dfd5d19f3f98b596ad524e4369086fc031065cd Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 21 Apr 2022 17:03:11 +0900 Subject: [ruby/io-nonblock] Rename `io_nonblock_mode` and extract `set_fcntl_flags` https://github.com/ruby/io-nonblock/commit/22f08574df --- ext/io/nonblock/nonblock.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'ext/io/nonblock') diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c index e5915fa128..b8a40ff38e 100644 --- a/ext/io/nonblock/nonblock.c +++ b/ext/io/nonblock/nonblock.c @@ -19,14 +19,14 @@ #ifdef F_GETFL static int -io_nonblock_mode(int fd) +get_fcntl_flags(int fd) { int f = fcntl(fd, F_GETFL); if (f == -1) rb_sys_fail(0); return f; } #else -#define io_nonblock_mode(fd) ((void)(fd), 0) +#define get_fcntl_flags(fd) ((void)(fd), 0) #endif #ifdef F_GETFL @@ -41,7 +41,7 @@ rb_io_nonblock_p(VALUE io) { rb_io_t *fptr; GetOpenFile(io, fptr); - if (io_nonblock_mode(fptr->fd) & O_NONBLOCK) + if (get_fcntl_flags(fptr->fd) & O_NONBLOCK) return Qtrue; return Qfalse; } @@ -50,6 +50,13 @@ rb_io_nonblock_p(VALUE io) #endif #ifdef F_SETFL +static void +set_fcntl_flags(int fd, int f) +{ + if (fcntl(fd, F_SETFL, f) == -1) + rb_sys_fail(0); +} + static int io_nonblock_set(int fd, int f, int nb) { @@ -63,8 +70,7 @@ io_nonblock_set(int fd, int f, int nb) return 0; f &= ~O_NONBLOCK; } - if (fcntl(fd, F_SETFL, f) == -1) - rb_sys_fail(0); + set_fcntl_flags(fd, f); return 1; } @@ -123,7 +129,7 @@ rb_io_nonblock_set(VALUE io, VALUE nb) if (RTEST(nb)) rb_io_set_nonblock(fptr); else - io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb)); + io_nonblock_set(fptr->fd, get_fcntl_flags(fptr->fd), RTEST(nb)); return io; } @@ -131,8 +137,7 @@ static VALUE io_nonblock_restore(VALUE arg) { int *restore = (int *)arg; - if (fcntl(restore[0], F_SETFL, restore[1]) == -1) - rb_sys_fail(0); + set_fcntl_flags(restore[0], restore[1]); return Qnil; } @@ -159,7 +164,7 @@ rb_io_nonblock_block(int argc, VALUE *argv, VALUE io) rb_scan_args(argc, argv, "01", &v); nb = RTEST(v); } - f = io_nonblock_mode(fptr->fd); + f = get_fcntl_flags(fptr->fd); restore[0] = fptr->fd; restore[1] = f; if (!io_nonblock_set(fptr->fd, f, nb)) -- cgit v1.2.3