Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: improve uv_guess_handle() implementation
Browse files Browse the repository at this point in the history
Make it understand FIFOs, character devices and sockets.
  • Loading branch information
bnoordhuis committed Mar 14, 2013
1 parent 9f714a1 commit 7b66ea1
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/unix/tty.c
Expand Up @@ -118,25 +118,52 @@ int uv_tty_get_winsize(uv_tty_t* tty, int* width, int* height) {


uv_handle_type uv_guess_handle(uv_file file) {
struct sockaddr sa;
struct stat s;
socklen_t len;
int type;

if (file < 0) {
if (file < 0)
return UV_UNKNOWN_HANDLE;
}

if (isatty(file)) {
if (isatty(file))
return UV_TTY;
}

if (fstat(file, &s)) {
if (fstat(file, &s))
return UV_UNKNOWN_HANDLE;
}

if (!S_ISSOCK(s.st_mode) && !S_ISFIFO(s.st_mode)) {
if (S_ISREG(s.st_mode))
return UV_FILE;

if (S_ISCHR(s.st_mode))
return UV_FILE; /* XXX UV_NAMED_PIPE? */

if (S_ISFIFO(s.st_mode))
return UV_NAMED_PIPE;

if (!S_ISSOCK(s.st_mode))
return UV_UNKNOWN_HANDLE;

len = sizeof(type);
if (getsockopt(file, SOL_SOCKET, SO_TYPE, &type, &len))
return UV_UNKNOWN_HANDLE;

len = sizeof(sa);
if (getsockname(file, &sa, &len))
return UV_UNKNOWN_HANDLE;

if (type == SOCK_DGRAM)
if (sa.sa_family == AF_INET || sa.sa_family == AF_INET6)
return UV_UDP;

if (type == SOCK_STREAM) {
if (sa.sa_family == AF_INET || sa.sa_family == AF_INET6)
return UV_TCP;
if (sa.sa_family == AF_UNIX)
return UV_NAMED_PIPE;
}

return UV_NAMED_PIPE;
return UV_UNKNOWN_HANDLE;
}


Expand Down

0 comments on commit 7b66ea1

Please sign in to comment.