Skip to content

Commit

Permalink
init.c: is_socket
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Nov 17, 2015
1 parent 8478b30 commit afe1429
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions ext/socket/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit afe1429

Please sign in to comment.