summaryrefslogtreecommitdiff
path: root/ext/socket/init.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-17 02:15:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-17 02:15:55 +0000
commitafe142997b61f7a06563e3a7d7203c2546c7675a (patch)
treee33b8a8a1ed1cf847b506e9c1edf6e0c0fe77aa1 /ext/socket/init.c
parent8478b30267c09152f1be6f55967fa3444e878ea4 (diff)
init.c: is_socket
* ext/socket/init.c (is_socket): extract predicate to see if the given fd is a socket. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52605 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/init.c')
-rw-r--r--ext/socket/init.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 45070d5..df32565 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -42,23 +42,28 @@ rsock_raise_socket_error(const char *reason, int error)
rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
}
-VALUE
-rsock_init_sock(VALUE sock, int fd)
+#ifdef _WIN32
+#define is_socket(fd) rb_w32_is_socket(fd)
+#else
+static int
+is_socket(int fd)
{
- rb_io_t *fp;
-#ifndef _WIN32
struct stat sbuf;
if (fstat(fd, &sbuf) < 0)
rb_sys_fail("fstat(2)");
+ return S_ISSOCK(sbuf.st_mode);
+}
+#endif
+
+VALUE
+rsock_init_sock(VALUE sock, int fd)
+{
+ rb_io_t *fp;
+
rb_update_max_fd(fd);
- if (!S_ISSOCK(sbuf.st_mode))
- rb_raise(rb_eArgError, "not a socket file descriptor");
-#else
- rb_update_max_fd(fd);
- if (!rb_w32_is_socket(fd))
+ if (!is_socket(fd))
rb_raise(rb_eArgError, "not a socket file descriptor");
-#endif
MakeOpenFile(sock, fp);
fp->fd = fd;